|
| 1 | +/* global location */ |
| 2 | +import '../build/polyfills/polyfill-module' |
| 3 | +// @ts-ignore react-dom/client exists when using React 18 |
| 4 | +import ReactDOMClient from 'react-dom/client' |
| 5 | +// @ts-ignore startTransition exists when using React 18 |
| 6 | +import React, { useState, startTransition } from 'react' |
| 7 | +import { RefreshContext } from './streaming/refresh' |
| 8 | +import { createFromFetch } from 'next/dist/compiled/react-server-dom-webpack' |
| 9 | + |
| 10 | +/// <reference types="react-dom/experimental" /> |
| 11 | + |
| 12 | +export const version = process.env.__NEXT_VERSION |
| 13 | + |
| 14 | +const appElement: HTMLElement | Document | null = document |
| 15 | + |
| 16 | +let reactRoot: any = null |
| 17 | + |
| 18 | +function renderReactElement( |
| 19 | + domEl: HTMLElement | Document, |
| 20 | + fn: () => JSX.Element |
| 21 | +): void { |
| 22 | + const reactEl = fn() |
| 23 | + if (!reactRoot) { |
| 24 | + // Unlike with createRoot, you don't need a separate root.render() call here |
| 25 | + reactRoot = (ReactDOMClient as any).hydrateRoot(domEl, reactEl) |
| 26 | + } else { |
| 27 | + reactRoot.render(reactEl) |
| 28 | + } |
| 29 | +} |
| 30 | + |
| 31 | +const getCacheKey = () => { |
| 32 | + const { pathname, search } = location |
| 33 | + return pathname + search |
| 34 | +} |
| 35 | + |
| 36 | +const encoder = new TextEncoder() |
| 37 | + |
| 38 | +let initialServerDataBuffer: string[] | undefined = undefined |
| 39 | +let initialServerDataWriter: WritableStreamDefaultWriter | undefined = undefined |
| 40 | +let initialServerDataLoaded = false |
| 41 | +let initialServerDataFlushed = false |
| 42 | + |
| 43 | +function nextServerDataCallback(seg: [number, string, string]) { |
| 44 | + if (seg[0] === 0) { |
| 45 | + initialServerDataBuffer = [] |
| 46 | + } else { |
| 47 | + if (!initialServerDataBuffer) |
| 48 | + throw new Error('Unexpected server data: missing bootstrap script.') |
| 49 | + |
| 50 | + if (initialServerDataWriter) { |
| 51 | + initialServerDataWriter.write(encoder.encode(seg[2])) |
| 52 | + } else { |
| 53 | + initialServerDataBuffer.push(seg[2]) |
| 54 | + } |
| 55 | + } |
| 56 | +} |
| 57 | + |
| 58 | +// There might be race conditions between `nextServerDataRegisterWriter` and |
| 59 | +// `DOMContentLoaded`. The former will be called when React starts to hydrate |
| 60 | +// the root, the latter will be called when the DOM is fully loaded. |
| 61 | +// For streaming, the former is called first due to partial hydration. |
| 62 | +// For non-streaming, the latter can be called first. |
| 63 | +// Hence, we use two variables `initialServerDataLoaded` and |
| 64 | +// `initialServerDataFlushed` to make sure the writer will be closed and |
| 65 | +// `initialServerDataBuffer` will be cleared in the right time. |
| 66 | +function nextServerDataRegisterWriter(writer: WritableStreamDefaultWriter) { |
| 67 | + if (initialServerDataBuffer) { |
| 68 | + initialServerDataBuffer.forEach((val) => { |
| 69 | + writer.write(encoder.encode(val)) |
| 70 | + }) |
| 71 | + if (initialServerDataLoaded && !initialServerDataFlushed) { |
| 72 | + writer.close() |
| 73 | + initialServerDataFlushed = true |
| 74 | + initialServerDataBuffer = undefined |
| 75 | + } |
| 76 | + } |
| 77 | + |
| 78 | + initialServerDataWriter = writer |
| 79 | +} |
| 80 | + |
| 81 | +// When `DOMContentLoaded`, we can close all pending writers to finish hydration. |
| 82 | +const DOMContentLoaded = function () { |
| 83 | + if (initialServerDataWriter && !initialServerDataFlushed) { |
| 84 | + initialServerDataWriter.close() |
| 85 | + initialServerDataFlushed = true |
| 86 | + initialServerDataBuffer = undefined |
| 87 | + } |
| 88 | + initialServerDataLoaded = true |
| 89 | +} |
| 90 | +// It's possible that the DOM is already loaded. |
| 91 | +if (document.readyState === 'loading') { |
| 92 | + document.addEventListener('DOMContentLoaded', DOMContentLoaded, false) |
| 93 | +} else { |
| 94 | + DOMContentLoaded() |
| 95 | +} |
| 96 | + |
| 97 | +const nextServerDataLoadingGlobal = ((self as any).__next_s = |
| 98 | + (self as any).__next_s || []) |
| 99 | +nextServerDataLoadingGlobal.forEach(nextServerDataCallback) |
| 100 | +nextServerDataLoadingGlobal.push = nextServerDataCallback |
| 101 | + |
| 102 | +function createResponseCache() { |
| 103 | + return new Map<string, any>() |
| 104 | +} |
| 105 | +const rscCache = createResponseCache() |
| 106 | + |
| 107 | +function fetchFlight(href: string, props?: any) { |
| 108 | + const url = new URL(href, location.origin) |
| 109 | + const searchParams = url.searchParams |
| 110 | + searchParams.append('__flight__', '1') |
| 111 | + if (props) { |
| 112 | + searchParams.append('__props__', JSON.stringify(props)) |
| 113 | + } |
| 114 | + return fetch(url.toString()) |
| 115 | +} |
| 116 | + |
| 117 | +function useServerResponse(cacheKey: string, serialized?: string) { |
| 118 | + let response = rscCache.get(cacheKey) |
| 119 | + if (response) return response |
| 120 | + |
| 121 | + if (initialServerDataBuffer) { |
| 122 | + const t = new TransformStream() |
| 123 | + const writer = t.writable.getWriter() |
| 124 | + response = createFromFetch(Promise.resolve({ body: t.readable })) |
| 125 | + nextServerDataRegisterWriter(writer) |
| 126 | + } else { |
| 127 | + const fetchPromise = serialized |
| 128 | + ? (() => { |
| 129 | + const t = new TransformStream() |
| 130 | + const writer = t.writable.getWriter() |
| 131 | + writer.ready.then(() => { |
| 132 | + writer.write(new TextEncoder().encode(serialized)) |
| 133 | + }) |
| 134 | + return Promise.resolve({ body: t.readable }) |
| 135 | + })() |
| 136 | + : fetchFlight(getCacheKey()) |
| 137 | + response = createFromFetch(fetchPromise) |
| 138 | + } |
| 139 | + |
| 140 | + rscCache.set(cacheKey, response) |
| 141 | + return response |
| 142 | +} |
| 143 | + |
| 144 | +const ServerRoot = ({ |
| 145 | + cacheKey, |
| 146 | + serialized, |
| 147 | +}: { |
| 148 | + cacheKey: string |
| 149 | + serialized?: string |
| 150 | +}) => { |
| 151 | + React.useEffect(() => { |
| 152 | + rscCache.delete(cacheKey) |
| 153 | + }) |
| 154 | + const response = useServerResponse(cacheKey, serialized) |
| 155 | + const root = response.readRoot() |
| 156 | + return root |
| 157 | +} |
| 158 | + |
| 159 | +function Root({ children }: React.PropsWithChildren<{}>): React.ReactElement { |
| 160 | + if (process.env.__NEXT_TEST_MODE) { |
| 161 | + // eslint-disable-next-line react-hooks/rules-of-hooks |
| 162 | + React.useEffect(() => { |
| 163 | + window.__NEXT_HYDRATED = true |
| 164 | + |
| 165 | + if (window.__NEXT_HYDRATED_CB) { |
| 166 | + window.__NEXT_HYDRATED_CB() |
| 167 | + } |
| 168 | + }, []) |
| 169 | + } |
| 170 | + |
| 171 | + return children as React.ReactElement |
| 172 | +} |
| 173 | + |
| 174 | +const RSCComponent = (props: any) => { |
| 175 | + const cacheKey = getCacheKey() |
| 176 | + const { __flight_serialized__ } = props |
| 177 | + const [, dispatch] = useState({}) |
| 178 | + const rerender = () => dispatch({}) |
| 179 | + // If there is no cache, or there is serialized data already |
| 180 | + function refreshCache(nextProps: any) { |
| 181 | + startTransition(() => { |
| 182 | + const currentCacheKey = getCacheKey() |
| 183 | + const response = createFromFetch(fetchFlight(currentCacheKey, nextProps)) |
| 184 | + |
| 185 | + rscCache.set(currentCacheKey, response) |
| 186 | + rerender() |
| 187 | + }) |
| 188 | + } |
| 189 | + |
| 190 | + return ( |
| 191 | + <RefreshContext.Provider value={refreshCache}> |
| 192 | + <ServerRoot cacheKey={cacheKey} serialized={__flight_serialized__} /> |
| 193 | + </RefreshContext.Provider> |
| 194 | + ) |
| 195 | +} |
| 196 | + |
| 197 | +export function hydrate() { |
| 198 | + renderReactElement(appElement!, () => ( |
| 199 | + <React.StrictMode> |
| 200 | + <Root> |
| 201 | + <RSCComponent /> |
| 202 | + </Root> |
| 203 | + </React.StrictMode> |
| 204 | + )) |
| 205 | +} |
0 commit comments