Unverified Commit e933e1d2 authored by Shu Ding's avatar Shu Ding Committed by GitHub

Load `beforeInteractive` scripts properly without blocking hydration (#41164)

This PR ensures that for the app directory, `beforeInteractive`, `afterInteractive` and `lazyOnload` scripts via `next/script` are properly supported.

For both `beforeInteractive` and `afterInteractive` scripts, a preload link tag needs to be injected by Float. For `beforeInteractive` scripts and Next.js' polyfills, they need to be manually executed in order before starting the Next.js' runtime, without blocking the downloading of HTML and other scripts.

This PR doesn't include the `worker` type of scripts yet.

Note: in this PR I changed the inlined flight data `__next_s` to `__next_f`, and use `__next_s` for scripts data, because I can't find a better name for `next/script` that is also short at the same time.

## Bug

- [ ] Related issues linked using `fixes #number`
- [ ] Integration tests added
- [ ] Errors have a helpful link attached, see `contributing.md`

## Feature

- [ ] Implements an existing feature request or RFC. Make sure the feature request has been accepted for implementation before opening a PR.
- [ ] Related issues linked using `fixes #number`
- [x] Integration tests added
- [ ] Documentation added
- [ ] Telemetry added. In case of a feature if it's used or not.
- [ ] Errors have a helpful link attached, see `contributing.md`

## Documentation / Examples

- [ ] Make sure the linting passes by running `pnpm lint`
- [ ] The "examples guidelines" are followed from [our contributing doc](https://github.com/vercel/next.js/blob/canary/contributing/examples/adding-examples.md)
parent 425490c2
/**
* Before starting the Next.js runtime and requiring any module, we need to make
* sure the following scripts are executed in the correct order:
* - Polyfills
* - next/script with `beforeInteractive` strategy
*/
const version = process.env.__NEXT_VERSION
window.next = {
version,
appDir: true,
}
function loadScriptsInSequence(scripts, hydrate) {
if (!scripts || !scripts.length) {
return hydrate()
}
return scripts
.reduce((promise, [src, props]) => {
return promise.then(() => {
return new Promise((resolve, reject) => {
const el = document.createElement('script')
if (props) {
for (const key in props) {
if (key !== 'children') {
el.setAttribute(key, props[key])
}
}
}
if (src) {
el.src = src
el.onload = resolve
el.onerror = reject
} else if (props) {
el.innerHTML = props.children
setTimeout(resolve)
}
document.head.appendChild(el)
})
})
}, Promise.resolve())
.then(() => {
hydrate()
})
.catch((err) => {
console.error(err)
// Still try to hydrate even if there's an error.
hydrate()
})
}
export function appBootstrap(callback) {
loadScriptsInSequence(self.__next_s, () => {
callback()
})
}
...@@ -7,6 +7,7 @@ import React, { experimental_use as use } from 'react' ...@@ -7,6 +7,7 @@ import React, { experimental_use as use } from 'react'
import { createFromReadableStream } from 'next/dist/compiled/react-server-dom-webpack' import { createFromReadableStream } from 'next/dist/compiled/react-server-dom-webpack'
import measureWebVitals from './performance-relayer' import measureWebVitals from './performance-relayer'
import { HeadManagerContext } from '../shared/lib/head-manager-context'
/// <reference types="react-dom/experimental" /> /// <reference types="react-dom/experimental" />
...@@ -42,8 +43,6 @@ self.__next_require__ = __webpack_require__ ...@@ -42,8 +43,6 @@ self.__next_require__ = __webpack_require__
return __webpack_chunk_load__(chunkId) return __webpack_chunk_load__(chunkId)
} }
export const version = process.env.__NEXT_VERSION
const appElement: HTMLElement | Document | null = document const appElement: HTMLElement | Document | null = document
const getCacheKey = () => { const getCacheKey = () => {
...@@ -115,8 +114,8 @@ if (document.readyState === 'loading') { ...@@ -115,8 +114,8 @@ if (document.readyState === 'loading') {
DOMContentLoaded() DOMContentLoaded()
} }
const nextServerDataLoadingGlobal = ((self as any).__next_s = const nextServerDataLoadingGlobal = ((self as any).__next_f =
(self as any).__next_s || []) (self as any).__next_f || [])
nextServerDataLoadingGlobal.forEach(nextServerDataCallback) nextServerDataLoadingGlobal.forEach(nextServerDataCallback)
nextServerDataLoadingGlobal.push = nextServerDataCallback nextServerDataLoadingGlobal.push = nextServerDataCallback
...@@ -177,9 +176,15 @@ function RSCComponent(props: any): JSX.Element { ...@@ -177,9 +176,15 @@ function RSCComponent(props: any): JSX.Element {
export function hydrate() { export function hydrate() {
const reactEl = ( const reactEl = (
<React.StrictMode> <React.StrictMode>
<Root> <HeadManagerContext.Provider
<RSCComponent /> value={{
</Root> appDir: true,
}}
>
<Root>
<RSCComponent />
</Root>
</HeadManagerContext.Provider>
</React.StrictMode> </React.StrictMode>
) )
......
import { hydrate, version } from './app-index'
// TODO-APP: hydration warning // TODO-APP: hydration warning
window.next = { import { appBootstrap } from './app-bootstrap'
version,
appDir: true,
}
hydrate() appBootstrap(() => {
const { hydrate } = require('./app-index')
hydrate()
})
// TODO-APP: build indicator // TODO-APP: build indicator
import { hydrate, version } from './app-index' import { appBootstrap } from './app-bootstrap'
// Include app-router and layout-router in the main chunk appBootstrap(() => {
import 'next/dist/client/components/app-router.client.js' // Include app-router and layout-router in the main chunk
import 'next/dist/client/components/layout-router.client.js' import('next/dist/client/components/app-router.client.js')
import('next/dist/client/components/layout-router.client.js')
window.next = { const { hydrate } = require('./app-index')
version, hydrate()
appDir: true, })
}
hydrate()
'client' 'client'
import ReactDOM from 'react-dom'
import React, { useEffect, useContext, useRef } from 'react' import React, { useEffect, useContext, useRef } from 'react'
import { ScriptHTMLAttributes } from 'react' import { ScriptHTMLAttributes } from 'react'
import { HeadManagerContext } from '../shared/lib/head-manager-context' import { HeadManagerContext } from '../shared/lib/head-manager-context'
...@@ -177,7 +178,8 @@ function Script(props: ScriptProps): JSX.Element | null { ...@@ -177,7 +178,8 @@ function Script(props: ScriptProps): JSX.Element | null {
} = props } = props
// Context is available only during SSR // Context is available only during SSR
const { updateScripts, scripts, getIsSsr } = useContext(HeadManagerContext) const { updateScripts, scripts, getIsSsr, appDir, nonce } =
useContext(HeadManagerContext)
/** /**
* - First mount: * - First mount:
...@@ -254,6 +256,61 @@ function Script(props: ScriptProps): JSX.Element | null { ...@@ -254,6 +256,61 @@ function Script(props: ScriptProps): JSX.Element | null {
} }
} }
// For the app directory, we need React Float to preload these scripts.
if (appDir) {
// Before interactive scripts need to be loaded by Next.js' runtime instead
// of native <script> tags, becasue they no longer have `defer`.
if (strategy === 'beforeInteractive') {
if (!src) {
// For inlined scripts, we put the content in `children`.
if (restProps.dangerouslySetInnerHTML) {
restProps.children = restProps.dangerouslySetInnerHTML.__html
delete restProps.dangerouslySetInnerHTML
}
return (
<script
nonce={nonce}
dangerouslySetInnerHTML={{
__html: `(self.__next_s=self.__next_s||[]).push(${JSON.stringify([
0,
{ ...restProps },
])})`,
}}
/>
)
}
// @ts-ignore
ReactDOM.preload(
src,
restProps.integrity
? { as: 'script', integrity: restProps.integrity }
: { as: 'script' }
)
return (
<script
nonce={nonce}
dangerouslySetInnerHTML={{
__html: `(self.__next_s=self.__next_s||[]).push(${JSON.stringify([
src,
])})`,
}}
/>
)
} else if (strategy === 'afterInteractive') {
if (src) {
// @ts-ignore
ReactDOM.preload(
src,
restProps.integrity
? { as: 'script', integrity: restProps.integrity }
: { as: 'script' }
)
}
}
}
return null return null
} }
......
...@@ -34,6 +34,7 @@ import { REDIRECT_ERROR_CODE } from '../client/components/redirect' ...@@ -34,6 +34,7 @@ import { REDIRECT_ERROR_CODE } from '../client/components/redirect'
import { NextCookies } from './web/spec-extension/cookies' import { NextCookies } from './web/spec-extension/cookies'
import { DYNAMIC_ERROR_CODE } from '../client/components/hooks-server-context' import { DYNAMIC_ERROR_CODE } from '../client/components/hooks-server-context'
import { NOT_FOUND_ERROR_CODE } from '../client/components/not-found' import { NOT_FOUND_ERROR_CODE } from '../client/components/not-found'
import { HeadManagerContext } from '../shared/lib/head-manager-context'
import { Writable } from 'stream' import { Writable } from 'stream'
const INTERNAL_HEADERS_INSTANCE = Symbol('internal for headers readonly') const INTERNAL_HEADERS_INSTANCE = Symbol('internal for headers readonly')
...@@ -287,7 +288,7 @@ function useFlightResponse( ...@@ -287,7 +288,7 @@ function useFlightResponse(
bootstrapped = true bootstrapped = true
writer.write( writer.write(
encodeText( encodeText(
`${startScriptTag}(self.__next_s=self.__next_s||[]).push(${htmlEscapeJsonString( `${startScriptTag}(self.__next_f=self.__next_f||[]).push(${htmlEscapeJsonString(
JSON.stringify([0]) JSON.stringify([0])
)})</script>` )})</script>`
) )
...@@ -298,7 +299,7 @@ function useFlightResponse( ...@@ -298,7 +299,7 @@ function useFlightResponse(
writer.close() writer.close()
} else { } else {
const responsePartial = decodeText(value) const responsePartial = decodeText(value)
const scripts = `${startScriptTag}(self.__next_s=self.__next_s||[]).push(${htmlEscapeJsonString( const scripts = `${startScriptTag}self.__next_f.push(${htmlEscapeJsonString(
JSON.stringify([1, responsePartial]) JSON.stringify([1, responsePartial])
)})</script>` )})</script>`
...@@ -1396,40 +1397,69 @@ export async function renderToHTMLOrFlight( ...@@ -1396,40 +1397,69 @@ export async function renderToHTMLOrFlight(
) )
return ( return (
<ServerInsertedHTMLContext.Provider value={addInsertedHtml}> <HeadManagerContext.Provider
{children} value={{
</ServerInsertedHTMLContext.Provider> appDir: true,
nonce,
}}
>
<ServerInsertedHTMLContext.Provider value={addInsertedHtml}>
{children}
</ServerInsertedHTMLContext.Provider>
</HeadManagerContext.Provider>
) )
} }
const bodyResult = async () => { const bodyResult = async () => {
const polyfills = buildManifest.polyfillFiles
.filter(
(polyfill) =>
polyfill.endsWith('.js') && !polyfill.endsWith('.module.js')
)
.map((polyfill) => ({
src: `${renderOpts.assetPrefix || ''}/_next/${polyfill}`,
integrity: subresourceIntegrityManifest?.[polyfill],
}))
const content = ( const content = (
<InsertedHTML> <InsertedHTML>
<ServerComponentsRenderer /> <ServerComponentsRenderer />
</InsertedHTML> </InsertedHTML>
) )
let polyfillsFlushed = false
const getServerInsertedHTML = (): Promise<string> => { const getServerInsertedHTML = (): Promise<string> => {
const flushed = renderToString( const flushed = renderToString(
<> <>
{Array.from(serverInsertedHTMLCallbacks).map((callback) => {Array.from(serverInsertedHTMLCallbacks).map((callback) =>
callback() callback()
)} )}
{polyfillsFlushed
? null
: polyfills?.map((polyfill) => {
return (
<script
key={polyfill.src}
noModule={true}
nonce={nonce}
dangerouslySetInnerHTML={{
__html: `(self.__next_s=self.__next_s||[]).push([${JSON.stringify(
polyfill.src
)},${
polyfill.integrity
? JSON.stringify({ integrity: polyfill.integrity })
: '{}'
}])`,
}}
/>
)
})}
</> </>
) )
polyfillsFlushed = true
return flushed return flushed
} }
const polyfills = buildManifest.polyfillFiles
.filter(
(polyfill) =>
polyfill.endsWith('.js') && !polyfill.endsWith('.module.js')
)
.map((polyfill) => ({
src: `${renderOpts.assetPrefix || ''}/_next/${polyfill}`,
integrity: subresourceIntegrityManifest?.[polyfill],
}))
try { try {
const renderStream = await renderToInitialStream({ const renderStream = await renderToInitialStream({
ReactDOMServer, ReactDOMServer,
...@@ -1456,7 +1486,6 @@ export async function renderToHTMLOrFlight( ...@@ -1456,7 +1486,6 @@ export async function renderToHTMLOrFlight(
generateStaticHTML: isStaticGeneration, generateStaticHTML: isStaticGeneration,
getServerInsertedHTML, getServerInsertedHTML,
serverInsertedHTMLToHead: true, serverInsertedHTMLToHead: true,
polyfills,
dev, dev,
}) })
} catch (err: any) { } catch (err: any) {
...@@ -1488,7 +1517,6 @@ export async function renderToHTMLOrFlight( ...@@ -1488,7 +1517,6 @@ export async function renderToHTMLOrFlight(
generateStaticHTML: isStaticGeneration, generateStaticHTML: isStaticGeneration,
getServerInsertedHTML, getServerInsertedHTML,
serverInsertedHTMLToHead: true, serverInsertedHTMLToHead: true,
polyfills,
dev, dev,
}) })
} }
......
...@@ -306,7 +306,6 @@ export async function continueFromInitialStream( ...@@ -306,7 +306,6 @@ export async function continueFromInitialStream(
generateStaticHTML, generateStaticHTML,
getServerInsertedHTML, getServerInsertedHTML,
serverInsertedHTMLToHead, serverInsertedHTMLToHead,
polyfills,
}: { }: {
dev?: boolean dev?: boolean
suffix?: string suffix?: string
...@@ -314,7 +313,6 @@ export async function continueFromInitialStream( ...@@ -314,7 +313,6 @@ export async function continueFromInitialStream(
generateStaticHTML: boolean generateStaticHTML: boolean
getServerInsertedHTML?: () => Promise<string> getServerInsertedHTML?: () => Promise<string>
serverInsertedHTMLToHead: boolean serverInsertedHTMLToHead: boolean
polyfills?: { src: string; integrity: string | undefined }[]
} }
): Promise<ReadableStream<Uint8Array>> { ): Promise<ReadableStream<Uint8Array>> {
const closeTag = '</body></html>' const closeTag = '</body></html>'
...@@ -333,26 +331,13 @@ export async function continueFromInitialStream( ...@@ -333,26 +331,13 @@ export async function continueFromInitialStream(
dataStream ? createInlineDataStream(dataStream) : null, dataStream ? createInlineDataStream(dataStream) : null,
suffixUnclosed != null ? createSuffixStream(closeTag) : null, suffixUnclosed != null ? createSuffixStream(closeTag) : null,
createHeadInjectionTransformStream(async () => { createHeadInjectionTransformStream(async () => {
// Inject polyfills for browsers that don't support modules. It has to be
// blocking here and can't be `defer` because other scripts have `async`.
const polyfillScripts = polyfills
? polyfills
.map(
({ src, integrity }) =>
`<script src="${src}" nomodule=""${
integrity ? ` integrity="${integrity}"` : ''
}></script>`
)
.join('')
: ''
// TODO-APP: Insert server side html to end of head in app layout rendering, to avoid // TODO-APP: Insert server side html to end of head in app layout rendering, to avoid
// hydration errors. Remove this once it's ready to be handled by react itself. // hydration errors. Remove this once it's ready to be handled by react itself.
const serverInsertedHTML = const serverInsertedHTML =
getServerInsertedHTML && serverInsertedHTMLToHead getServerInsertedHTML && serverInsertedHTMLToHead
? await getServerInsertedHTML() ? await getServerInsertedHTML()
: '' : ''
return polyfillScripts + serverInsertedHTML return serverInsertedHTML
}), }),
dev ? createRootLayoutValidatorStream() : null, dev ? createRootLayoutValidatorStream() : null,
].filter(nonNullable) ].filter(nonNullable)
......
...@@ -6,6 +6,10 @@ export const HeadManagerContext: React.Context<{ ...@@ -6,6 +6,10 @@ export const HeadManagerContext: React.Context<{
updateScripts?: (state: any) => void updateScripts?: (state: any) => void
scripts?: any scripts?: any
getIsSsr?: () => boolean getIsSsr?: () => boolean
// Used in app directory, to render script tags as server components.
appDir?: boolean
nonce?: string
}> = React.createContext({}) }> = React.createContext({})
if (process.env.NODE_ENV !== 'production') { if (process.env.NODE_ENV !== 'production') {
......
'client'
export default function Client() {
if (typeof window !== 'undefined') {
window._script_order = window._script_order || []
if (window._script_order[window._script_order.length - 1] !== 'render') {
window._script_order.push('render')
}
console.log(window._script_order)
}
return null
}
import Script from 'next/script'
import Client from './client'
export default function Page() {
return (
<div>
<h2>next/script</h2>
<Client />
<Script strategy="lazyOnload" src="/test4.js" />
<Script strategy="afterInteractive" src="/test3.js" />
<Script strategy="beforeInteractive" src="/test1.js" />
<Script strategy="beforeInteractive" id="1.5">{`
;(window._script_order = window._script_order || []).push(1.5)
console.log(window._script_order)
`}</Script>
<Script strategy="beforeInteractive" src="/test2.js" />
<Script
strategy="beforeInteractive"
id="2.5"
dangerouslySetInnerHTML={{
__html: `
;(window._script_order = window._script_order || []).push(2.5)
console.log(window._script_order)
`,
}}
/>
</div>
)
}
;(window._script_order = window._script_order || []).push(1)
console.log(window._script_order)
;(window._script_order = window._script_order || []).push(2)
console.log(window._script_order)
;(window._script_order = window._script_order || []).push(3)
console.log(window._script_order)
;(window._script_order = window._script_order || []).push(4)
console.log(window._script_order)
...@@ -114,9 +114,7 @@ describe('app dir', () => { ...@@ -114,9 +114,7 @@ describe('app dir', () => {
it('should serve polyfills for browsers that do not support modules', async () => { it('should serve polyfills for browsers that do not support modules', async () => {
const html = await renderViaHTTP(next.url, '/dashboard/index') const html = await renderViaHTTP(next.url, '/dashboard/index')
expect(html).toMatch( expect(html).toMatch(/\/_next\/static\/chunks\/polyfills(-.+)?\.js/)
/<script src="\/_next\/static\/chunks\/polyfills(-\w+)?\.js" nomodule="">/
)
}) })
// TODO-APP: handle css modules fouc in dev // TODO-APP: handle css modules fouc in dev
...@@ -1717,6 +1715,43 @@ describe('app dir', () => { ...@@ -1717,6 +1715,43 @@ describe('app dir', () => {
} }
}) })
}) })
describe('next/script', () => {
it('should support next/script and render in correct order', async () => {
const browser = await webdriver(next.url, '/script')
// Wait for lazyOnload scripts to be ready.
await new Promise((resolve) => setTimeout(resolve, 1000))
expect(await browser.eval(`window._script_order`)).toStrictEqual([
1,
1.5,
2,
2.5,
'render',
3,
4,
])
})
it('should insert preload tags for beforeInteractive and afterInteractive scripts', async () => {
const html = await renderViaHTTP(next.url, '/script')
expect(html).toContain(
'<link href="/test1.js" rel="preload" as="script"/>'
)
expect(html).toContain(
'<link href="/test2.js" rel="preload" as="script"/>'
)
expect(html).toContain(
'<link href="/test3.js" rel="preload" as="script"/>'
)
// test4.js has lazyOnload which doesn't need to be preloaded
expect(html).not.toContain(
'<script src="/test4.js" rel="preload" as="script"/>'
)
})
})
} }
runTests() runTests()
......
...@@ -347,7 +347,7 @@ describe('app dir - rsc basics', () => { ...@@ -347,7 +347,7 @@ describe('app dir - rsc basics', () => {
let gotInlinedData = false let gotInlinedData = false
await resolveStreamResponse(response, (_, result) => { await resolveStreamResponse(response, (_, result) => {
gotInlinedData = result.includes('self.__next_s=') gotInlinedData = result.includes('self.__next_f=')
gotData = result.includes('next_streaming_data') gotData = result.includes('next_streaming_data')
if (!gotFallback) { if (!gotFallback) {
gotFallback = result.includes('next_streaming_fallback') gotFallback = result.includes('next_streaming_fallback')
......
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