Skip to content

chore: sync nextjs template with example - #3926

Merged
fredericoo merged 5 commits into
previewfrom
fb-update-nextjs-template-cart
Aug 6, 2026
Merged

fredericoo merged 5 commits into
previewfrom
fb-update-nextjs-template-cart

Conversation

@fredericoo

Copy link
Copy Markdown
Contributor

TL;DR: Updates the Next.js template to match the current example pattern for Cache Components, streamed app shell data, and Suspense-backed cart state.

Before

The root layout waited for cart and navigation data before rendering the template shell:

const [{data: cartData}, navResult] = await Promise.all([
  cartHandlers.get({storefrontClient}),
  storefrontClient.graphql(NAV_COLLECTIONS_QUERY),
]);

Cart UI read directly from useCart, so initial cart loading was handled as local loading state rather than a Suspense boundary.

After

The root layout is a static shell. Request-bound cart and navigation setup lives in AppShell, which streams under Suspense:

<Suspense fallback={<AppShellFallback />}>
  <AppShell>{children}</AppShell>
</Suspense>

The cart seed is passed to CartProvider as a promise, and cart surfaces that need resolved cart data use useSuspenseCart().

What this changes

  • Enables cacheComponents for the Next.js template.
  • Adds an AppShell server component for request-bound cart and nav setup.
  • Passes the cart seed promise into CartProvider so the shell can stream.
  • Exports and uses useSuspenseCart() for full-cart reads.
  • Removes force-dynamic route exports that are not compatible with Cache Components.
  • Adds accessible loading fallbacks for the app shell, cart route, and redirect checks.
  • Keeps the cart drawer header stable while only the cart body streams.
  • Emits cart_viewed once per drawer open cycle instead of on every cart update while open.

UX impact

The storefront shell can render before personalised cart data resolves. Loading states keep main#main-content and status text available so keyboard and assistive tech users do not lose page context during Suspense fallbacks.

Out of scope

  • Existing template lint debt in CollectionBrowse.tsx and standard-actions.d.ts.
  • Existing @next/next/no-img-element warnings from the template image strategy.

Risk

  • Redirect checks now render a neutral "Checking redirect..." fallback before showing the 404 content. This avoids announcing "Not found" before a Shopify redirect lookup completes, but still relies on Next's streamed redirect behaviour under Cache Components.
  • Cart drawer analytics now publish once per open cycle. If analytics expected another cart_viewed after each in-drawer cart mutation, this changes that behaviour intentionally.

How to Test

  1. From templates/nextjs, run pnpm dev.
  2. Open http://localhost:3000.
  3. Confirm the homepage renders and the cart count appears after cart data resolves.
  4. Open the cart drawer and confirm the header/close button stay stable while cart content loads.
  5. Visit /cart and confirm the cart route renders with the same cart state.
  6. Visit /collections, a collection page, /search, and a product page to confirm the streamed shell still wraps each route.

@fredericoo
fredericoo force-pushed the fb-update-nextjs-template-cart branch from 0f2ef5c to 523ed72 Compare August 6, 2026 10:49
@fredericoo fredericoo self-assigned this Aug 6, 2026
Assisted-By: devx/c4e58df2-7ec2-41dc-960e-461d976f5ab0
Assisted-By: devx/c4e58df2-7ec2-41dc-960e-461d976f5ab0
@fredericoo
fredericoo force-pushed the fb-update-nextjs-template-cart branch from 060c16a to 0b51850 Compare August 6, 2026 13:47
@fredericoo
fredericoo marked this pull request as ready for review August 6, 2026 13:49
@fredericoo
fredericoo requested a review from a team as a code owner August 6, 2026 13:49
@fredericoo fredericoo changed the title Stream cart in Next.js template chore: sync nextjs template with example Aug 6, 2026
Assisted-By: devx/93464ba5-5073-4a80-8430-e1b04505804d
Comment thread .docs/dependencies.md Outdated

@andguy95 andguy95 left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

Top hatted template locally, looks to behave accordingly.

@fredericoo
fredericoo merged commit 7925412 into preview Aug 6, 2026
3 of 4 checks passed
@fredericoo
fredericoo deleted the fb-update-nextjs-template-cart branch August 6, 2026 21:05
IgorOffline pushed a commit to IgorOffline/Shopify-hydrogen that referenced this pull request Oct 10, 2026
* Sync Next.js template with example

Assisted-By: devx/c4e58df2-7ec2-41dc-960e-461d976f5ab0

* Show account icon in Next.js template

Assisted-By: devx/c4e58df2-7ec2-41dc-960e-461d976f5ab0

* docs: add nextjs agents md

Assisted-By: devx/93464ba5-5073-4a80-8430-e1b04505804d

* chore: pin preview version of hydrogen

* chore: remove dependencies.md
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants