From ed346835d324fc88b07bdb8450f93c21008b660a Mon Sep 17 00:00:00 2001 From: JJ Kasper Date: Thu, 18 Jul 2019 19:34:57 -0500 Subject: [PATCH] Fix /_error not rendering 404 in development --- packages/next-server/lib/constants.ts | 2 +- packages/next-server/server/next-server.ts | 14 ++++++++++---- .../client-navigation/test/index.test.js | 9 +++++++++ 3 files changed, 20 insertions(+), 5 deletions(-) diff --git a/packages/next-server/lib/constants.ts b/packages/next-server/lib/constants.ts index 2a2834aba5da8..82c5376f4ad6f 100644 --- a/packages/next-server/lib/constants.ts +++ b/packages/next-server/lib/constants.ts @@ -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'] export const CLIENT_PUBLIC_FILES_PATH = 'public' export const CLIENT_STATIC_FILES_PATH = 'static' export const CLIENT_STATIC_FILES_RUNTIME = 'runtime' diff --git a/packages/next-server/server/next-server.ts b/packages/next-server/server/next-server.ts index 683bd8e0f413c..26cfbd8a065db 100644 --- a/packages/next-server/server/next-server.ts +++ b/packages/next-server/server/next-server.ts @@ -14,6 +14,7 @@ import { PHASE_PRODUCTION_SERVER, SERVER_DIRECTORY, SERVERLESS_DIRECTORY, + BLOCKED_PAGES, } from '../lib/constants' import { getRouteMatcher, @@ -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) @@ -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)) || diff --git a/test/integration/client-navigation/test/index.test.js b/test/integration/client-navigation/test/index.test.js index 469739e649460..ae64a560307bc 100644 --- a/test/integration/client-navigation/test/index.test.js +++ b/test/integration/client-navigation/test/index.test.js @@ -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)