Skip to content

Commit

Permalink
Fix missing client reference manifest error when using route groups (#…
Browse files Browse the repository at this point in the history
…73606)

While splitting up the `"use cache"` e2e test pages into two separate root layout groups (one with a suspense boundary, one without), I noticed that there's a bug when retrieving the client reference manifest for a page in a route group.

To fix it, we're now normalizing the `page` that's passed into `setReferenceManifestsSingleton` with the existing `normalizeAppPath` helper. When the manifest is retrieved, we're using the already normalized `workStore.route` instead of `workStore.page`.
  • Loading branch information
unstubbable authored Dec 6, 2024
1 parent 3fbb2a6 commit 2772f8e
Show file tree
Hide file tree
Showing 35 changed files with 20 additions and 12 deletions.
16 changes: 7 additions & 9 deletions packages/next/src/server/app-render/encryption-utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import type {
} from '../../build/webpack/plugins/flight-manifest-plugin'
import type { DeepReadonly } from '../../shared/lib/deep-readonly'
import { InvariantError } from '../../shared/lib/invariant-error'
import { normalizeAppPath } from '../../shared/lib/router/utils/app-paths'
import { workAsyncStorage } from './work-async-storage.external'

let __next_loaded_action_key: CryptoKey
Expand Down Expand Up @@ -98,7 +99,7 @@ export function setReferenceManifestsSingleton({
globalThis[SERVER_ACTION_MANIFESTS_SINGLETON] = {
clientReferenceManifestsPerPage: {
...clientReferenceManifestsPerPage,
[normalizePage(page)]: clientReferenceManifest,
[normalizeAppPath(page)]: clientReferenceManifest,
},
serverActionsManifest,
serverModuleMap,
Expand Down Expand Up @@ -153,12 +154,13 @@ export function getClientReferenceManifestForRsc(): DeepReadonly<ClientReference
return mergeClientReferenceManifests(clientReferenceManifestsPerPage)
}

const page = normalizePage(workStore.page)

const clientReferenceManifest = clientReferenceManifestsPerPage[page]
const clientReferenceManifest =
clientReferenceManifestsPerPage[workStore.route]

if (!clientReferenceManifest) {
throw new InvariantError(`Missing Client Reference Manifest for ${page}.`)
throw new InvariantError(
`Missing Client Reference Manifest for ${workStore.route}.`
)
}

return clientReferenceManifest
Expand Down Expand Up @@ -198,10 +200,6 @@ export async function getActionEncryptionKey() {
return __next_loaded_action_key
}

function normalizePage(page: string): string {
return page.replace(/\/(page|route)$/, '')
}

function mergeClientReferenceManifests(
clientReferenceManifestsPerPage: DeepReadonly<
Record<string, ClientReferenceManifest>
Expand Down
File renamed without changes.
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
'use client'

import { foo, bar, baz } from './cached'
import { Form } from '../form'
import { Form } from '../../form'

export default function Page() {
return <Form foo={foo} bar={bar} baz={baz} />
Expand Down
7 changes: 7 additions & 0 deletions test/e2e/app-dir/use-cache/app/(no-suspense)/layout.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
export default function Root({ children }: { children: React.ReactNode }) {
return (
<html>
<body>{children}</body>
</html>
)
}
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { Form } from '../form'
import { Form } from '../../form'

function getRandomValue() {
const v = Math.random()
Expand Down
File renamed without changes.
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
// TODO: This should not need the suspense boundary in the root layout.
'use cache'

import { notFound } from 'next/navigation'
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { Foo } from './client'
import { Foo } from '../client'

async function getCachedRandom(x: number, children: React.ReactNode) {
'use cache'
Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
// TODO: This should not need the suspense boundary in the root layout, but
// currently does with Turbopack.
import { Form } from './form'

async function action() {
Expand Down
Binary file added test/e2e/app-dir/use-cache/public/favicon.ico
Binary file not shown.

0 comments on commit 2772f8e

Please sign in to comment.