Unverified Commit e3fb62b6 authored by JJ Kasper's avatar JJ Kasper Committed by GitHub

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

parent 77f9a6a7
...@@ -22,6 +22,7 @@ import { ...@@ -22,6 +22,7 @@ import {
NormalizeError, NormalizeError,
DecodeError, DecodeError,
normalizeRepeatedSlashes, normalizeRepeatedSlashes,
MissingStaticPage,
} from '../shared/lib/utils' } from '../shared/lib/utils'
import type { PreviewData } from 'next/types' import type { PreviewData } from 'next/types'
import type { PagesManifest } from '../build/webpack/plugins/pages-manifest-plugin' import type { PagesManifest } from '../build/webpack/plugins/pages-manifest-plugin'
...@@ -1856,6 +1857,26 @@ export default abstract class Server<ServerOptions extends Options = Options> { ...@@ -1856,6 +1857,26 @@ export default abstract class Server<ServerOptions extends Options = Options> {
} }
} catch (error) { } catch (error) {
const err = getProperError(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) { if (err instanceof NoFallbackError && bubbleNoFallback) {
throw err throw err
} }
......
...@@ -11,7 +11,7 @@ import { normalizeLocalePath } from '../shared/lib/i18n/normalize-locale-path' ...@@ -11,7 +11,7 @@ import { normalizeLocalePath } from '../shared/lib/i18n/normalize-locale-path'
import { normalizePagePath } from '../shared/lib/page-path/normalize-page-path' import { normalizePagePath } from '../shared/lib/page-path/normalize-page-path'
import { denormalizePagePath } from '../shared/lib/page-path/denormalize-page-path' import { denormalizePagePath } from '../shared/lib/page-path/denormalize-page-path'
import type { PagesManifest } from '../build/webpack/plugins/pages-manifest-plugin' 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( export function getPagePath(
page: string, page: string,
...@@ -90,7 +90,9 @@ export function requirePage( ...@@ -90,7 +90,9 @@ export function requirePage(
appDirEnabled appDirEnabled
) )
if (pagePath.endsWith('.html')) { 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) return require(pagePath)
} }
......
...@@ -413,6 +413,13 @@ export class PageNotFoundError extends Error { ...@@ -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 { export class MiddlewareNotFoundError extends Error {
code: string code: string
constructor() { constructor() {
......
...@@ -138,6 +138,24 @@ describe('should set-up next', () => { ...@@ -138,6 +138,24 @@ describe('should set-up next', () => {
if (server) await killApp(server) 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([ it.each([
{ {
case: 'redirect no revalidate', case: 'redirect no revalidate',
......
export default function Page() {
return <p>automatic static page</p>
}
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