The Node.js® Website

meta: reduce middleware invocations by only running on /

Changed files
+23 -12
+10 -5
middleware.ts
··· 8 8 9 9 // Used when no locale matches 10 10 defaultLocale: defaultLocale.code, 11 + 12 + // Always use a Locale as a prefix for routing 13 + localePrefix: 'always', 14 + 15 + // We already have our own way of providing alternate links 16 + // generated on `next.dynamic.mjs` 17 + alternateLinks: false, 11 18 }); 12 19 13 - export const config = { 14 - // Note.: This needs to be updated when activating more locales 15 - // Format: '/(locale1|locale2|locale3|...)/:path*' 16 - matcher: ['/', '/(en)/:path*'], 17 - }; 20 + // We only want the middleware to run on the `/` route 21 + // to redirect users to their preferred locale 22 + export const config = { matcher: ['/'] };
+13 -7
next.dynamic.mjs
··· 218 218 : siteConfig.title; 219 219 220 220 pageMetadata.twitter.title = pageMetadata.title; 221 - pageMetadata.alternates.canonical = `${baseUrlAndPath}/${locale}/${path}`; 222 221 223 - pageMetadata.alternates.languages[ 224 - 'x-default' 225 - ] = `${baseUrlAndPath}/${defaultLocale.code}/${path}`; 222 + const getUrlForPathname = (l, p) => 223 + `${baseUrlAndPath}/${l}${p ? `/${p}` : ''}`; 224 + 225 + pageMetadata.alternates.canonical = getUrlForPathname(locale, path); 226 + 227 + pageMetadata.alternates.languages['x-default'] = getUrlForPathname( 228 + defaultLocale.code, 229 + path 230 + ); 226 231 227 232 availableLocaleCodes.forEach(currentLocale => { 228 - pageMetadata.alternates.languages[ 229 - currentLocale 230 - ] = `${baseUrlAndPath}/${currentLocale}/${path}`; 233 + pageMetadata.alternates.languages[currentLocale] = getUrlForPathname( 234 + currentLocale, 235 + path 236 + ); 231 237 }); 232 238 233 239 return pageMetadata;