Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

🐛 Handle cases where sanity assets are undefined #3682

Merged
merged 1 commit into from
Mar 20, 2025
Merged

Conversation

KenAJoh
Copy link
Collaborator

@KenAJoh KenAJoh commented Mar 20, 2025

Description

In cases where assets were "valid", example {} the URL-lookup would crash the page. More specifically .url() crashed it. This update solves it by returning undefined in cases where the image would be invalid.

if (!source?.asset?._ref) {
    return undefined;
  }

Component Checklist 📝

  • JSDoc
  • Examples
  • Documentation / Decision Records
  • Storybook
  • Style mappings (@navikt/core/css/config/_mappings.js)
  • Component tokens (@navikt/core/css/tokens.json)
  • CSS class deprecations (@navikt/aksel-stylelint/src/deprecations.ts)
  • Exports (@navikt/core/react/src/index.ts and @navikt/core/react/package.json)
  • New component? CSS import (@navikt/core/css/index.css)
  • Breaking change? Update migration guide. Consider codemod.
  • Changeset (Format: <Component>: <gitmoji?> <Text>. E.g. "Button: ✨ Add feature xyz.")

@KenAJoh KenAJoh requested a review from Copilot March 20, 2025 09:19
Copy link

changeset-bot bot commented Mar 20, 2025

⚠️ No Changeset found

Latest commit: 799a555

Merging this PR will not cause a version bump for any packages. If these changes should not result in a new version, you're good to go. If these changes should result in a version bump, you need to add a changeset.

Click here to learn what changesets are, and how to add one.

Click here if you're a maintainer who wants to add a changeset to this PR

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull Request Overview

This PR addresses a bug where accessing properties on undefined or empty sanity assets was crashing the page. It introduces guard checks within the image URL generation and refactors multiple components to compute image URLs before rendering.

  • Introduces a guard check in urlFor (interface.ts) to return undefined when asset references are missing.
  • Updates image rendering in several components (Card.tsx, Highlight.tsx, SEO.tsx, etc.) to use precomputed image URLs.
  • Adjusts the queries and conditional rendering to avoid crashes when assets are invalid.

Reviewed Changes

Copilot reviewed 18 out of 18 changed files in this pull request and generated no comments.

Show a summary per file
File Description
components/sanity-modules/frontpage-blocks/latest-articles/Card.tsx Refactored image rendering logic with precomputed URLs for status and fallback images.
pages/produktbloggen/[slug].tsx Precomputed SEO image URLs for improved error handling.
components/sanity-modules/do-dont/DoDont.tsx Updated image URL handling to avoid rendering invalid images.
components/website-modules/seo/SEO.tsx Adjusted meta tag generation to use precomputed image URLs.
components/sanity-modules/frontpage-blocks/latest-articles/Highlight.tsx Refactored image rendering with URL variables for both status and SEO images.
components/website-modules/search/parts/Hit.tsx Simplified image URL usage in search hit rendering.
components/sanity-modules/bilde/Bilde.tsx Added check for valid image URL before rendering image.
components/layout/god-praksis-page/cards/GpFrontpageCard.tsx Updated conditional rendering to use precomputed image URLs.
app/_ui/global-search/GlobalSearch.hit.tsx Streamlined image URL usage for global search results.
sanity/interface/interface.ts Introduced a guard in urlFor to return undefined for invalid asset references.
components/website-modules/blogg-page/parts/HighlightedBlogg.tsx Refactored to use precomputed SEO image URLs.
components/sanity-modules/compare-images/CompareImages.tsx Added checks for both images to avoid rendering when missing.
components/layout/templates/WithSidebar.tsx Updated rendering logic for sidebar images using computed image URLs.
components/website-modules/blogg-page/parts/BloggList.tsx Refactored image rendering in the blog list to improve reliability.
pages/index.tsx Adjusted the query for tema to support the PR’s purpose.
components/layout/god-praksis-page/cards/GpHeroCard.tsx Updated hero card image rendering using a precomputed URL.
components/layout/god-praksis-page/hero/tema-hero/parts/HeroIntro.tsx Simplified hero intro image rendering by computing the image URL prior.
components/sanity-modules/component-overview/ComponentOverview.tsx Adopted precomputed image URLs in the component overview for consistency.
Comments suppressed due to low confidence (1)

aksel.nav.no/website/components/sanity-modules/frontpage-blocks/latest-articles/Card.tsx:67

  • [nitpick] The variable name 'Image' may be confused with a React component. Consider renaming it to something like 'imageElement' or 'renderedImage' to improve clarity.
let Image = (
@KenAJoh KenAJoh merged commit 46c0f43 into main Mar 20, 2025
6 checks passed
@KenAJoh KenAJoh deleted the image-fallback branch March 20, 2025 10:45
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant