The Node.js® Website
at main 2.4 kB view raw
1'use strict'; 2 3import { siteRedirects } from './next.json.mjs'; 4import { availableLocaleCodes } from './next.locales.mjs'; 5 6// This allows us to prefix redirects with all available locale codes so that redirects are not bound to a single locale 7// This also transforms the locale itself as a matching group that can be used for rewrites 8// This match group also has an empty string match for the lack of locales, for example 9// Example: /:locale(ar/|ca/|de/|en/|es/|fa/|fr/|)about/security 10// Would match /ar/about/security, /ar/about/security/ for every language code (replace "ar") and 11// it would also match /about/security (without any language prefix) 12const localesMatch = `/:locale(${availableLocaleCodes.join('|')}|)?`; 13 14/** 15 * These are external redirects that happen before we check dynamic routes and rewrites 16 * These are sourced originally from https://github.com/nodejs/build/blob/main/ansible/www-standalone/resources/config/nodejs.org?plain=1 17 * 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 18 * 19 * @return {Promise<import('next').NextConfig['redirects']>} 20 */ 21const redirects = async () => { 22 return siteRedirects.external.map(({ source, destination }) => ({ 23 source: source.replace('/:locale', localesMatch), 24 // We prevent permanent redirects as in general the redirects are safeguards 25 // of legacy or old pages or pages that moved, and in general we don't want permanent redirects 26 permanent: false, 27 destination, 28 })); 29}; 30 31/** 32 * These are rewrites that happen before we check dynamic routes and after we check regular redirects 33 * These should be used either for internal or external rewrite rules (like NGINX, for example) 34 * These are sourced originally from https://github.com/nodejs/build/blob/main/ansible/www-standalone/resources/config/nodejs.org?plain=1 35 * 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 36 * 37 * @return {Promise<import('next').NextConfig['rewrites']>} 38 */ 39const rewrites = async () => { 40 const mappedRewrites = siteRedirects.internal.map( 41 ({ source, destination }) => ({ 42 source: source.replace('/:locale', localesMatch), 43 destination, 44 }) 45 ); 46 47 return { afterFiles: mappedRewrites }; 48}; 49 50export { rewrites, redirects };