Separate the rendering of viewport from metadata (#70685)
viewport will never be streaming because is necessarily blocks important shell time rendering with things like themeColor and viewport size. Most other metadata can be streamed in if we're not serving an agent which doesn't construct a DOM. This change doesn't actually make metadata streaming but it does further separate the two so we can more easily alter the rendering characteristics of metadata without affecting viewport. The approach is to ensure the metadata component creation is in the react-server layer in the server module graph so it can access `React.cache`. then we construct a pair of functions that can produce the resolved metadata and viewport values, throwing if there is an encountered error. We suppress these errors in our top level metadata component (MetadataRoot) and and simultaneously expose a ready function which calls these (`React.cache()`'d) functions in the Metadata outlet where any errors will throw. This cleans up the layering quite a bit and makes some runtime error checks unecessary such as ensuring you rendered the MetadataRoot before attempting to determine if the metadata was resolved. The key thing achieved here though is that Metadata and Viewport now resolve in parallel but separately. We avoid double walking the module graph by again making use of `React.cache()`. In the future we can render each of these components differently without affecting each other. This work is motivated more immediately by some error tracking work I will land after this that needs to determine whether viewport or metadata were dynamic independently. There is one semantic difference worth calling out. Previously if you called `notFound()` in `generateMetadata()` or `generateViewport()` both would render the appropriate not found metadata/viewport. In this latest update only the one that called notFound will update to reflect the notFound metadata/viewport. I think fundamentally the approach to handling notFound needs to change here. Really we should be abandoning the render and starting over. This will be doubly important once metadata is streaming. I'm ok with the edge case as is given customizing viewport is not very common and calling notFound from within generateViewport is expected to be an edge case of an edge case. The workaround for now is to call notFound simultaneously from both generateViewport and generateMetadata
Showing
This diff is collapsed.
Please
register
or
sign in
to comment