Unverified Commit 18fe1eb4 authored by Tobias Koppers's avatar Tobias Koppers Committed by GitHub

Turbopack: inject chunk group list file into the page for server component CSS (#57376)

### What?

incjects the script tag for CSS reloading

### Why?

### How?


Closes WEB-1851

---------
Co-authored-by: 's avatarWill Binns-Smith <wbinnssmith@gmail.com>
parent 59ebfbea
...@@ -74,6 +74,11 @@ impl ClientReferenceManifest { ...@@ -74,6 +74,11 @@ impl ClientReferenceManifest {
.entry(server_component_name.clone_value()) .entry(server_component_name.clone_value())
.or_default(); .or_default();
let entry_js_files = entry_manifest
.entry_js_files
.entry(server_component_name.clone_value())
.or_default();
match app_client_reference_ty { match app_client_reference_ty {
ClientReferenceType::CssClientReference(_) => { ClientReferenceType::CssClientReference(_) => {
entry_css_files.extend( entry_css_files.extend(
...@@ -88,9 +93,22 @@ impl ClientReferenceManifest { ...@@ -88,9 +93,22 @@ impl ClientReferenceManifest {
}) })
.map(ToString::to_string), .map(ToString::to_string),
); );
entry_js_files.extend(
client_chunks_paths
.iter()
.filter_map(|chunk_path| {
if chunk_path.extension_ref() != Some("css") {
client_relative_path.get_path_to(chunk_path)
} else {
None
}
})
.map(ToString::to_string),
);
} }
ClientReferenceType::EcmascriptClientReference(_) => { ClientReferenceType::EcmascriptClientReference(_) => {
// TODO should this be removed? does it make sense?
entry_css_files.extend( entry_css_files.extend(
client_chunks_paths client_chunks_paths
.iter() .iter()
......
...@@ -206,6 +206,9 @@ pub struct ClientReferenceManifest { ...@@ -206,6 +206,9 @@ pub struct ClientReferenceManifest {
/// Mapping of server component path to required CSS client chunks. /// Mapping of server component path to required CSS client chunks.
#[serde(rename = "entryCSSFiles")] #[serde(rename = "entryCSSFiles")]
pub entry_css_files: HashMap<String, Vec<String>>, pub entry_css_files: HashMap<String, Vec<String>>,
/// Mapping of server component path to required JS client chunks.
#[serde(rename = "entryJSFiles")]
pub entry_js_files: HashMap<String, Vec<String>>,
} }
#[derive(Serialize, Default, Debug)] #[derive(Serialize, Default, Debug)]
......
...@@ -76,6 +76,9 @@ export type ClientReferenceManifest = { ...@@ -76,6 +76,9 @@ export type ClientReferenceManifest = {
entryCSSFiles: { entryCSSFiles: {
[entry: string]: string[] [entry: string]: string[]
} }
entryJSFiles?: {
[entry: string]: string[]
}
} }
function getAppPathRequiredChunks( function getAppPathRequiredChunks(
......
...@@ -32,6 +32,7 @@ export interface ErrorBoundaryProps { ...@@ -32,6 +32,7 @@ export interface ErrorBoundaryProps {
children?: React.ReactNode children?: React.ReactNode
errorComponent: ErrorComponent errorComponent: ErrorComponent
errorStyles?: React.ReactNode | undefined errorStyles?: React.ReactNode | undefined
errorScripts?: React.ReactNode | undefined
} }
interface ErrorBoundaryHandlerProps extends ErrorBoundaryProps { interface ErrorBoundaryHandlerProps extends ErrorBoundaryProps {
...@@ -106,6 +107,7 @@ export class ErrorBoundaryHandler extends React.Component< ...@@ -106,6 +107,7 @@ export class ErrorBoundaryHandler extends React.Component<
<> <>
<HandleISRError error={this.state.error} /> <HandleISRError error={this.state.error} />
{this.props.errorStyles} {this.props.errorStyles}
{this.props.errorScripts}
<this.props.errorComponent <this.props.errorComponent
error={this.state.error} error={this.state.error}
reset={this.reset} reset={this.reset}
...@@ -158,6 +160,7 @@ export default GlobalError ...@@ -158,6 +160,7 @@ export default GlobalError
export function ErrorBoundary({ export function ErrorBoundary({
errorComponent, errorComponent,
errorStyles, errorStyles,
errorScripts,
children, children,
}: ErrorBoundaryProps & { children: React.ReactNode }): JSX.Element { }: ErrorBoundaryProps & { children: React.ReactNode }): JSX.Element {
const pathname = usePathname() const pathname = usePathname()
...@@ -167,6 +170,7 @@ export function ErrorBoundary({ ...@@ -167,6 +170,7 @@ export function ErrorBoundary({
pathname={pathname} pathname={pathname}
errorComponent={errorComponent} errorComponent={errorComponent}
errorStyles={errorStyles} errorStyles={errorStyles}
errorScripts={errorScripts}
> >
{children} {children}
</ErrorBoundaryHandler> </ErrorBoundaryHandler>
......
...@@ -466,11 +466,13 @@ function LoadingBoundary({ ...@@ -466,11 +466,13 @@ function LoadingBoundary({
children, children,
loading, loading,
loadingStyles, loadingStyles,
loadingScripts,
hasLoading, hasLoading,
}: { }: {
children: React.ReactNode children: React.ReactNode
loading?: React.ReactNode loading?: React.ReactNode
loadingStyles?: React.ReactNode loadingStyles?: React.ReactNode
loadingScripts?: React.ReactNode
hasLoading: boolean hasLoading: boolean
}): JSX.Element { }): JSX.Element {
if (hasLoading) { if (hasLoading) {
...@@ -479,6 +481,7 @@ function LoadingBoundary({ ...@@ -479,6 +481,7 @@ function LoadingBoundary({
fallback={ fallback={
<> <>
{loadingStyles} {loadingStyles}
{loadingScripts}
{loading} {loading}
</> </>
} }
...@@ -501,9 +504,12 @@ export default function OuterLayoutRouter({ ...@@ -501,9 +504,12 @@ export default function OuterLayoutRouter({
childProp, childProp,
error, error,
errorStyles, errorStyles,
errorScripts,
templateStyles, templateStyles,
templateScripts,
loading, loading,
loadingStyles, loadingStyles,
loadingScripts,
hasLoading, hasLoading,
template, template,
notFound, notFound,
...@@ -515,10 +521,13 @@ export default function OuterLayoutRouter({ ...@@ -515,10 +521,13 @@ export default function OuterLayoutRouter({
childProp: ChildProp childProp: ChildProp
error: ErrorComponent error: ErrorComponent
errorStyles: React.ReactNode | undefined errorStyles: React.ReactNode | undefined
errorScripts: React.ReactNode | undefined
templateStyles: React.ReactNode | undefined templateStyles: React.ReactNode | undefined
templateScripts: React.ReactNode | undefined
template: React.ReactNode template: React.ReactNode
loading: React.ReactNode | undefined loading: React.ReactNode | undefined
loadingStyles: React.ReactNode | undefined loadingStyles: React.ReactNode | undefined
loadingScripts: React.ReactNode | undefined
hasLoading: boolean hasLoading: boolean
notFound: React.ReactNode | undefined notFound: React.ReactNode | undefined
notFoundStyles: React.ReactNode | undefined notFoundStyles: React.ReactNode | undefined
...@@ -580,11 +589,16 @@ export default function OuterLayoutRouter({ ...@@ -580,11 +589,16 @@ export default function OuterLayoutRouter({
key={createRouterCacheKey(preservedSegment, true)} key={createRouterCacheKey(preservedSegment, true)}
value={ value={
<ScrollAndFocusHandler segmentPath={segmentPath}> <ScrollAndFocusHandler segmentPath={segmentPath}>
<ErrorBoundary errorComponent={error} errorStyles={errorStyles}> <ErrorBoundary
errorComponent={error}
errorStyles={errorStyles}
errorScripts={errorScripts}
>
<LoadingBoundary <LoadingBoundary
hasLoading={hasLoading} hasLoading={hasLoading}
loading={loading} loading={loading}
loadingStyles={loadingStyles} loadingStyles={loadingStyles}
loadingScripts={loadingScripts}
> >
<NotFoundBoundary <NotFoundBoundary
notFound={notFound} notFound={notFound}
...@@ -611,6 +625,7 @@ export default function OuterLayoutRouter({ ...@@ -611,6 +625,7 @@ export default function OuterLayoutRouter({
} }
> >
{templateStyles} {templateStyles}
{templateScripts}
{template} {template}
</TemplateContext.Provider> </TemplateContext.Provider>
) )
......
...@@ -273,6 +273,7 @@ async function generateFlight( ...@@ -273,6 +273,7 @@ async function generateFlight(
<MetadataTree key={requestId} /> <MetadataTree key={requestId} />
), ),
injectedCSS: new Set(), injectedCSS: new Set(),
injectedJS: new Set(),
injectedFontPreloadTags: new Set(), injectedFontPreloadTags: new Set(),
rootLayoutIncluded: false, rootLayoutIncluded: false,
asNotFound: ctx.isNotFoundPath || options?.asNotFound, asNotFound: ctx.isNotFoundPath || options?.asNotFound,
...@@ -318,6 +319,7 @@ function createServerComponentsRenderer( ...@@ -318,6 +319,7 @@ function createServerComponentsRenderer(
preinitScripts() preinitScripts()
// Create full component tree from root to leaf. // Create full component tree from root to leaf.
const injectedCSS = new Set<string>() const injectedCSS = new Set<string>()
const injectedJS = new Set<string>()
const injectedFontPreloadTags = new Set<string>() const injectedFontPreloadTags = new Set<string>()
const { const {
getDynamicParamFromSegment, getDynamicParamFromSegment,
...@@ -349,6 +351,7 @@ function createServerComponentsRenderer( ...@@ -349,6 +351,7 @@ function createServerComponentsRenderer(
parentParams: {}, parentParams: {},
firstItem: true, firstItem: true,
injectedCSS, injectedCSS,
injectedJS,
injectedFontPreloadTags, injectedFontPreloadTags,
rootLayoutIncluded: false, rootLayoutIncluded: false,
asNotFound: props.asNotFound, asNotFound: props.asNotFound,
......
import React from 'react' import React from 'react'
import { interopDefault } from './interop-default' import { interopDefault } from './interop-default'
import { getCssInlinedLinkTags } from './get-css-inlined-link-tags' import { getLinkAndScriptTags } from './get-css-inlined-link-tags'
import type { AppRenderContext } from './app-render' import type { AppRenderContext } from './app-render'
import { getAssetQueryString } from './get-asset-query-string' import { getAssetQueryString } from './get-asset-query-string'
export async function createComponentAndStyles({ export async function createComponentStylesAndScripts({
filePath, filePath,
getComponent, getComponent,
injectedCSS, injectedCSS,
injectedJS,
ctx, ctx,
}: { }: {
filePath: string filePath: string
getComponent: () => any getComponent: () => any
injectedCSS: Set<string> injectedCSS: Set<string>
injectedJS: Set<string>
ctx: AppRenderContext ctx: AppRenderContext
}): Promise<any> { }): Promise<[any, React.ReactNode, React.ReactNode]> {
const cssHrefs = getCssInlinedLinkTags( const { styles: cssHrefs, scripts: jsHrefs } = getLinkAndScriptTags(
ctx.clientReferenceManifest, ctx.clientReferenceManifest,
filePath, filePath,
injectedCSS injectedCSS,
injectedJS
) )
const styles = cssHrefs const styles = cssHrefs
...@@ -56,7 +59,13 @@ export async function createComponentAndStyles({ ...@@ -56,7 +59,13 @@ export async function createComponentAndStyles({
}) })
: null : null
const scripts = jsHrefs
? jsHrefs.map((href) => (
<script src={`${ctx.assetPrefix}/_next/${href}`} async={true} />
))
: null
const Comp = interopDefault(await getComponent()) const Comp = interopDefault(await getComponent())
return [Comp, styles] return [Comp, styles, scripts]
} }
...@@ -8,7 +8,7 @@ import { preloadComponent } from './preload-component' ...@@ -8,7 +8,7 @@ import { preloadComponent } from './preload-component'
import { addSearchParamsIfPageSegment } from './create-flight-router-state-from-loader-tree' import { addSearchParamsIfPageSegment } from './create-flight-router-state-from-loader-tree'
import { parseLoaderTree } from './parse-loader-tree' import { parseLoaderTree } from './parse-loader-tree'
import type { CreateSegmentPath, AppRenderContext } from './app-render' import type { CreateSegmentPath, AppRenderContext } from './app-render'
import { createComponentAndStyles } from './create-component-and-styles' import { createComponentStylesAndScripts } from './create-component-styles-and-scripts'
import { getLayerAssets } from './get-layer-assets' import { getLayerAssets } from './get-layer-assets'
import { hasLoadingComponentInTree } from './has-loading-component-in-tree' import { hasLoadingComponentInTree } from './has-loading-component-in-tree'
...@@ -23,6 +23,7 @@ export async function createComponentTree({ ...@@ -23,6 +23,7 @@ export async function createComponentTree({
firstItem, firstItem,
rootLayoutIncluded, rootLayoutIncluded,
injectedCSS, injectedCSS,
injectedJS,
injectedFontPreloadTags, injectedFontPreloadTags,
asNotFound, asNotFound,
metadataOutlet, metadataOutlet,
...@@ -34,6 +35,7 @@ export async function createComponentTree({ ...@@ -34,6 +35,7 @@ export async function createComponentTree({
rootLayoutIncluded: boolean rootLayoutIncluded: boolean
firstItem?: boolean firstItem?: boolean
injectedCSS: Set<string> injectedCSS: Set<string>
injectedJS: Set<string>
injectedFontPreloadTags: Set<string> injectedFontPreloadTags: Set<string>
asNotFound?: boolean asNotFound?: boolean
metadataOutlet?: React.ReactNode metadataOutlet?: React.ReactNode
...@@ -66,41 +68,46 @@ export async function createComponentTree({ ...@@ -66,41 +68,46 @@ export async function createComponentTree({
const { layout, template, error, loading, 'not-found': notFound } = components const { layout, template, error, loading, 'not-found': notFound } = components
const injectedCSSWithCurrentLayout = new Set(injectedCSS) const injectedCSSWithCurrentLayout = new Set(injectedCSS)
const injectedJSWithCurrentLayout = new Set(injectedJS)
const injectedFontPreloadTagsWithCurrentLayout = new Set( const injectedFontPreloadTagsWithCurrentLayout = new Set(
injectedFontPreloadTags injectedFontPreloadTags
) )
const styles = getLayerAssets({ const layerAssets = getLayerAssets({
ctx, ctx,
layoutOrPagePath, layoutOrPagePath,
injectedCSS: injectedCSSWithCurrentLayout, injectedCSS: injectedCSSWithCurrentLayout,
injectedJS: injectedJSWithCurrentLayout,
injectedFontPreloadTags: injectedFontPreloadTagsWithCurrentLayout, injectedFontPreloadTags: injectedFontPreloadTagsWithCurrentLayout,
}) })
const [Template, templateStyles] = template const [Template, templateStyles, templateScripts] = template
? await createComponentAndStyles({ ? await createComponentStylesAndScripts({
ctx, ctx,
filePath: template[1], filePath: template[1],
getComponent: template[0], getComponent: template[0],
injectedCSS: injectedCSSWithCurrentLayout, injectedCSS: injectedCSSWithCurrentLayout,
injectedJS: injectedJSWithCurrentLayout,
}) })
: [React.Fragment] : [React.Fragment]
const [ErrorComponent, errorStyles] = error const [ErrorComponent, errorStyles, errorScripts] = error
? await createComponentAndStyles({ ? await createComponentStylesAndScripts({
ctx, ctx,
filePath: error[1], filePath: error[1],
getComponent: error[0], getComponent: error[0],
injectedCSS: injectedCSSWithCurrentLayout, injectedCSS: injectedCSSWithCurrentLayout,
injectedJS: injectedJSWithCurrentLayout,
}) })
: [] : []
const [Loading, loadingStyles] = loading const [Loading, loadingStyles, loadingScripts] = loading
? await createComponentAndStyles({ ? await createComponentStylesAndScripts({
ctx, ctx,
filePath: loading[1], filePath: loading[1],
getComponent: loading[0], getComponent: loading[0],
injectedCSS: injectedCSSWithCurrentLayout, injectedCSS: injectedCSSWithCurrentLayout,
injectedJS: injectedJSWithCurrentLayout,
}) })
: [] : []
...@@ -119,11 +126,12 @@ export async function createComponentTree({ ...@@ -119,11 +126,12 @@ export async function createComponentTree({
rootLayoutIncluded || rootLayoutAtThisLevel rootLayoutIncluded || rootLayoutAtThisLevel
const [NotFound, notFoundStyles] = notFound const [NotFound, notFoundStyles] = notFound
? await createComponentAndStyles({ ? await createComponentStylesAndScripts({
ctx, ctx,
filePath: notFound[1], filePath: notFound[1],
getComponent: notFound[0], getComponent: notFound[0],
injectedCSS: injectedCSSWithCurrentLayout, injectedCSS: injectedCSSWithCurrentLayout,
injectedJS: injectedJSWithCurrentLayout,
}) })
: [] : []
...@@ -210,7 +218,7 @@ export async function createComponentTree({ ...@@ -210,7 +218,7 @@ export async function createComponentTree({
<NotFoundBoundary <NotFoundBoundary
notFound={ notFound={
<> <>
{styles} {layerAssets}
<RootLayoutComponent> <RootLayoutComponent>
{notFoundStyles} {notFoundStyles}
<NotFoundComponent /> <NotFoundComponent />
...@@ -302,16 +310,19 @@ export async function createComponentTree({ ...@@ -302,16 +310,19 @@ export async function createComponentTree({
segmentPath={createSegmentPath(currentSegmentPath)} segmentPath={createSegmentPath(currentSegmentPath)}
loading={Loading ? <Loading /> : undefined} loading={Loading ? <Loading /> : undefined}
loadingStyles={loadingStyles} loadingStyles={loadingStyles}
loadingScripts={loadingScripts}
// TODO-APP: Add test for loading returning `undefined`. This currently can't be tested as the `webdriver()` tab will wait for the full page to load before returning. // TODO-APP: Add test for loading returning `undefined`. This currently can't be tested as the `webdriver()` tab will wait for the full page to load before returning.
hasLoading={Boolean(Loading)} hasLoading={Boolean(Loading)}
error={ErrorComponent} error={ErrorComponent}
errorStyles={errorStyles} errorStyles={errorStyles}
errorScripts={errorScripts}
template={ template={
<Template> <Template>
<RenderFromTemplateContext /> <RenderFromTemplateContext />
</Template> </Template>
} }
templateStyles={templateStyles} templateStyles={templateStyles}
templateScripts={templateScripts}
notFound={notFoundComponent} notFound={notFoundComponent}
notFoundStyles={notFoundStyles} notFoundStyles={notFoundStyles}
childProp={currentChildProp} childProp={currentChildProp}
...@@ -345,6 +356,7 @@ export async function createComponentTree({ ...@@ -345,6 +356,7 @@ export async function createComponentTree({
parentParams: currentParams, parentParams: currentParams,
rootLayoutIncluded: rootLayoutIncludedAtThisLevelOrAbove, rootLayoutIncluded: rootLayoutIncludedAtThisLevelOrAbove,
injectedCSS: injectedCSSWithCurrentLayout, injectedCSS: injectedCSSWithCurrentLayout,
injectedJS: injectedJSWithCurrentLayout,
injectedFontPreloadTags: injectedFontPreloadTagsWithCurrentLayout, injectedFontPreloadTags: injectedFontPreloadTagsWithCurrentLayout,
asNotFound, asNotFound,
metadataOutlet, metadataOutlet,
...@@ -378,7 +390,7 @@ export async function createComponentTree({ ...@@ -378,7 +390,7 @@ export async function createComponentTree({
if (!Component) { if (!Component) {
return { return {
Component: () => <>{parallelRouteComponents.children}</>, Component: () => <>{parallelRouteComponents.children}</>,
styles, styles: layerAssets,
} }
} }
...@@ -461,6 +473,6 @@ export async function createComponentTree({ ...@@ -461,6 +473,6 @@ export async function createComponentTree({
</> </>
) )
}, },
styles, styles: layerAssets,
} }
} }
...@@ -3,28 +3,43 @@ import type { ClientReferenceManifest } from '../../build/webpack/plugins/flight ...@@ -3,28 +3,43 @@ import type { ClientReferenceManifest } from '../../build/webpack/plugins/flight
/** /**
* Get external stylesheet link hrefs based on server CSS manifest. * Get external stylesheet link hrefs based on server CSS manifest.
*/ */
export function getCssInlinedLinkTags( export function getLinkAndScriptTags(
clientReferenceManifest: ClientReferenceManifest, clientReferenceManifest: ClientReferenceManifest,
filePath: string, filePath: string,
injectedCSS: Set<string>, injectedCSS: Set<string>,
collectNewCSSImports?: boolean injectedScripts: Set<string>,
): string[] { collectNewImports?: boolean
): { styles: string[]; scripts: string[] } {
const filePathWithoutExt = filePath.replace(/\.[^.]+$/, '') const filePathWithoutExt = filePath.replace(/\.[^.]+$/, '')
const chunks = new Set<string>() const cssChunks = new Set<string>()
const jsChunks = new Set<string>()
const entryCSSFiles = const entryCSSFiles =
clientReferenceManifest.entryCSSFiles[filePathWithoutExt] clientReferenceManifest.entryCSSFiles[filePathWithoutExt]
const entryJSFiles =
clientReferenceManifest.entryJSFiles?.[filePathWithoutExt] ?? []
if (entryCSSFiles) { if (entryCSSFiles) {
for (const file of entryCSSFiles) { for (const file of entryCSSFiles) {
if (!injectedCSS.has(file)) { if (!injectedCSS.has(file)) {
if (collectNewCSSImports) { if (collectNewImports) {
injectedCSS.add(file) injectedCSS.add(file)
} }
chunks.add(file) cssChunks.add(file)
} }
} }
} }
return [...chunks] if (entryJSFiles) {
for (const file of entryJSFiles) {
if (!injectedScripts.has(file)) {
if (collectNewImports) {
injectedScripts.add(file)
}
jsChunks.add(file)
}
}
}
return { styles: [...cssChunks], scripts: [...jsChunks] }
} }
import React from 'react' import React from 'react'
import { getCssInlinedLinkTags } from './get-css-inlined-link-tags' import { getLinkAndScriptTags } from './get-css-inlined-link-tags'
import { getPreloadableFonts } from './get-preloadable-fonts' import { getPreloadableFonts } from './get-preloadable-fonts'
import type { AppRenderContext } from './app-render' import type { AppRenderContext } from './app-render'
import { getAssetQueryString } from './get-asset-query-string' import { getAssetQueryString } from './get-asset-query-string'
...@@ -8,21 +8,24 @@ export function getLayerAssets({ ...@@ -8,21 +8,24 @@ export function getLayerAssets({
ctx, ctx,
layoutOrPagePath, layoutOrPagePath,
injectedCSS: injectedCSSWithCurrentLayout, injectedCSS: injectedCSSWithCurrentLayout,
injectedJS: injectedJSWithCurrentLayout,
injectedFontPreloadTags: injectedFontPreloadTagsWithCurrentLayout, injectedFontPreloadTags: injectedFontPreloadTagsWithCurrentLayout,
}: { }: {
layoutOrPagePath: string | undefined layoutOrPagePath: string | undefined
injectedCSS: Set<string> injectedCSS: Set<string>
injectedJS: Set<string>
injectedFontPreloadTags: Set<string> injectedFontPreloadTags: Set<string>
ctx: AppRenderContext ctx: AppRenderContext
}): React.ReactNode { }): React.ReactNode {
const stylesheets: string[] = layoutOrPagePath const { styles: styleTags, scripts: scriptTags } = layoutOrPagePath
? getCssInlinedLinkTags( ? getLinkAndScriptTags(
ctx.clientReferenceManifest, ctx.clientReferenceManifest,
layoutOrPagePath, layoutOrPagePath,
injectedCSSWithCurrentLayout, injectedCSSWithCurrentLayout,
injectedJSWithCurrentLayout,
true true
) )
: [] : { styles: [], scripts: [] }
const preloadedFontFiles = layoutOrPagePath const preloadedFontFiles = layoutOrPagePath
? getPreloadableFonts( ? getPreloadableFonts(
...@@ -53,8 +56,8 @@ export function getLayerAssets({ ...@@ -53,8 +56,8 @@ export function getLayerAssets({
} }
} }
const styles = stylesheets const styles = styleTags
? stylesheets.map((href, index) => { ? styleTags.map((href, index) => {
// In dev, Safari and Firefox will cache the resource during HMR: // In dev, Safari and Firefox will cache the resource during HMR:
// - https://github.com/vercel/next.js/issues/5860 // - https://github.com/vercel/next.js/issues/5860
// - https://bugs.webkit.org/show_bug.cgi?id=187726 // - https://bugs.webkit.org/show_bug.cgi?id=187726
...@@ -88,7 +91,15 @@ export function getLayerAssets({ ...@@ -88,7 +91,15 @@ export function getLayerAssets({
/> />
) )
}) })
: null : []
const scripts = scriptTags
? scriptTags.map((href, index) => {
const fullSrc = `${ctx.assetPrefix}/_next/${href}`
return <script src={fullSrc} async={true} key={index} />
})
: []
return styles return styles.length || scripts.length ? [...styles, ...scripts] : null
} }
...@@ -10,7 +10,7 @@ import { ...@@ -10,7 +10,7 @@ import {
matchSegment, matchSegment,
} from '../../client/components/match-segments' } from '../../client/components/match-segments'
import type { LoaderTree } from '../lib/app-dir-module' import type { LoaderTree } from '../lib/app-dir-module'
import { getCssInlinedLinkTags } from './get-css-inlined-link-tags' import { getLinkAndScriptTags } from './get-css-inlined-link-tags'
import { getPreloadableFonts } from './get-preloadable-fonts' import { getPreloadableFonts } from './get-preloadable-fonts'
import { import {
addSearchParamsIfPageSegment, addSearchParamsIfPageSegment,
...@@ -35,6 +35,7 @@ export async function walkTreeWithFlightRouterState({ ...@@ -35,6 +35,7 @@ export async function walkTreeWithFlightRouterState({
parentRendered, parentRendered,
rscPayloadHead, rscPayloadHead,
injectedCSS, injectedCSS,
injectedJS,
injectedFontPreloadTags, injectedFontPreloadTags,
rootLayoutIncluded, rootLayoutIncluded,
asNotFound, asNotFound,
...@@ -49,6 +50,7 @@ export async function walkTreeWithFlightRouterState({ ...@@ -49,6 +50,7 @@ export async function walkTreeWithFlightRouterState({
parentRendered?: boolean parentRendered?: boolean
rscPayloadHead: React.ReactNode rscPayloadHead: React.ReactNode
injectedCSS: Set<string> injectedCSS: Set<string>
injectedJS: Set<string>
injectedFontPreloadTags: Set<string> injectedFontPreloadTags: Set<string>
rootLayoutIncluded: boolean rootLayoutIncluded: boolean
asNotFound?: boolean asNotFound?: boolean
...@@ -145,6 +147,7 @@ export async function walkTreeWithFlightRouterState({ ...@@ -145,6 +147,7 @@ export async function walkTreeWithFlightRouterState({
parentParams: currentParams, parentParams: currentParams,
firstItem: isFirst, firstItem: isFirst,
injectedCSS, injectedCSS,
injectedJS,
injectedFontPreloadTags, injectedFontPreloadTags,
// This is intentionally not "rootLayoutIncludedAtThisLevelOrAbove" as createComponentTree starts at the current level and does a check for "rootLayoutAtThisLevel" too. // This is intentionally not "rootLayoutIncludedAtThisLevelOrAbove" as createComponentTree starts at the current level and does a check for "rootLayoutAtThisLevel" too.
rootLayoutIncluded, rootLayoutIncluded,
...@@ -160,16 +163,17 @@ export async function walkTreeWithFlightRouterState({ ...@@ -160,16 +163,17 @@ export async function walkTreeWithFlightRouterState({
: (() => { : (() => {
const { layoutOrPagePath } = parseLoaderTree(loaderTreeToFilter) const { layoutOrPagePath } = parseLoaderTree(loaderTreeToFilter)
const styles = getLayerAssets({ const layerAssets = getLayerAssets({
ctx, ctx,
layoutOrPagePath, layoutOrPagePath,
injectedCSS: new Set(injectedCSS), injectedCSS: new Set(injectedCSS),
injectedJS: new Set(injectedJS),
injectedFontPreloadTags: new Set(injectedFontPreloadTags), injectedFontPreloadTags: new Set(injectedFontPreloadTags),
}) })
return ( return (
<> <>
{styles} {layerAssets}
{rscPayloadHead} {rscPayloadHead}
</> </>
) )
...@@ -183,14 +187,16 @@ export async function walkTreeWithFlightRouterState({ ...@@ -183,14 +187,16 @@ export async function walkTreeWithFlightRouterState({
// the result consistent. // the result consistent.
const layoutPath = layout?.[1] const layoutPath = layout?.[1]
const injectedCSSWithCurrentLayout = new Set(injectedCSS) const injectedCSSWithCurrentLayout = new Set(injectedCSS)
const injectedJSWithCurrentLayout = new Set(injectedJS)
const injectedFontPreloadTagsWithCurrentLayout = new Set( const injectedFontPreloadTagsWithCurrentLayout = new Set(
injectedFontPreloadTags injectedFontPreloadTags
) )
if (layoutPath) { if (layoutPath) {
getCssInlinedLinkTags( getLinkAndScriptTags(
ctx.clientReferenceManifest, ctx.clientReferenceManifest,
layoutPath, layoutPath,
injectedCSSWithCurrentLayout, injectedCSSWithCurrentLayout,
injectedJSWithCurrentLayout,
true true
) )
getPreloadableFonts( getPreloadableFonts(
...@@ -224,6 +230,7 @@ export async function walkTreeWithFlightRouterState({ ...@@ -224,6 +230,7 @@ export async function walkTreeWithFlightRouterState({
isFirst: false, isFirst: false,
rscPayloadHead, rscPayloadHead,
injectedCSS: injectedCSSWithCurrentLayout, injectedCSS: injectedCSSWithCurrentLayout,
injectedJS: injectedJSWithCurrentLayout,
injectedFontPreloadTags: injectedFontPreloadTagsWithCurrentLayout, injectedFontPreloadTags: injectedFontPreloadTagsWithCurrentLayout,
rootLayoutIncluded: rootLayoutIncludedAtThisLevelOrAbove, rootLayoutIncluded: rootLayoutIncludedAtThisLevelOrAbove,
asNotFound, asNotFound,
......
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