Skip to content
This repository was archived by the owner on May 10, 2021. It is now read-only.

Commit 683b656

Browse files
committed
Add support for NextJS 404.html page
For Netlify (deployed), the 404.html page in the out/ folder already works. For netlify dev, the 404.html page must be at the directory root. Fixes: #2
1 parent 3afc944 commit 683b656

File tree

4 files changed

+37
-0
lines changed

4 files changed

+37
-0
lines changed

README.md

+1
Original file line numberDiff line numberDiff line change
@@ -107,6 +107,7 @@ Lastly, add the following lines to your `.gitignore`:
107107
# Files generated by next-on-netlify command
108108
functions/nextRouter
109109
out/
110+
404.html
110111
```
111112

112113
Now you're all set.

cypress/integration/default_spec.js

+14
Original file line numberDiff line numberDiff line change
@@ -199,3 +199,17 @@ describe('dynamic static page', () => {
199199
cy.get('p').should('contain', 'it has a dynamic URL parameter: /static/:id.')
200200
})
201201
})
202+
203+
describe('404 page', () => {
204+
it('renders', () => {
205+
cy.request({
206+
url: '/this-page-does-not-exist',
207+
failOnStatusCode: false
208+
}).then(response => {
209+
expect(response.status).to.eq(404)
210+
cy.state('document').write(response.body)
211+
})
212+
213+
cy.get('h2').should('contain', 'This page could not be found.')
214+
})
215+
})

next-on-netlify.js

+9
Original file line numberDiff line numberDiff line change
@@ -182,5 +182,14 @@ writeFileSync(
182182
)
183183

184184

185+
// 6. Copy 404.html to directory root
186+
// This is a temporary workaround. Currenly, Netlify (deployed) expects the
187+
// 404.html file in the publish folder, while netlify-cli dev expects it at
188+
// the directory root. In order to cover both cases, we copy the 404.html to
189+
// both locations until this is fixed.
190+
if(existsSync(join(OUTPUT_PATH, "404.html")))
191+
copySync(join(OUTPUT_PATH, "404.html"), join(".", "404.html"))
192+
193+
185194
// Done!
186195
console.log("\x1b[1m✅ Success! All done!\x1b[22m")

tests/defaults.test.js

+13
Original file line numberDiff line numberDiff line change
@@ -186,6 +186,19 @@ describe('Static Pages', () => {
186186
})
187187
})
188188

189+
describe('404 Page', () => {
190+
test('copies 404.html to output directory', () => {
191+
const OUTPUT_PATH = join(PROJECT_PATH, "out")
192+
193+
expect(existsSync(join(OUTPUT_PATH, "404.html"))).toBe(true)
194+
})
195+
196+
// This is required for 404.html to work on netlify-dev
197+
test('copies 404.html to directory root', () => {
198+
expect(existsSync(join(PROJECT_PATH, "404.html"))).toBe(true)
199+
})
200+
})
201+
189202
describe('Public assets', () => {
190203
test('copies public files to output directory', () => {
191204
const OUTPUT_PATH = join(PROJECT_PATH, "out")

0 commit comments

Comments
 (0)