Skip to content

Commit ac23f0d

Browse files
committed
i18n rewrite to default locale if path is not locale prefixed
1 parent d4d4e59 commit ac23f0d

File tree

1 file changed

+82
-28
lines changed

1 file changed

+82
-28
lines changed

src/adapter/build/routing.ts

Lines changed: 82 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -18,34 +18,6 @@ import {
1818
} from './constants.js'
1919
import type { NetlifyAdapterContext, OnBuildCompleteContext } from './types.js'
2020

21-
function fixDestinationGroupReplacements(destination: string, sourceRegex: string): string {
22-
// convert $nxtPslug to $<nxtPslug> etc
23-
24-
// find all capturing groups in sourceRegex
25-
const segments = [...sourceRegex.matchAll(/\(\?<(?<segment_name>[^>]+)>/g)]
26-
27-
let adjustedDestination = destination
28-
for (const segment of segments) {
29-
if (segment.groups?.segment_name) {
30-
adjustedDestination = adjustedDestination.replaceAll(
31-
`$${segment.groups.segment_name}`,
32-
`$<${segment.groups.segment_name}>`,
33-
)
34-
}
35-
}
36-
37-
if (adjustedDestination !== destination) {
38-
console.log('fixing named captured group replacement', {
39-
sourceRegex,
40-
segments,
41-
destination,
42-
adjustedDestination,
43-
})
44-
}
45-
46-
return adjustedDestination
47-
}
48-
4921
export function convertRedirectToRoutingRule(
5022
redirect: Pick<
5123
OnBuildCompleteContext['routes']['redirects'][number],
@@ -256,6 +228,88 @@ export async function generateRoutingRules(
256228
...normalizeNextData, // originally: // normalize _next/data if middleware + pages
257229

258230
// i18n prefixing routes
231+
...(nextAdapterContext.config.i18n
232+
? [
233+
// i18n domain handling - not implementing for now
234+
// Handle auto-adding current default locale to path based on $wildcard
235+
// This is split into two rules to avoid matching the `/index` route as it causes issues with trailing slash redirect
236+
// {
237+
// description: 'stuff1',
238+
// match: {
239+
// path: `^${join(
240+
// '/',
241+
// nextAdapterContext.config.basePath,
242+
// '/',
243+
// )}(?!(?:_next/.*|${nextAdapterContext.config.i18n.locales
244+
// .map((locale) => escapeStringRegexp(locale))
245+
// .join('|')})(?:/.*|$))$`,
246+
// },
247+
// apply: {
248+
// type: 'rewrite',
249+
// // we aren't able to ensure trailing slash mode here
250+
// // so ensure this comes after the trailing slash redirect
251+
// destination: `${
252+
// nextAdapterContext.config.basePath && nextAdapterContext.config.basePath !== '/'
253+
// ? join('/', nextAdapterContext.config.basePath)
254+
// : ''
255+
// }$wildcard${nextAdapterContext.config.trailingSlash ? '/' : ''}`,
256+
// },
257+
// } satisfies RoutingRuleRewrite,
258+
259+
// Handle redirecting to locale paths based on NEXT_LOCALE cookie or Accept-Language header
260+
// eslint-disable-next-line no-negated-condition
261+
...(nextAdapterContext.config.i18n.localeDetection !== false
262+
? [
263+
// TODO: implement locale detection
264+
// {
265+
// description: 'Detect locale on root path, redirect and set cookie',
266+
// match: {
267+
// path: '/',
268+
// },
269+
// apply: {
270+
// type: 'apply',
271+
// },
272+
// } satisfies RoutingRuleApply,
273+
]
274+
: []),
275+
276+
{
277+
description: 'Prefix default locale to index',
278+
match: {
279+
path: `^${join('/', nextAdapterContext.config.basePath)}$`,
280+
},
281+
apply: {
282+
type: 'rewrite',
283+
destination: join(
284+
'/',
285+
nextAdapterContext.config.basePath,
286+
nextAdapterContext.config.i18n.defaultLocale,
287+
),
288+
},
289+
} satisfies RoutingRuleRewrite,
290+
{
291+
description: 'Auto-prefix non-locale path with default locale',
292+
match: {
293+
path: `^${join(
294+
'/',
295+
nextAdapterContext.config.basePath,
296+
'/',
297+
)}(?!(?:_next/.*|${nextAdapterContext.config.i18n.locales
298+
.map((locale) => escapeStringRegexp(locale))
299+
.join('|')})(?:/.*|$))(.*)$`,
300+
},
301+
apply: {
302+
type: 'rewrite',
303+
destination: join(
304+
'/',
305+
nextAdapterContext.config.basePath,
306+
nextAdapterContext.config.i18n.defaultLocale,
307+
'$1',
308+
),
309+
},
310+
} satisfies RoutingRuleRewrite,
311+
]
312+
: []),
259313

260314
// ...convertedHeaders,
261315

0 commit comments

Comments
 (0)