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

Add invariant with debug information when failing to load static file #38075

Merged
merged 1 commit into from
Jun 27, 2022
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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 21 additions & 0 deletions packages/next/server/base-server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ import {
NormalizeError,
DecodeError,
normalizeRepeatedSlashes,
MissingStaticPage,
} from '../shared/lib/utils'
import type { PreviewData } from 'next/types'
import type { PagesManifest } from '../build/webpack/plugins/pages-manifest-plugin'
Expand Down Expand Up @@ -1856,6 +1857,26 @@ export default abstract class Server<ServerOptions extends Options = Options> {
}
} catch (error) {
const err = getProperError(error)

if (error instanceof MissingStaticPage) {
console.error(
'Invariant: failed to load static page',
JSON.stringify(
{
page,
url: ctx.req.url,
matchedPath: ctx.req.headers['x-matched-path'],
initUrl: getRequestMeta(ctx.req, '__NEXT_INIT_URL'),
didRewrite: getRequestMeta(ctx.req, '_nextDidRewrite'),
rewroteUrl: getRequestMeta(ctx.req, '_nextRewroteUrl'),
},
null,
2
)
)
throw err
}

if (err instanceof NoFallbackError && bubbleNoFallback) {
throw err
}
Expand Down
6 changes: 4 additions & 2 deletions packages/next/server/require.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import { normalizeLocalePath } from '../shared/lib/i18n/normalize-locale-path'
import { normalizePagePath } from '../shared/lib/page-path/normalize-page-path'
import { denormalizePagePath } from '../shared/lib/page-path/denormalize-page-path'
import type { PagesManifest } from '../build/webpack/plugins/pages-manifest-plugin'
import { PageNotFoundError } from '../shared/lib/utils'
import { PageNotFoundError, MissingStaticPage } from '../shared/lib/utils'

export function getPagePath(
page: string,
Expand Down Expand Up @@ -90,7 +90,9 @@ export function requirePage(
appDirEnabled
)
if (pagePath.endsWith('.html')) {
return promises.readFile(pagePath, 'utf8')
return promises.readFile(pagePath, 'utf8').catch((err) => {
throw new MissingStaticPage(page, err.message)
})
}
return require(pagePath)
}
Expand Down
7 changes: 7 additions & 0 deletions packages/next/shared/lib/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -413,6 +413,13 @@ export class PageNotFoundError extends Error {
}
}

export class MissingStaticPage extends Error {
constructor(page: string, message: string) {
super()
this.message = `Failed to load static file for page: ${page} ${message}`
}
}

export class MiddlewareNotFoundError extends Error {
code: string
constructor() {
Expand Down
18 changes: 18 additions & 0 deletions test/production/required-server-files.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -138,6 +138,24 @@ describe('should set-up next', () => {
if (server) await killApp(server)
})

it('should show invariant when an automatic static page is requested', async () => {
const toRename = `standalone/.next/server/pages/auto-static.html`
await next.renameFile(toRename, `${toRename}.bak`)

try {
const res = await fetchViaHTTP(appPort, '/auto-static', undefined, {
headers: {
'x-matched-path': '/auto-static',
},
})

expect(res.status).toBe(500)
await check(() => stderr, /Invariant: failed to load static page/)
} finally {
await next.renameFile(`${toRename}.bak`, toRename)
}
})

it.each([
{
case: 'redirect no revalidate',
Expand Down
3 changes: 3 additions & 0 deletions test/production/required-server-files/pages/auto-static.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
export default function Page() {
return <p>automatic static page</p>
}