Skip to content

Commit

Permalink
fix: route param parsing (nuxt-modules#2711)
Browse files Browse the repository at this point in the history
* fix: route param parsing

* test: update snapshots and tests

* test: fix test custom route

* test: fix fixture localePath usage
  • Loading branch information
BobbieGoede committed Jan 19, 2024
1 parent aa0bdba commit 8ccecb8
Show file tree
Hide file tree
Showing 6 changed files with 18 additions and 20 deletions.
2 changes: 1 addition & 1 deletion specs/fixtures/basic/pages/index.vue
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ useHead(() => ({
>go to ignoring localized pick route</NuxtLink
>
<NuxtLink id="link-category" :to="localePath('/category/test')">go to category test</NuxtLink>
<NuxtLink id="link-products" :to="localePath({ name: 'products', params: { id: 'foo' } })">
<NuxtLink id="link-products" :to="localePath({ name: 'products-id', params: { id: 'foo' } })">
go to product foo
</NuxtLink>
<NuxtLink id="link-history" :to="localePath({ name: 'history' })">go to history</NuxtLink>
Expand Down
9 changes: 0 additions & 9 deletions specs/fixtures/basic/pages/products.vue
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,3 @@
<NuxtPage />
</div>
</template>

<script setup>
defineI18nRoute({
paths: {
en: '/products/:id',
fr: '/produits/:id'
}
})
</script>
9 changes: 9 additions & 0 deletions specs/fixtures/basic/pages/products/[id].vue
Original file line number Diff line number Diff line change
Expand Up @@ -3,3 +3,12 @@
<h1>This is a product page</h1>
</div>
</template>

<script setup>
defineI18nRoute({
paths: {
en: '/products/[id]',
fr: '/produits/[id]'
}
})
</script>
8 changes: 3 additions & 5 deletions src/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -392,7 +392,7 @@ export function parseSegment(segment: string) {
if (c === '[' && state === SegmentParserState.dynamic) {
state = SegmentParserState.optional
}
if (c === ']' && (state !== SegmentParserState.optional || buffer[buffer.length - 1] === ']')) {
if (c === ']' && (state !== SegmentParserState.optional || segment[i - 1] === ']')) {
if (!buffer) {
throw new Error('Empty param')
} else {
Expand All @@ -402,7 +402,6 @@ export function parseSegment(segment: string) {
} else if (PARAM_CHAR_RE.test(c)) {
buffer += c
} else {
// eslint-disable-next-line no-console
// console.debug(`[pages]Ignored character "${c}" while building param "${buffer}" from "segment"`)
}
break
Expand Down Expand Up @@ -543,16 +542,15 @@ export const mergeI18nModules = async (options: NuxtI18nOptions, nuxt: Nuxt) =>

export function getRoutePath(tokens: SegmentToken[]): string {
return tokens.reduce((path, token) => {
// prettier-ignore
return (
path +
(token.type === SegmentTokenType.optional
? `:${token.value}?`
: token.type === SegmentTokenType.dynamic
? `:${token.value}`
? `:${token.value}()`
: token.type === SegmentTokenType.catchall
? `:${token.value}(.*)*`
: encodePath(token.value))
: encodePath(token.value).replace(/:/g, '\\:'))
)
}, '/')
}
Expand Down
8 changes: 4 additions & 4 deletions test/pages/__snapshots__/custom_route.test.ts.snap
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,7 @@ exports[`Module configuration > dynamic parameters 1`] = `
"children": [],
"file": "/path/to/nuxt-app/pages/blog/[date]/[slug].vue",
"name": "blog-date-slug___ja",
"path": "/ja/blog/tech/:date/:slug",
"path": "/ja/blog/tech/:date()/:slug()",
},
{
"children": [],
Expand Down Expand Up @@ -270,17 +270,17 @@ exports[`Page components > dynamic route 1`] = `
{
"children": [],
"name": "articles-name___en",
"path": "/articles/:name",
"path": "/articles/:name()",
},
{
"children": [],
"name": "articles-name___ja",
"path": "/ja/%E8%A8%98%E4%BA%8B/:name",
"path": "/ja/%E8%A8%98%E4%BA%8B/:name()",
},
{
"children": [],
"name": "articles-name___fr",
"path": "/fr/articles/:name",
"path": "/fr/articles/:name()",
},
]
`;
Expand Down
2 changes: 1 addition & 1 deletion test/utils.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -197,5 +197,5 @@ test('parseSegment', () => {

test('getRoutePath', () => {
const tokens = parseSegment('[foo]_[bar]:[...buz]_buz_[[qux]]')
expect(getRoutePath(tokens)).toBe(`/:foo_:bar::buz(.*)*_buz_:qux?`)
expect(getRoutePath(tokens)).toBe(`/:foo()_:bar()\\::buz(.*)*_buz_:qux?`)
})

0 comments on commit 8ccecb8

Please sign in to comment.