@@ -30,6 +30,8 @@ import { LOOPBACK_HOSTS, isLocalDevRequest, isLoopbackPeer } from './dev-request
3030import { template as defaultSpaLoadingTemplate } from './templates/spa-loading-icon.ts'
3131// TODO: figure out a good way to share this
3232import { createImportProtectionPatterns } from '../../nuxt/src/core/plugins/import-protection.ts'
33+ import { normalizeRouteRulePath } from '../../nuxt/src/core/utils/route-rules.ts'
34+ import { decodeRoutePath } from '../../nuxt/src/core/utils/index.ts'
3335import { nitroSchemaTemplate } from './templates.ts'
3436// Re-export a type from the augment module rather than a bare `import './augments.ts'`
3537// side-effect import to work around bug in oxc's dts emitter which drops side-effect-only imports
@@ -396,26 +398,29 @@ export async function bundle (nuxt: Nuxt & { _nitro?: Nitro }): Promise<void> {
396398 // rou3 matches keys case-sensitively, but vue-router matches routes case-insensitively
397399 // unless `sensitive`, so an insensitive-routing rule keyed `/Admin` would never match a
398400 // folded lookup and silently lose its protections. `sensitive` can also come from
399- // `app/router.options.ts` (runtime-only), so emit both a verbatim and a folded matcher
400- // and pick at runtime.
401+ // `app/router.options.ts` (runtime-only), so emit both a decoded and a decoded+ folded
402+ // matcher and pick at runtime.
401403 const caseSensitiveRouteRules = ! ! nuxt . options . router . options . sensitive
402- const foldRouteRuleKey = ( route : string ) => caseSensitiveRouteRules || typeof route !== 'string' ? route : route . toLowerCase ( )
404+
405+ // Both matchers are compiled on every build, so collisions are reported once rather
406+ // than once per compilation pass.
407+ const warnedKeyCollisions = new Set < string > ( )
403408
404409 function getRouteRulesRouter ( fold : boolean ) {
405410 const routeRulesRouter = createRou3Router < NitroRouteRules > ( )
406411 if ( nuxt . _nitro ) {
407- const foldedKeys = new Map < string , string > ( )
412+ const normalizedKeys = new Map < string , string > ( )
408413 for ( const [ route , rules ] of Object . entries ( nuxt . _nitro . options . routeRules ) ) {
409414 if ( route === '/__nuxt_error' ) { continue }
410415 if ( validManifestKeys . every ( key => ! ( key in rules ) ) ) { continue }
411- const key = fold && typeof route === 'string' ? route . toLowerCase ( ) : route
412- if ( fold ) {
413- const existing = foldedKeys . get ( key )
414- if ( existing !== undefined && existing !== route && ! caseSensitiveRouteRules ) {
415- logger . warn ( `Route rules for \`${ existing } \` and \`${ route } \` resolve to the same path when matched case-insensitively; \`${ route } \` takes precedence. Disambiguate the keys or set \`router.options.sensitive: true\`.` )
416- }
417- foldedKeys . set ( key , route )
416+ const key = normalizeRouteRulePath ( route , fold )
417+ const existing = normalizedKeys . get ( key )
418+ // Only the matcher that will actually be used at runtime should report collisions.
419+ if ( existing !== undefined && existing !== route && fold !== caseSensitiveRouteRules && ! warnedKeyCollisions . has ( key ) ) {
420+ warnedKeyCollisions . add ( key )
421+ logger . warn ( `Route rules for \`${ existing } \` and \`${ route } \` resolve to the same path; \`${ route } \` takes precedence. Disambiguate the keys${ fold ? ' or set `router.options.sensitive: true`' : '' } .` )
418422 }
423+ normalizedKeys . set ( key , route )
419424 addRoute ( routeRulesRouter , undefined , key , rules )
420425 }
421426 }
@@ -468,9 +473,19 @@ export async function bundle (nuxt: Nuxt & { _nitro?: Nitro }): Promise<void> {
468473 `import routerOptions from '#build/router.options.mjs'` ,
469474 `const sensitiveMatcher = ${ sensitiveMatcher } ` ,
470475 foldedMatcher === sensitiveMatcher ? `const foldedMatcher = sensitiveMatcher` : `const foldedMatcher = ${ foldedMatcher } ` ,
476+ // `decodeRoutePath` has no free variables, so it can be inlined by source to keep
477+ // the runtime lookup and the build-time key normalisation from drifting apart.
478+ `const decodeRoutePath = ${ decodeRoutePath . toString ( ) } ` ,
479+ // Decoding must precede case folding, or a percent-encoded non-ASCII character
480+ // would never fold.
481+ `const normalizePath = (path, fold) => {` ,
482+ ` if (typeof path !== 'string') { return path }` ,
483+ ` const decoded = decodeRoutePath(path)` ,
484+ ` return fold ? decoded.toLowerCase() : decoded` ,
485+ `}` ,
471486 `export default (path) => routerOptions.sensitive` ,
472- ` ? defu({}, ...sensitiveMatcher('', path).map(r => r.data).reverse())` ,
473- ` : defu({}, ...foldedMatcher('', typeof path === 'string' ? path.toLowerCase() : path ).map(r => r.data).reverse())` ,
487+ ` ? defu({}, ...sensitiveMatcher('', normalizePath( path, false) ).map(r => r.data).reverse())` ,
488+ ` : defu({}, ...foldedMatcher('', normalizePath( path, true) ).map(r => r.data).reverse())` ,
474489 ] . join ( '\n' )
475490 } ,
476491 } )
@@ -560,7 +575,7 @@ export async function bundle (nuxt: Nuxt & { _nitro?: Nitro }): Promise<void> {
560575 for ( const route of nitro . _prerenderedRoutes ) {
561576 if ( ! route . error && route . route . endsWith ( payloadSuffix ) ) {
562577 const url = route . route . slice ( 0 , - payloadSuffix . length ) || '/'
563- const rules = defu ( { } , ...findAllRoutes ( routeRulesMatcher , undefined , foldRouteRuleKey ( url ) ) . reverse ( ) ) as Record < string , any >
578+ const rules = defu ( { } , ...findAllRoutes ( routeRulesMatcher , undefined , normalizeRouteRulePath ( url , ! caseSensitiveRouteRules ) ) . reverse ( ) ) as Record < string , any >
564579 if ( ! rules . prerender ) {
565580 prerenderedRoutes . add ( url )
566581 }
0 commit comments