forked from nodejs/nodejs.org
-
Notifications
You must be signed in to change notification settings - Fork 0
/
next.rewrites.mjs
74 lines (67 loc) · 3.04 KB
/
next.rewrites.mjs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
'use strict';
import { ENABLE_WEBSITE_REDESIGN } from './next.constants.mjs';
import { siteRedirects } from './next.json.mjs';
import { availableLocaleCodes } from './next.locales.mjs';
// This allows us to prefix redirects with all available locale codes so that redirects are not bound to a single locale
// This also transforms the locale itself as a matching group that can be used for rewrites
// This match group also has an empty string match for the lack of locales, for example
// Example: /:locale(ar/|ca/|de/|en/|es/|fa/|fr/|)about/security
// Would match /ar/about/security, /ar/about/security/ for every language code (replace "ar") and
// it would also match /about/security (without any language prefix)
const localesMatch = `/:locale(${availableLocaleCodes.join('|')}|)?`;
/**
* These are external redirects that happen before we check dynamic routes and rewrites
* These are sourced originally from https://github.com/nodejs/build/blob/main/ansible/www-standalone/resources/config/nodejs.org?plain=1
* and were then converted to Next.js rewrites. Note that only relevant rewrites were added, and some were modified to match Next.js's syntax
*
* @return {Promise<import('next').NextConfig['redirects']>}
*/
const redirects = async () => {
return siteRedirects.external.map(({ source, destination }) => ({
source: source.replace('/:locale', localesMatch),
// We prevent permanent redirects as in general the redirects are safeguards
// of legacy or old pages or pages that moved, and in general we don't want permanent redirects
permanent: false,
destination,
}));
};
/**
* These are rewrites that happen before we check dynamic routes and after we check regular redirects
* These should be used either for internal or external rewrite rules (like NGINX, for example)
* These are sourced originally from https://github.com/nodejs/build/blob/main/ansible/www-standalone/resources/config/nodejs.org?plain=1
* and were then converted to Next.js rewrites. Note that only relevant rewrites were added, and some were modified to match Next.js's syntax
*
* @return {Promise<import('next').NextConfig['rewrites']>}
*/
const rewrites = async () => {
const mappedRewrites = siteRedirects.internal.map(
({ source, destination }) => ({
source: source.replace('/:locale', localesMatch),
destination,
})
);
// This allows us to remap legacy website URLs to the temporary redesign ones
// @todo: remove this once website redesign is done
if (ENABLE_WEBSITE_REDESIGN) {
mappedRewrites.push(
{
source: localesMatch,
destination: '/:locale/new-design',
},
{
source: '/:locale/download',
destination: '/:locale/new-design/download',
},
{
source: '/:locale/download/:path',
destination: '/:locale/new-design/download/:path',
},
{
source: '/:locale/download/:path/:version',
destination: '/:locale/new-design/download/:path/:version',
}
);
}
return { afterFiles: mappedRewrites };
};
export { rewrites, redirects };