From de3efc59e2062c56a95138b6077b2e7c9b91f7cb Mon Sep 17 00:00:00 2001 From: Zack Tanner Date: Thu, 14 Sep 2023 17:24:22 -0700 Subject: [PATCH 1/2] fix manifest load errors when using assetPrefix --- packages/next/src/client/page-loader.ts | 8 ++++++-- .../src/server/lib/router-utils/setup-dev.ts | 4 ++-- test/development/basic/asset-prefix.test.ts | 19 ++++++++++++++++++ .../basic/asset-prefix/next.config.js | 20 +++++++++++++++++++ .../basic/asset-prefix/pages/index.js | 5 +++++ 5 files changed, 52 insertions(+), 4 deletions(-) create mode 100644 test/development/basic/asset-prefix.test.ts create mode 100644 test/development/basic/asset-prefix/next.config.js create mode 100644 test/development/basic/asset-prefix/pages/index.js diff --git a/packages/next/src/client/page-loader.ts b/packages/next/src/client/page-loader.ts index 06cc6bcde6164..76ecb8fb0439d 100644 --- a/packages/next/src/client/page-loader.ts +++ b/packages/next/src/client/page-loader.ts @@ -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 { @@ -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[] }) => { @@ -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[]) => { diff --git a/packages/next/src/server/lib/router-utils/setup-dev.ts b/packages/next/src/server/lib/router-utils/setup-dev.ts index cd471284ffd81..88f63f8265091 100644 --- a/packages/next/src/server/lib/router-utils/setup-dev.ts +++ b/packages/next/src/server/lib/router-utils/setup-dev.ts @@ -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( @@ -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 || [])) diff --git a/test/development/basic/asset-prefix.test.ts b/test/development/basic/asset-prefix.test.ts new file mode 100644 index 0000000000000..94b6c24025d84 --- /dev/null +++ b/test/development/basic/asset-prefix.test.ts @@ -0,0 +1,19 @@ +import { join } from 'path' +import { createNextDescribe } from 'e2e-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') + + expect(await browser.eval(`window.__v`)).toBe(1) + }) + } +) diff --git a/test/development/basic/asset-prefix/next.config.js b/test/development/basic/asset-prefix/next.config.js new file mode 100644 index 0000000000000..cbce94fd47847 --- /dev/null +++ b/test/development/basic/asset-prefix/next.config.js @@ -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, + }, + ], + } + }, +} diff --git a/test/development/basic/asset-prefix/pages/index.js b/test/development/basic/asset-prefix/pages/index.js new file mode 100644 index 0000000000000..135367d1f85f3 --- /dev/null +++ b/test/development/basic/asset-prefix/pages/index.js @@ -0,0 +1,5 @@ +import React from 'react' + +export default function Page() { + return
Hello World
+} From 72680d841ddf81c24581cabcea401f05e723406e Mon Sep 17 00:00:00 2001 From: Zack Tanner Date: Thu, 14 Sep 2023 17:36:22 -0700 Subject: [PATCH 2/2] also check for errors in the logs --- test/development/basic/asset-prefix.test.ts | 9 +++++++++ 1 file changed, 9 insertions(+) diff --git a/test/development/basic/asset-prefix.test.ts b/test/development/basic/asset-prefix.test.ts index 94b6c24025d84..91e6f90adee64 100644 --- a/test/development/basic/asset-prefix.test.ts +++ b/test/development/basic/asset-prefix.test.ts @@ -1,5 +1,6 @@ import { join } from 'path' import { createNextDescribe } from 'e2e-utils' +import { check } from 'next-test-utils' createNextDescribe( 'asset-prefix', @@ -13,6 +14,14 @@ createNextDescribe( 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) }) }