Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: custom routes named config #2408

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions src/utils.ts
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ah, we have a diff happening between nuxt ...

We want to use parseSegment and getRoutePath that are exported by nuxt/kit.
We need to sync the implementation.

Original file line number Diff line number Diff line change
Expand Up @@ -352,7 +352,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 Down Expand Up @@ -508,7 +508,7 @@ export function getRoutePath(tokens: SegmentToken[]): string {
(token.type === SegmentTokenType.optional
? `:${token.value}?`
: token.type === SegmentTokenType.dynamic
? `:${token.value}`
? `:${token.value}()`
: token.type === SegmentTokenType.catchall
? `:${token.value}(.*)*`
: encodePath(token.value))
Expand Down
12 changes: 6 additions & 6 deletions test/pages/__snapshots__/custom_route.test.ts.snap
Original file line number Diff line number Diff line change
Expand Up @@ -101,19 +101,19 @@ exports[`Module configuration > dynamic parameters 1`] = `
"children": [],
"file": "/path/to/nuxt-app/pages/blog/[date]/[slug].vue",
"name": "blog-date-slug___en",
"path": "/blog/:date/:slug",
"path": "/blog/:date()/:slug()",
},
{
"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": [],
"file": "/path/to/nuxt-app/pages/blog/[date]/[slug].vue",
"name": "blog-date-slug___fr",
"path": "/fr/blog/:date/:slug",
"path": "/fr/blog/:date()/:slug()",
},
]
`;
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
4 changes: 2 additions & 2 deletions test/pages/custom_route.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,7 @@ describe.each([
pages: [
{
name: 'blog-date-slug',
path: '/blog/:date/:slug',
path: '/blog/:date()/:slug()',
file: '/path/to/nuxt-app/pages/blog/[date]/[slug].vue',
children: []
}
Expand Down Expand Up @@ -154,7 +154,7 @@ describe.each([
pages: [
{
name: 'articles-name',
path: '/articles/:name',
path: '/articles/:name()',
file: resolve(__dirname, '../fixtures/custom_route/dynamic/pages/articles/[name].vue'),
children: []
}
Expand Down
9 changes: 5 additions & 4 deletions test/utils.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -93,19 +93,20 @@ test('resolveLocales', async () => {
})

test('parseSegment', () => {
const tokens = parseSegment('[foo]_[bar]:[...buz]_buz_[[qux]]')
const tokens = parseSegment('[foo]_[bar]:[...buz]_buz_[[qux]]__smth')
expect(tokens).toEqual([
{ type: 1, value: 'foo' },
{ type: 0, value: '_' },
{ type: 1, value: 'bar' },
{ type: 0, value: ':' },
{ type: 3, value: 'buz' },
{ type: 0, value: '_buz_' },
{ type: 2, value: 'qux' }
{ type: 2, value: 'qux' },
{ type: 0, value: '__smth' }
])
})

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