Skip to content

Commit

Permalink
fix: root path / not being matched with route rules
Browse files Browse the repository at this point in the history
Fixes #362
  • Loading branch information
harlan-zw committed Sep 20, 2024
1 parent be5935e commit c1aaba9
Showing 1 changed file with 6 additions and 4 deletions.
10 changes: 6 additions & 4 deletions src/runtime/nitro/kit.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { createRouter as createRadixRouter, toRouteMatcher } from 'radix3'
import { defu } from 'defu'
import { withoutBase, withoutTrailingSlash } from 'ufo'
import { parseURL, withoutBase, withoutTrailingSlash } from 'ufo'
import type { NitroRouteRules } from 'nitropack'
import { useRuntimeConfig } from '#imports'

Expand All @@ -14,14 +14,16 @@ export function createNitroRouteRuleMatcher() {
createRadixRouter({
routes: Object.fromEntries(
Object.entries(nitro?.routeRules || {})
.map(([path, rules]) => [withoutTrailingSlash(path), rules]),
.map(([path, rules]) => [path === '/' ? path : withoutTrailingSlash(path), rules]),
),
}),
)
return (path: string) => {
return (pathOrUrl: string) => {
const path = pathOrUrl[0] === '/' ? pathOrUrl : parseURL(pathOrUrl, app.baseURL).pathname
const pathWithoutQuery = withoutQuery(path)
return defu({}, ..._routeRulesMatcher.matchAll(
// radix3 does not support trailing slashes
withoutBase(withoutTrailingSlash(withoutQuery(path)), app.baseURL),
withoutBase(pathWithoutQuery === '/' ? pathWithoutQuery : withoutTrailingSlash(pathWithoutQuery), app.baseURL),
).reverse()) as NitroRouteRules
}
}

0 comments on commit c1aaba9

Please sign in to comment.