From cccf547d32084db5f0a9c344b953ccf78318671e Mon Sep 17 00:00:00 2001 From: Finn Woelm Date: Thu, 27 Aug 2020 21:20:04 +0200 Subject: [PATCH] Fix: Use 404.html page in publish folder In Netlify, you can set a custom 404 page by putting a 404.html file into the publish folder (e.g., public or whatever is specified in netlify.toml). Until now, this custom 404 page was not picked up by netlify dev. For it to work in netlify dev, the custom 404.html file had to be in the root folder. This commit makes the behavior of netlify dev consistent with Netlify in production by using the custom 404.html page in the publish folder. Fixes: https://github.com/netlify/cli/issues/1158 --- src/commands/dev/index.js | 2 +- tests/command.dev.test.js | 25 +++++++++++++++++++++++++ 2 files changed, 26 insertions(+), 1 deletion(-) diff --git a/src/commands/dev/index.js b/src/commands/dev/index.js index ba5a2be9f41..9774ad25ec6 100644 --- a/src/commands/dev/index.js +++ b/src/commands/dev/index.js @@ -380,7 +380,7 @@ async function startDevServer(settings, log) { name: 'netlify-dev', port: settings.frameworkPort, templates: { - notFound: '404.html', + notFound: path.join(settings.dist, '404.html'), }, }) diff --git a/tests/command.dev.test.js b/tests/command.dev.test.js index 8af6420dab2..d3ff61719a5 100644 --- a/tests/command.dev.test.js +++ b/tests/command.dev.test.js @@ -658,6 +658,31 @@ test('should return 404.html if exists for non existing routes', async t => { }) }) +test('should return 404.html from publish folder if exists for non existing routes', async t => { + await withSiteBuilder('site-with-shadowing-404-in-publish-folder', async builder => { + builder + .withContentFile({ + path: 'public/404.html', + content: '

404 - My Custom 404 Page

', + }) + .withNetlifyToml({ + config: { + build: { + publish: 'public/', + }, + }, + }) + + await builder.buildAsync() + + await withDevServer({ cwd: builder.directory }, async server => { + const response = await fetch(`${server.url}/non-existent`) + t.is(response.status, 404) + t.is(await response.text(), '

404 - My Custom 404 Page

') + }) + }) +}) + test('should return 404 for redirect', async t => { await withSiteBuilder('site-with-shadowing-404-redirect', async builder => { builder