Skip to content

Commit cbb71e7

Browse files
fix: move locale/404 to publishPath/404 if custom 404 (#616)
1 parent f2d553e commit cbb71e7

File tree

6 files changed

+39
-1
lines changed

6 files changed

+39
-1
lines changed

src/lib/pages/getStaticProps/setup.js

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,11 @@
11
const { join } = require('path')
22

3+
const { copySync } = require('fs-extra')
34
const slash = require('slash')
45

56
const getFilePathForRoute = require('../../helpers/getFilePathForRoute')
7+
const getI18n = require('../../helpers/getI18n')
8+
const getNextDistDir = require('../../helpers/getNextDistDir')
69
const isRouteWithFallback = require('../../helpers/isRouteWithFallback')
710
const { logTitle } = require('../../helpers/logger')
811

@@ -15,6 +18,8 @@ const setup = async ({ functionsPath, publishPath }) => {
1518
// a function for the same file path twice
1619
const filePathsDone = new Set()
1720
const pages = await getPages()
21+
const nextDistDir = await getNextDistDir()
22+
const i18n = await getI18n()
1823

1924
const jobs = []
2025

@@ -34,6 +39,11 @@ const setup = async ({ functionsPath, publishPath }) => {
3439
publishPath,
3540
})
3641

42+
// HACK: If i18n, 404.html needs to be at the top level of the publish directory
43+
if (i18n.defaultLocale && route === `/${i18n.defaultLocale}/404`) {
44+
copySync(join(nextDistDir, 'serverless', 'pages', htmlPath), join(publishPath, '404.html'))
45+
}
46+
3747
// Set up the Netlify function (this is ONLY for preview mode)
3848
const relativePath = getFilePathForRoute(srcRoute || route, 'js')
3949
const filePath = slash(join('pages', relativePath))

src/tests/__snapshots__/basePath.test.js.snap

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
exports[`Routing creates Netlify redirects 1`] = `
44
"# Next-on-Netlify Redirects
55
/ /foo 301!
6+
/_next/data/%BUILD_ID%/404.json /.netlify/functions/next_404 200! Cookie=__prerender_bypass,__next_preview_data
67
/_next/data/%BUILD_ID%/getServerSideProps/static.json /.netlify/functions/next_getServerSideProps_static 200
78
/_next/data/%BUILD_ID%/getStaticProps/1.json /.netlify/functions/next_getStaticProps_id 200! Cookie=__prerender_bypass,__next_preview_data
89
/_next/data/%BUILD_ID%/getStaticProps/2.json /.netlify/functions/next_getStaticProps_id 200! Cookie=__prerender_bypass,__next_preview_data
@@ -21,6 +22,7 @@ exports[`Routing creates Netlify redirects 1`] = `
2122
/api/hello-background /foo/api/hello-background 301!
2223
/api/static /foo/api/static 301!
2324
/foo /.netlify/functions/next_index 200
25+
/foo/404 /.netlify/functions/next_404 200! Cookie=__prerender_bypass,__next_preview_data
2426
/foo/404 /404 200
2527
/foo/_next/* /_next/:splat 301!
2628
/foo/api/hello-background /.netlify/functions/next_api_hello-background 200

src/tests/__snapshots__/defaults.test.js.snap

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,8 @@ exports[`Headers creates Netlify headers 1`] = `
1111
exports[`Routing creates Netlify redirects 1`] = `
1212
"# Next-on-Netlify Redirects
1313
/ /.netlify/functions/next_index 200
14+
/404 /.netlify/functions/next_404 200! Cookie=__prerender_bypass,__next_preview_data
15+
/_next/data/%BUILD_ID%/404.json /.netlify/functions/next_404 200! Cookie=__prerender_bypass,__next_preview_data
1416
/_next/data/%BUILD_ID%/getServerSideProps/static.json /.netlify/functions/next_getServerSideProps_static 200
1517
/_next/data/%BUILD_ID%/getStaticProps/1.json /.netlify/functions/next_getStaticProps_id 200! Cookie=__prerender_bypass,__next_preview_data
1618
/_next/data/%BUILD_ID%/getStaticProps/2.json /.netlify/functions/next_getStaticProps_id 200! Cookie=__prerender_bypass,__next_preview_data

src/tests/__snapshots__/i18n.test.js.snap

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,11 @@
33
exports[`Routing creates Netlify redirects 1`] = `
44
"# Next-on-Netlify Redirects
55
/ /.netlify/functions/next_index 200
6-
/404 /en/404.html 200
6+
/404 /.netlify/functions/next_404 200! Cookie=__prerender_bypass,__next_preview_data
7+
/404 /en/404 200
78
/500 /en/500.html 200
89
/_next/data/%BUILD_ID%/en.json /.netlify/functions/next_index 200
10+
/_next/data/%BUILD_ID%/en/404.json /.netlify/functions/next_404 200! Cookie=__prerender_bypass,__next_preview_data
911
/_next/data/%BUILD_ID%/en/getServerSideProps/static.json /.netlify/functions/next_getServerSideProps_static 200
1012
/_next/data/%BUILD_ID%/en/getStaticProps/1.json /.netlify/functions/next_getStaticProps_id 200! Cookie=__prerender_bypass,__next_preview_data
1113
/_next/data/%BUILD_ID%/en/getStaticProps/2.json /.netlify/functions/next_getStaticProps_id 200! Cookie=__prerender_bypass,__next_preview_data
@@ -22,13 +24,15 @@ exports[`Routing creates Netlify redirects 1`] = `
2224
/_next/data/%BUILD_ID%/en/getStaticProps/withRevalidate/withFallback/1.json /.netlify/functions/preview-next_getStaticProps_withRevalidate_withFallback_id 200! Cookie=__prerender_bypass,__next_preview_data
2325
/_next/data/%BUILD_ID%/en/getStaticProps/withRevalidate/withFallback/2.json /.netlify/functions/preview-next_getStaticProps_withRevalidate_withFallback_id 200! Cookie=__prerender_bypass,__next_preview_data
2426
/_next/data/%BUILD_ID%/es.json /.netlify/functions/next_index 200
27+
/_next/data/%BUILD_ID%/es/404.json /.netlify/functions/next_404 200! Cookie=__prerender_bypass,__next_preview_data
2528
/_next/data/%BUILD_ID%/es/getServerSideProps/static.json /.netlify/functions/next_getServerSideProps_static 200
2629
/_next/data/%BUILD_ID%/es/getStaticProps/static.json /.netlify/functions/next_getStaticProps_static 200! Cookie=__prerender_bypass,__next_preview_data
2730
/_next/data/%BUILD_ID%/es/getStaticProps/with-revalidate.json /.netlify/functions/next_getStaticProps_withrevalidate 200
2831
/_next/data/%BUILD_ID%/getServerSideProps/static.json /.netlify/functions/next_getServerSideProps_static 200
2932
/api/hello-background /.netlify/functions/next_api_hello-background 200
3033
/api/static /.netlify/functions/next_api_static 200
3134
/en /.netlify/functions/next_index 200
35+
/en/404 /.netlify/functions/next_404 200! Cookie=__prerender_bypass,__next_preview_data
3236
/en/getServerSideProps/static /.netlify/functions/next_getServerSideProps_static 200
3337
/en/getStaticProps/1 /.netlify/functions/next_getStaticProps_id 200! Cookie=__prerender_bypass,__next_preview_data
3438
/en/getStaticProps/2 /.netlify/functions/next_getStaticProps_id 200! Cookie=__prerender_bypass,__next_preview_data
@@ -45,6 +49,7 @@ exports[`Routing creates Netlify redirects 1`] = `
4549
/en/getStaticProps/withRevalidate/withFallback/1 /.netlify/functions/preview-next_getStaticProps_withRevalidate_withFallback_id 200! Cookie=__prerender_bypass,__next_preview_data
4650
/en/getStaticProps/withRevalidate/withFallback/2 /.netlify/functions/preview-next_getStaticProps_withRevalidate_withFallback_id 200! Cookie=__prerender_bypass,__next_preview_data
4751
/es /.netlify/functions/next_index 200
52+
/es/404 /.netlify/functions/next_404 200! Cookie=__prerender_bypass,__next_preview_data
4853
/es/getServerSideProps/static /.netlify/functions/next_getServerSideProps_static 200
4954
/es/getStaticProps/static /.netlify/functions/next_getStaticProps_static 200! Cookie=__prerender_bypass,__next_preview_data
5055
/es/getStaticProps/with-revalidate /.netlify/functions/next_getStaticProps_withrevalidate 200

src/tests/fixtures/pages/404.js

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
import React from 'react';
2+
3+
export default function Page404() {
4+
return (
5+
<h1>404!</h1>
6+
)
7+
}
8+
9+
export const getStaticProps = async () => {
10+
return {
11+
props: {},
12+
}
13+
}

src/tests/i18n.test.js

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -94,6 +94,12 @@ describe('SSG Pages with getStaticProps', () => {
9494
expect(existsSync(join(OUTPUT_PATH, 'getStaticProps', 'withFallback', 'my', 'path', '2.html'))).toBe(true)
9595
})
9696

97+
test('moves defaultLocale/404.html to top level for custom 404', () => {
98+
const OUTPUT_PATH = join(PROJECT_PATH, 'out_publish')
99+
100+
expect(existsSync(join(OUTPUT_PATH, '404.html'))).toBe(true)
101+
})
102+
97103
test('creates data .json file in /_next/data/ directory', () => {
98104
// Get path to data files
99105
const dirs = readdirSync(join(PROJECT_PATH, 'out_publish', '_next', 'data'))

0 commit comments

Comments
 (0)