Unverified Commit 7c0a504a authored by JJ Kasper's avatar JJ Kasper Committed by GitHub

Handle assetPrefix in app (#39236)

This ensures we properly leverage the `assetPrefix` for `app`. Note for reviewing the test changes are mostly spacing so hiding that may help. 

## Bug

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

Fixes: [slack thread](https://vercel.slack.com/archives/C035J346QQL/p1659387244559979)
parent d56aadae
...@@ -33,7 +33,8 @@ self.__next_require__ = __webpack_require__ ...@@ -33,7 +33,8 @@ self.__next_require__ = __webpack_require__
;(self as any).__next_chunk_load__ = (chunk: string) => { ;(self as any).__next_chunk_load__ = (chunk: string) => {
if (!chunk) return Promise.resolve() if (!chunk) return Promise.resolve()
if (chunk.endsWith('.css')) { if (chunk.endsWith('.css')) {
const chunkPath = `/_next/${chunk}` // @ts-expect-error __webpack_public_path__ is inlined by webpack
const chunkPath = `${__webpack_public_path__ || ''}${chunk}`
const existingTag = document.querySelector(`link[href="${chunkPath}"]`) const existingTag = document.querySelector(`link[href="${chunkPath}"]`)
if (!existingTag) { if (!existingTag) {
const link = document.createElement('link') const link = document.createElement('link')
......
...@@ -37,6 +37,7 @@ export type RenderOptsPartial = { ...@@ -37,6 +37,7 @@ export type RenderOptsPartial = {
supportsDynamicHTML?: boolean supportsDynamicHTML?: boolean
runtime?: ServerRuntime runtime?: ServerRuntime
serverComponents?: boolean serverComponents?: boolean
assetPrefix?: string
} }
export type RenderOpts = LoadComponentsReturnType & RenderOptsPartial export type RenderOpts = LoadComponentsReturnType & RenderOptsPartial
...@@ -962,7 +963,11 @@ export async function renderToHTMLOrFlight( ...@@ -962,7 +963,11 @@ export async function renderToHTMLOrFlight(
return ( return (
<AppRouter <AppRouter
hotReloader={HotReloader && <HotReloader assetPrefix="" />} hotReloader={
HotReloader && (
<HotReloader assetPrefix={renderOpts.assetPrefix || ''} />
)
}
initialCanonicalUrl={initialCanonicalUrl} initialCanonicalUrl={initialCanonicalUrl}
initialTree={initialTree} initialTree={initialTree}
initialStylesheets={initialStylesheets} initialStylesheets={initialStylesheets}
...@@ -1021,7 +1026,7 @@ export async function renderToHTMLOrFlight( ...@@ -1021,7 +1026,7 @@ export async function renderToHTMLOrFlight(
streamOptions: { streamOptions: {
// Include hydration scripts in the HTML // Include hydration scripts in the HTML
bootstrapScripts: buildManifest.rootMainFiles.map( bootstrapScripts: buildManifest.rootMainFiles.map(
(src) => '/_next/' + src (src) => `${renderOpts.assetPrefix || ''}/_next/` + src
), ),
}, },
}) })
......
...@@ -6,12 +6,16 @@ module.exports = { ...@@ -6,12 +6,16 @@ module.exports = {
legacyBrowsers: false, legacyBrowsers: false,
browsersListForSwc: true, browsersListForSwc: true,
}, },
// assetPrefix: '/assets',
rewrites: async () => { rewrites: async () => {
return [ return {
{ // beforeFiles: [ { source: '/assets/:path*', destination: '/:path*' } ],
source: '/rewritten-to-dashboard', afterFiles: [
destination: '/dashboard', {
}, source: '/rewritten-to-dashboard',
] destination: '/dashboard',
},
],
}
}, },
} }
This diff is collapsed.
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