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 /_error not rendering 404 in development #8033

Closed
wants to merge 1 commit into from
Closed
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
2 changes: 1 addition & 1 deletion packages/next-server/lib/constants.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ export const SERVER_DIRECTORY = 'server'
export const SERVERLESS_DIRECTORY = 'serverless'
export const CONFIG_FILE = 'next.config.js'
export const BUILD_ID_FILE = 'BUILD_ID'
export const BLOCKED_PAGES = ['/_document', '/_app']
export const BLOCKED_PAGES = ['/_document', '/_app', '/_error']
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is also used during next build with serverless target I think 🤔

export const CLIENT_PUBLIC_FILES_PATH = 'public'
export const CLIENT_STATIC_FILES_PATH = 'static'
export const CLIENT_STATIC_FILES_RUNTIME = 'runtime'
Expand Down
14 changes: 10 additions & 4 deletions packages/next-server/server/next-server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import {
PHASE_PRODUCTION_SERVER,
SERVER_DIRECTORY,
SERVERLESS_DIRECTORY,
BLOCKED_PAGES,
} from '../lib/constants'
import {
getRouteMatcher,
Expand Down Expand Up @@ -240,6 +241,15 @@ export default class Server {
},
]

BLOCKED_PAGES.map(page => {
routes.unshift({
match: route(page),
fn: async (req, res, params, parsedUrl) => {
return this.render404(req, res, parsedUrl)
},
})
})

if (
this.nextConfig.experimental.publicDirectory &&
fs.existsSync(this.publicDir)
Expand Down Expand Up @@ -414,10 +424,6 @@ export default class Server {
return this.handleRequest(req, res, parsedUrl)
}

if (isBlockedPage(pathname)) {
return this.render404(req, res, parsedUrl)
}

const html = await this.renderToHTML(req, res, pathname, query, {
dataOnly:
(this.renderOpts.ampBindInitData && Boolean(query.dataOnly)) ||
Expand Down
9 changes: 9 additions & 0 deletions test/integration/client-navigation/test/index.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -1038,6 +1038,15 @@ describe('Client Navigation', () => {
await browser.close()
})

it('should render 404 for reserved pages', async () => {
const reservedPages = ['/_app', '/_document', '/_error']

for (const page of reservedPages) {
const html = await renderViaHTTP(context.appPort, page)
expect(html).toMatch(/This page could not be found/)
}
})

renderingSuite(
(p, q) => renderViaHTTP(context.appPort, p, q),
(p, q) => fetchViaHTTP(context.appPort, p, q)
Expand Down