Skip to content

Commit a0de92e

Browse files
authored
types: improve types in app-index (vercel#75045)
1 parent 3141f07 commit a0de92e

File tree

1 file changed

+24
-16
lines changed

1 file changed

+24
-16
lines changed

packages/next/src/client/app-index.tsx

Lines changed: 24 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ import { shouldRenderRootLevelErrorOverlay } from './lib/is-error-thrown-while-r
2929

3030
/// <reference types="react-dom/experimental" />
3131

32-
const appElement: HTMLElement | Document | null = document
32+
const appElement: HTMLElement | Document = document
3333

3434
const encoder = new TextEncoder()
3535

@@ -41,13 +41,24 @@ let initialServerDataFlushed = false
4141

4242
let initialFormStateData: null | any = null
4343

44-
function nextServerDataCallback(
45-
seg:
46-
| [isBootStrap: 0]
47-
| [isNotBootstrap: 1, responsePartial: string]
48-
| [isFormState: 2, formState: any]
49-
| [isBinary: 3, responseBase64Partial: string]
50-
): void {
44+
type FlightSegment =
45+
| [isBootStrap: 0]
46+
| [isNotBootstrap: 1, responsePartial: string]
47+
| [isFormState: 2, formState: any]
48+
| [isBinary: 3, responseBase64Partial: string]
49+
50+
type NextFlight = Omit<Array<FlightSegment>, 'push'> & {
51+
push: (seg: FlightSegment) => void
52+
}
53+
54+
declare global {
55+
// If you're working in a browser environment
56+
interface Window {
57+
__next_f: NextFlight
58+
}
59+
}
60+
61+
function nextServerDataCallback(seg: FlightSegment): void {
5162
if (seg[0] === 0) {
5263
initialServerDataBuffer = []
5364
} else if (seg[0] === 1) {
@@ -134,8 +145,7 @@ if (document.readyState === 'loading') {
134145
setTimeout(DOMContentLoaded)
135146
}
136147

137-
const nextServerDataLoadingGlobal = ((self as any).__next_f =
138-
(self as any).__next_f || [])
148+
const nextServerDataLoadingGlobal = (self.__next_f = self.__next_f || [])
139149
nextServerDataLoadingGlobal.forEach(nextServerDataCallback)
140150
nextServerDataLoadingGlobal.push = nextServerDataCallback
141151

@@ -256,16 +266,14 @@ export function hydrate() {
256266
element = createRootLevelDevOverlayElement(element)
257267
}
258268

259-
ReactDOMClient.createRoot(appElement as any, reactRootOptions).render(
260-
element
261-
)
269+
ReactDOMClient.createRoot(appElement, reactRootOptions).render(element)
262270
} else {
263-
React.startTransition(() =>
264-
(ReactDOMClient as any).hydrateRoot(appElement, reactEl, {
271+
React.startTransition(() => {
272+
ReactDOMClient.hydrateRoot(appElement, reactEl, {
265273
...reactRootOptions,
266274
formState: initialFormStateData,
267275
})
268-
)
276+
})
269277
}
270278

271279
// TODO-APP: Remove this logic when Float has GC built-in in development.

0 commit comments

Comments
 (0)