Skip to content

Commit cccf547

Browse files
committed
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: #1158
1 parent f88b460 commit cccf547

File tree

2 files changed

+26
-1
lines changed

2 files changed

+26
-1
lines changed

src/commands/dev/index.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -380,7 +380,7 @@ async function startDevServer(settings, log) {
380380
name: 'netlify-dev',
381381
port: settings.frameworkPort,
382382
templates: {
383-
notFound: '404.html',
383+
notFound: path.join(settings.dist, '404.html'),
384384
},
385385
})
386386

tests/command.dev.test.js

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -658,6 +658,31 @@ test('should return 404.html if exists for non existing routes', async t => {
658658
})
659659
})
660660

661+
test('should return 404.html from publish folder if exists for non existing routes', async t => {
662+
await withSiteBuilder('site-with-shadowing-404-in-publish-folder', async builder => {
663+
builder
664+
.withContentFile({
665+
path: 'public/404.html',
666+
content: '<h1>404 - My Custom 404 Page</h1>',
667+
})
668+
.withNetlifyToml({
669+
config: {
670+
build: {
671+
publish: 'public/',
672+
},
673+
},
674+
})
675+
676+
await builder.buildAsync()
677+
678+
await withDevServer({ cwd: builder.directory }, async server => {
679+
const response = await fetch(`${server.url}/non-existent`)
680+
t.is(response.status, 404)
681+
t.is(await response.text(), '<h1>404 - My Custom 404 Page</h1>')
682+
})
683+
})
684+
})
685+
661686
test('should return 404 for redirect', async t => {
662687
await withSiteBuilder('site-with-shadowing-404-redirect', async builder => {
663688
builder

0 commit comments

Comments
 (0)