Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix manifest load errors when using assetPrefix #55416

Merged
merged 2 commits into from
Sep 15, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
8 changes: 6 additions & 2 deletions packages/next/src/client/page-loader.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,10 @@ import { isDynamicRoute } from '../shared/lib/router/utils/is-dynamic'
import { parseRelativeUrl } from '../shared/lib/router/utils/parse-relative-url'
import { removeTrailingSlash } from '../shared/lib/router/utils/remove-trailing-slash'
import { createRouteLoader, getClientBuildManifest } from './route-loader'
import {
DEV_CLIENT_PAGES_MANIFEST,
DEV_MIDDLEWARE_MANIFEST,
} from '../shared/lib/constants'

declare global {
interface Window {
Expand Down Expand Up @@ -60,7 +64,7 @@ export default class PageLoader {
return window.__DEV_PAGES_MANIFEST.pages
} else {
this.promisedDevPagesManifest ||= fetch(
`${this.assetPrefix}/_next/static/development/_devPagesManifest.json`
`${this.assetPrefix}/_next/static/development/${DEV_CLIENT_PAGES_MANIFEST}`
)
.then((res) => res.json())
.then((manifest: { pages: string[] }) => {
Expand Down Expand Up @@ -94,7 +98,7 @@ export default class PageLoader {
// TODO: Decide what should happen when fetching fails instead of asserting
// @ts-ignore
this.promisedMiddlewareMatchers = fetch(
`${this.assetPrefix}/_next/static/${this.buildId}/_devMiddlewareManifest.json`
`${this.assetPrefix}/_next/static/${this.buildId}/${DEV_MIDDLEWARE_MANIFEST}`
)
.then((res) => res.json())
.then((matchers: MiddlewareMatcher[]) => {
Expand Down
4 changes: 2 additions & 2 deletions packages/next/src/server/lib/router-utils/setup-dev.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1919,7 +1919,7 @@ async function startWatcher(opts: SetupOpts) {
async function requestHandler(req: IncomingMessage, res: ServerResponse) {
const parsedUrl = url.parse(req.url || '/')

if (parsedUrl.pathname === clientPagesManifestPath) {
if (parsedUrl.pathname?.includes(clientPagesManifestPath)) {
res.statusCode = 200
res.setHeader('Content-Type', 'application/json; charset=utf-8')
res.end(
Expand All @@ -1932,7 +1932,7 @@ async function startWatcher(opts: SetupOpts) {
return { finished: true }
}

if (parsedUrl.pathname === devMiddlewareManifestPath) {
if (parsedUrl.pathname?.includes(devMiddlewareManifestPath)) {
res.statusCode = 200
res.setHeader('Content-Type', 'application/json; charset=utf-8')
res.end(JSON.stringify(serverFields.middleware?.matchers || []))
Expand Down
28 changes: 28 additions & 0 deletions test/development/basic/asset-prefix.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
import { join } from 'path'
import { createNextDescribe } from 'e2e-utils'
import { check } from 'next-test-utils'

createNextDescribe(
'asset-prefix',
{
files: join(__dirname, 'asset-prefix'),
},
({ next }) => {
it('should load the app properly without reloading', async () => {
const browser = await next.browser('/')
await browser.eval(`window.__v = 1`)

expect(await browser.elementByCss('div').text()).toBe('Hello World')

await check(async () => {
const logs = await browser.log()
const hasError = logs.some((log) =>
log.message.includes('Failed to fetch')
)
return hasError ? 'error' : 'success'
}, 'success')

expect(await browser.eval(`window.__v`)).toBe(1)
})
}
)
20 changes: 20 additions & 0 deletions test/development/basic/asset-prefix/next.config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
const ASSET_PREFIX = 'asset-prefix'

module.exports = {
assetPrefix: ASSET_PREFIX,
i18n: {
locales: ['en-US'],
defaultLocale: 'en-US',
},
async rewrites() {
return {
beforeFiles: [
{
source: `/:locale/${ASSET_PREFIX}/_next/:path*`,
destination: '/_next/:path*',
locale: false,
},
],
}
},
}
5 changes: 5 additions & 0 deletions test/development/basic/asset-prefix/pages/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
import React from 'react'

export default function Page() {
return <div>Hello World</div>
}