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

Separate the rendering of viewport from metadata #70685

Merged
merged 1 commit into from
Oct 2, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
46 changes: 46 additions & 0 deletions packages/next/src/lib/metadata/metadata-context.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
import type { AppRenderContext } from '../../server/app-render/app-render'
import type { MetadataContext } from './types/resolvers'
import type { StaticGenerationStore } from '../../client/components/static-generation-async-storage.external'
import { trackFallbackParamAccessed } from '../../server/app-render/dynamic-rendering'

export function createMetadataContext(
pathname: string,
renderOpts: AppRenderContext['renderOpts']
): MetadataContext {
return {
pathname,
trailingSlash: renderOpts.trailingSlash,
isStandaloneMode: renderOpts.nextConfigOutput === 'standalone',
}
}

export function createTrackedMetadataContext(
pathname: string,
renderOpts: AppRenderContext['renderOpts'],
staticGenerationStore: StaticGenerationStore | null
): MetadataContext {
return {
// Use the regular metadata context, but we trap the pathname access.
Comment on lines +1 to +23
Copy link
Contributor Author

Choose a reason for hiding this comment

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

Broke this file out of metadata because it is imported directly into app-render and not proxied through entry-base. this is a style choice more than anything since most of the code in app-render ought to be explicitly in the RSC layer. We can solve that problem some other time but for now I wanted to make it so metadata.tsx was only imported in the RSC layer and not anywhere else.

...createMetadataContext(pathname, renderOpts),

// Setup the trap around the pathname access so we can track when the
// pathname is accessed while resolving metadata which would indicate it's
// being used to resolve a relative URL. If that's the case, we don't want
// to provide it, and instead we should error.
get pathname() {
if (
staticGenerationStore &&
staticGenerationStore.isStaticGeneration &&
staticGenerationStore.fallbackRouteParams &&
staticGenerationStore.fallbackRouteParams.size > 0
) {
trackFallbackParamAccessed(
staticGenerationStore,
'metadata relative url resolving'
)
}

return pathname
},
}
}
Loading
Loading