Repository: vercel/next.js
File: packages/next/src/lib/metadata/metadata.tsx (TypeScript)

Code before:
// and the error will be caught by the error boundary and trigger fallbacks.
export function createMetadataComponents({
  tree,
  pathname,
  parsedQuery,
  metadataContext,
  getDynamicParamFromSegment,
  errorType,
  workStore,
  serveStreamingMetadata,
  isRuntimePrefetchable,
}: {
  tree: LoaderTree
  pathname: string
  parsedQuery: SearchParams
  metadataContext: MetadataContext
  getDynamicParamFromSegment: GetDynamicParamFromSegment
  errorType?: MetadataErrorType | 'redirect'
  workStore: WorkStore
  serveStreamingMetadata: boolean
  isRuntimePrefetchable: boolean
}): {
  Viewport: React.ComponentType
  Metadata: React.ComponentType
  MetadataOutlet: React.ComponentType
} {
  const searchParams = createServerSearchParamsForMetadata(
    parsedQuery,
    workStore,
    isRuntimePrefetchable
  )
  const pathnameForMetadata = createServerPathnameForMetadata(
    pathname,
    workStore
  )

  async function Viewport() {
    // Gate metadata to the correct render stage. If the page is not
    // runtime-prefetchable, defer until the Static stage so that
    // prefetchable segments get a head start.
    if (!isRuntimePrefetchable) {
      const workUnitStore = workUnitAsyncStorage.getStore()
      if (workUnitStore) {
        let stagedRendering: StagedRenderingController | null | undefined
        switch (workUnitStore.type) {
          case 'request':
          case 'prerender-runtime':
            stagedRendering = workUnitStore.stagedRendering
            break
          case 'prerender':
          case 'prerender-client':

Review comment (performance):
i think it's time to make a `getStagedRenderingController(workUnitStore)` util, like `getCacheSignal` and friends

Code after:
  errorType?: MetadataErrorType | 'redirect'
  serveStreamingMetadata: boolean
  isRuntimePrefetchable: boolean
}): {
  Viewport: React.ComponentType
  Metadata: React.ComponentType
  MetadataOutlet: React.ComponentType
} {
  const searchParams = createServerSearchParamsForMetadata(
    parsedQuery,
    isRuntimePrefetchable
  )
  const pathnameForMetadata = createServerPathnameForMetadata(pathname)

  async function Viewport() {
    // Gate metadata to the correct render stage. If the page is not
    // runtime-prefetchable, defer until the Static stage so that
    // prefetchable segments get a head start.
    if (!isRuntimePrefetchable) {
      const workUnitStore = workUnitAsyncStorage.getStore()
      if (workUnitStore) {
        const stagedRendering = getStagedRenderingController(workUnitStore)
        if (stagedRendering) {
          await stagedRendering.waitForStage(RenderStage.Static)
        }
      }
    }

    const tags = await getResolvedViewport(
      tree,
      searchParams,
      interpolatedParams,
      isRuntimePrefetchable,
      errorType
    ).catch((viewportErr) => {
      // When Legacy PPR is enabled viewport can reject with a Postpone type
      // This will go away once Legacy PPR is removed and dynamic metadata will
      // stay pending until after the prerender is complete when it is dynamic
      if (isPostpone(viewportErr)) {
        throw viewportErr
      }
      if (!errorType && isHTTPAccessFallbackError(viewportErr)) {
        return getNotFoundViewport(
          tree,
          searchParams,
          interpolatedParams,
          isRuntimePrefetchable
        ).catch(() => null)
      }
      // We're going to throw the error from the metadata outlet so we just render null here instead
      return null