Unverified Commit 0525ec3c authored by Josh Story's avatar Josh Story Committed by GitHub

Update app-index to only ever construct the initial data response once (#61869)

There was a bug where if the root hydrates and then an update happens
the intitial server data response can be replaced by one that is
completely empty and will never resolve. This can lead to a frozen
hydration that blocks interactivity. This udpate makes it so it is
impossible for the initial data response to ever be created more than
once.

Making a regression test is tricky because this relies on subtle timing
of hydration, updates, and when the inline chunks arrive in the stream.
The original implementation is just not safe in that it violates the
rules of react and the new one is self-evidently unable to produce a
similar situation so as long as our existing test suite passes that must
be sufficient absent a good alternative to making a specific regression
test

Closes NEXT-2420
parent 34f9039d
/* global location */
import '../build/polyfills/polyfill-module'
// @ts-ignore react-dom/client exists when using React 18
import ReactDOMClient from 'react-dom/client'
......@@ -37,11 +36,6 @@ window.addEventListener('error', (ev: WindowEventMap['error']): void => {
const appElement: HTMLElement | Document | null = document
const getCacheKey = () => {
const { pathname, search } = location
return pathname + search
}
const encoder = new TextEncoder()
let initialServerDataBuffer: string[] | undefined = undefined
......@@ -118,36 +112,18 @@ const nextServerDataLoadingGlobal = ((self as any).__next_f =
nextServerDataLoadingGlobal.forEach(nextServerDataCallback)
nextServerDataLoadingGlobal.push = nextServerDataCallback
function createResponseCache() {
return new Map<string, any>()
}
const rscCache = createResponseCache()
function useInitialServerResponse(cacheKey: string): Promise<JSX.Element> {
const response = rscCache.get(cacheKey)
if (response) return response
const readable = new ReadableStream({
start(controller) {
nextServerDataRegisterWriter(controller)
},
})
const newResponse = createFromReadableStream(readable, {
callServer,
})
const readable = new ReadableStream({
start(controller) {
nextServerDataRegisterWriter(controller)
},
})
rscCache.set(cacheKey, newResponse)
return newResponse
}
const initialServerResponse = createFromReadableStream(readable, {
callServer,
})
function ServerRoot({ cacheKey }: { cacheKey: string }): JSX.Element {
React.useEffect(() => {
rscCache.delete(cacheKey)
})
const response = useInitialServerResponse(cacheKey)
const root = use(response)
return root
function ServerRoot(): React.ReactNode {
return use(initialServerResponse)
}
const StrictModeIfEnabled = process.env.__NEXT_STRICT_MODE_APP
......@@ -177,10 +153,6 @@ function Root({ children }: React.PropsWithChildren<{}>): React.ReactElement {
return children as React.ReactElement
}
function RSCComponent(props: any): JSX.Element {
return <ServerRoot {...props} cacheKey={getCacheKey()} />
}
export function hydrate() {
if (process.env.NODE_ENV !== 'production') {
const rootLayoutMissingTagsError = (self as any)
......@@ -238,7 +210,7 @@ export function hydrate() {
>
<ActionQueueContext.Provider value={actionQueue}>
<Root>
<RSCComponent />
<ServerRoot />
</Root>
</ActionQueueContext.Provider>
</HeadManagerContext.Provider>
......
......@@ -49,7 +49,6 @@ describe('Component Stack in error overlay', () => {
ErrorBoundary
AppRouter
ServerRoot
RSCComponent
Root"
`)
} else {
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment