Skip to content

Commit

Permalink
getClientReferenceManifestSingleton before we enter a clean AsyncLoca…
Browse files Browse the repository at this point in the history
…lStorage

This ensures that if we implement that using AsyncLocalStorage we get the
outer one that's currently executing the page.
  • Loading branch information
sebmarkbage committed Sep 26, 2024
1 parent 6cbcdcc commit 0e49218
Showing 1 changed file with 15 additions and 3 deletions.
18 changes: 15 additions & 3 deletions packages/next/src/server/use-cache/use-cache-wrapper.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import type { DeepReadonly } from '../../shared/lib/deep-readonly'
import { createSnapshot } from '../../client/components/async-local-storage'
/* eslint-disable import/no-extraneous-dependencies */
import {
Expand All @@ -15,6 +16,8 @@ import {
import type { StaticGenerationStore } from '../../client/components/static-generation-async-storage.external'
import { staticGenerationAsyncStorage } from '../../client/components/static-generation-async-storage.external'

import type { ClientReferenceManifest } from '../../build/webpack/plugins/flight-manifest-plugin'

import {
getClientReferenceManifestSingleton,
getServerModuleMap,
Expand Down Expand Up @@ -75,6 +78,7 @@ const runInCleanSnapshot = createSnapshot()

async function generateCacheEntry(
staticGenerationStore: StaticGenerationStore,
clientReferenceManifest: DeepReadonly<ClientReferenceManifest>,
cacheHandler: CacheHandler,
serializedCacheKey: string | ArrayBuffer,
encodedArguments: FormData | string,
Expand All @@ -96,11 +100,9 @@ async function generateCacheEntry(
let didError = false
let firstError: any = null

const clientReferenceManifestSingleton = getClientReferenceManifestSingleton()

const stream = renderToReadableStream(
result,
clientReferenceManifestSingleton.clientModules,
clientReferenceManifest.clientModules,
{
environmentName: 'Cache',
temporaryReferences,
Expand Down Expand Up @@ -216,9 +218,16 @@ export function cache(kind: string, id: string, fn: any) {
// might include request specific things like cookies() inside a React.cache().
// Note: It is important that we await at least once before this because it lets us
// pop out of any stack specific contexts as well - aka "Sync" Local Storage.

// Get the clientReferenceManifestSingleton while we're still in the outer Context.
// In case getClientReferenceManifestSingleton is implemented using AsyncLocalStorage.
const clientReferenceManifestSingleton =
getClientReferenceManifestSingleton()

stream = await runInCleanSnapshot(
generateCacheEntry,
staticGenerationStore,
clientReferenceManifestSingleton,
cacheHandler,
serializedCacheKey,
encodedArguments,
Expand All @@ -229,9 +238,12 @@ export function cache(kind: string, id: string, fn: any) {
if (entry.stale) {
// If this is stale, and we're not in a prerender (i.e. this is dynamic render),
// then we should warm up the cache with a fresh revalidated entry.
const clientReferenceManifestSingleton =
getClientReferenceManifestSingleton()
const ignoredStream = await runInCleanSnapshot(
generateCacheEntry,
staticGenerationStore,
clientReferenceManifestSingleton,
cacheHandler,
serializedCacheKey,
encodedArguments,
Expand Down

0 comments on commit 0e49218

Please sign in to comment.