Skip to content

Commit a5b8971

Browse files
committed
feat: support custom 404 pages in all locales
1 parent c2ff55f commit a5b8971

File tree

1 file changed

+29
-0
lines changed

1 file changed

+29
-0
lines changed

packages/runtime/src/helpers/redirects.ts

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,33 @@ const generateLocaleRedirects = ({
5656
return redirects
5757
}
5858

59+
const generateCustom404Redirects = ({ i18n }: Pick<NextConfig, 'i18n'>): NetlifyConfig['redirects'] => {
60+
const redirects: NetlifyConfig['redirects'] = []
61+
62+
if (i18n) {
63+
i18n.locales.forEach((locale) => {
64+
redirects.push({
65+
from: `/${locale}/*`,
66+
to: `/server/pages/${locale}/404.html`,
67+
status: 404,
68+
})
69+
})
70+
redirects.push({
71+
from: '/*',
72+
to: `/server/pages/${i18n.defaultLocale}/404.html`,
73+
status: 404,
74+
})
75+
} else {
76+
redirects.push({
77+
from: '/*',
78+
to: '/server/pages/404.html',
79+
status: 404,
80+
})
81+
}
82+
83+
return redirects
84+
}
85+
5986
export const generateStaticRedirects = ({
6087
netlifyConfig,
6188
nextConfig: { i18n, basePath },
@@ -245,6 +272,8 @@ export const generateRedirects = async ({
245272
netlifyConfig.redirects.push(...generateLocaleRedirects({ i18n, basePath, trailingSlash }))
246273
}
247274

275+
netlifyConfig.redirects.push(...generateCustom404Redirects({ i18n }))
276+
248277
// This is only used in prod, so dev uses `next dev` directly
249278
netlifyConfig.redirects.push(
250279
// API routes always need to be served from the regular function

0 commit comments

Comments
 (0)