Skip to content

Commit

Permalink
fix(app-render): make css and font respect assetPrefix (#41455)
Browse files Browse the repository at this point in the history
Add missing `assetPrefix` for link tags (`@next/font` and CSS).
The PR also merges multiple duplicated `renderOpt.assetPrefix || ''`.
  • Loading branch information
SukkaW committed Oct 16, 2022
1 parent 1d37afb commit 7c2fd34
Showing 1 changed file with 10 additions and 8 deletions.
18 changes: 10 additions & 8 deletions packages/next/server/app-render.tsx
Expand Up @@ -872,6 +872,8 @@ export async function renderToHTMLOrFlight(
ComponentMod.pages
)

const assetPrefix = renderOpts.assetPrefix || ''

/**
* Use the provided loader tree to create the React Component tree.
*/
Expand Down Expand Up @@ -1105,7 +1107,7 @@ export async function renderToHTMLOrFlight(
<link
key={fontFile}
rel="preload"
href={`/_next/${fontFile}`}
href={`${assetPrefix}/_next/${fontFile}`}
as="font"
type={`font/${ext}`}
crossOrigin="anonymous"
Expand All @@ -1116,7 +1118,7 @@ export async function renderToHTMLOrFlight(
? stylesheets.map((href) => (
<link
rel="stylesheet"
href={`/_next/${href}${cacheBustingUrlSuffix}`}
href={`${assetPrefix}/_next/${href}${cacheBustingUrlSuffix}`}
// `Precedence` is an opt-in signal for React to handle
// resource loading and deduplication, etc:
// https://github.com/facebook/react/pull/25060
Expand Down Expand Up @@ -1351,7 +1353,7 @@ export async function renderToHTMLOrFlight(

return (
<AppRouter
assetPrefix={renderOpts.assetPrefix || ''}
assetPrefix={assetPrefix}
initialCanonicalUrl={initialCanonicalUrl}
initialTree={initialTree}
>
Expand Down Expand Up @@ -1397,7 +1399,7 @@ export async function renderToHTMLOrFlight(
polyfill.endsWith('.js') && !polyfill.endsWith('.module.js')
)
.map((polyfill) => ({
src: `${renderOpts.assetPrefix || ''}/_next/${polyfill}`,
src: `${assetPrefix}/_next/${polyfill}`,
integrity: subresourceIntegrityManifest?.[polyfill],
}))

Expand Down Expand Up @@ -1444,11 +1446,11 @@ export async function renderToHTMLOrFlight(
bootstrapScripts: [
...(subresourceIntegrityManifest
? buildManifest.rootMainFiles.map((src) => ({
src: `${renderOpts.assetPrefix || ''}/_next/` + src,
src: `${assetPrefix}/_next/` + src,
integrity: subresourceIntegrityManifest[src],
}))
: buildManifest.rootMainFiles.map(
(src) => `${renderOpts.assetPrefix || ''}/_next/` + src
(src) => `${assetPrefix}/_next/` + src
)),
],
},
Expand Down Expand Up @@ -1476,11 +1478,11 @@ export async function renderToHTMLOrFlight(
// Include hydration scripts in the HTML
bootstrapScripts: subresourceIntegrityManifest
? buildManifest.rootMainFiles.map((src) => ({
src: `${renderOpts.assetPrefix || ''}/_next/` + src,
src: `${assetPrefix}/_next/` + src,
integrity: subresourceIntegrityManifest[src],
}))
: buildManifest.rootMainFiles.map(
(src) => `${renderOpts.assetPrefix || ''}/_next/` + src
(src) => `${assetPrefix}/_next/` + src
),
},
})
Expand Down

0 comments on commit 7c2fd34

Please sign in to comment.