Skip to content

Commit

Permalink
fix: custom paths pages config should use route name as key (#3286)
Browse files Browse the repository at this point in the history
  • Loading branch information
BobbieGoede authored Dec 28, 2024
1 parent 757195a commit 3ba779c
Show file tree
Hide file tree
Showing 5 changed files with 250 additions and 18 deletions.
6 changes: 5 additions & 1 deletion src/pages.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ export type AnalyzedNuxtPageMeta = {
* Analyzed path used to retrieve configured custom paths
*/
path: string
name?: string
}

export type NuxtPageAnalyzeContext = {
Expand Down Expand Up @@ -233,6 +234,8 @@ export function analyzeNuxtPages(ctx: NuxtPageAnalyzeContext, pages?: NuxtPage[]

ctx.pages.set(page, {
path: analyzePagePath(filePath, ctx.stack.length),
// if route has an index child the parent will not have a name
name: page.name ?? page.children?.find(x => x.path.endsWith('/index'))?.name,
inRoot: ctx.stack.length === 0
})

Expand Down Expand Up @@ -296,7 +299,8 @@ function getRouteOptionsFromPages(
return options
}

const pageOptions = pageMeta.path ? pages[pageMeta.path] : undefined
const valueByName = pageMeta.name ? pages[pageMeta.name] : undefined
const pageOptions = valueByName ?? pages[pageMeta.path]

// routing disabled
if (pageOptions === false) {
Expand Down
5 changes: 5 additions & 0 deletions src/routing.ts
Original file line number Diff line number Diff line change
Expand Up @@ -220,6 +220,11 @@ export function localizeRoutes(routes: NuxtPage[], options: LocalizeRoutesParams
// remove parent path from child route
if (parentLocalized != null) {
localized.path = localized.path.replace(parentLocalized.path + '/', '')

// handle parent/index
if (localized.path === parentLocalized.path) {
localized.path = ''
}
}

// localize child routes if set
Expand Down
95 changes: 95 additions & 0 deletions test/pages/__snapshots__/custom_route.test.ts.snap
Original file line number Diff line number Diff line change
Expand Up @@ -318,3 +318,98 @@ exports[`Page components > with definePageMeta 1`] = `
},
]
`;

exports[`pages config using route name 1`] = `
[
{
"children": [
{
"children": [],
"file": "/path/to/1649/pages/account/addresses.vue",
"name": "account-addresses___en",
"path": "addresses",
},
{
"children": [],
"file": "/path/to/1649/pages/account/index.vue",
"name": "account___en",
"path": "",
},
{
"children": [],
"file": "/path/to/1649/pages/account/profile.vue",
"name": "account-profile___en",
"path": "profile",
},
],
"file": "/path/to/1649/pages/account.vue",
"path": "/account",
},
{
"children": [
{
"children": [],
"file": "/path/to/1649/pages/account/addresses.vue",
"name": "account-addresses___ja",
"path": "addresses",
},
{
"children": [],
"file": "/path/to/1649/pages/account/index.vue",
"name": "account___ja",
"path": "",
},
{
"children": [],
"file": "/path/to/1649/pages/account/profile.vue",
"name": "account-profile___ja",
"path": "profile",
},
],
"file": "/path/to/1649/pages/account.vue",
"path": "/ja/account",
},
{
"children": [
{
"children": [],
"file": "/path/to/1649/pages/account/addresses.vue",
"name": "account-addresses___fr",
"path": "adresses",
},
{
"children": [],
"file": "/path/to/1649/pages/account/index.vue",
"name": "account___fr",
"path": "",
},
{
"children": [],
"file": "/path/to/1649/pages/account/profile.vue",
"name": "account-profile___fr",
"path": "profil",
},
],
"file": "/path/to/1649/pages/account.vue",
"path": "/fr/compte",
},
{
"children": [],
"file": "/path/to/1649/pages/index.vue",
"name": "index___en",
"path": "/",
},
{
"children": [],
"file": "/path/to/1649/pages/index.vue",
"name": "index___ja",
"path": "/ja",
},
{
"children": [],
"file": "/path/to/1649/pages/index.vue",
"name": "index___fr",
"path": "/fr",
},
]
`;
96 changes: 79 additions & 17 deletions test/pages/analyze_nuxt_page.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -112,21 +112,83 @@ test('analyzeNuxtPages', () => {
analyzeNuxtPages(ctx, pages)

expect(ctx.stack.length).toBe(0)
expect([...ctx.pages.values()]).toEqual([
{ inRoot: true, path: '[...catch]' },
{ inRoot: true, path: 'account' },
{ inRoot: false, path: 'account/addresses' },
{ inRoot: false, path: 'account/foo[id]' },
{ inRoot: false, path: 'account/index' },
{ inRoot: false, path: 'account/profile' },
{ inRoot: true, path: 'blog/[date]/[slug]' },
{ inRoot: true, path: 'foo' },
{ inRoot: false, path: 'foo/bar' },
{ inRoot: false, path: 'foo/bar' },
{ inRoot: false, path: 'foo/bar/buz' },
{ inRoot: false, path: 'foo/hoge/[piyo]' },
{ inRoot: false, path: 'foo/qux' },
{ inRoot: true, path: 'index' },
{ inRoot: true, path: 'users/[[profile]]' }
])
expect([...ctx.pages.values()]).toMatchInlineSnapshot(`
[
{
"inRoot": true,
"name": "catch",
"path": "[...catch]",
},
{
"inRoot": true,
"name": undefined,
"path": "account",
},
{
"inRoot": false,
"name": "account-addresses",
"path": "account/addresses",
},
{
"inRoot": false,
"name": "account-fooid",
"path": "account/foo[id]",
},
{
"inRoot": false,
"name": "account",
"path": "account/index",
},
{
"inRoot": false,
"name": "account-profile",
"path": "account/profile",
},
{
"inRoot": true,
"name": "blog-date-slug",
"path": "blog/[date]/[slug]",
},
{
"inRoot": true,
"name": "foo",
"path": "foo",
},
{
"inRoot": false,
"name": "foo-bar",
"path": "foo/bar",
},
{
"inRoot": false,
"name": "foo-bar",
"path": "foo/bar",
},
{
"inRoot": false,
"name": "foo-bar-buz",
"path": "foo/bar/buz",
},
{
"inRoot": false,
"name": "foo-hoge-piyo",
"path": "foo/hoge/[piyo]",
},
{
"inRoot": false,
"name": "foo-qux",
"path": "foo/qux",
},
{
"inRoot": true,
"name": "index",
"path": "index",
},
{
"inRoot": true,
"name": "users-profile",
"path": "users/[[profile]]",
},
]
`)
})
66 changes: 66 additions & 0 deletions test/pages/custom_route.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -269,3 +269,69 @@ test('#1649', () => {

expect(localizedPages).toMatchSnapshot()
})

test('pages config using route name', () => {
const pages = [
{
path: '/account',
file: '/path/to/1649/pages/account.vue',
children: [
{
name: 'account-addresses',
path: 'addresses',
file: '/path/to/1649/pages/account/addresses.vue',
children: []
},
{
name: 'account',
path: '',
file: '/path/to/1649/pages/account/index.vue',
children: []
},
{
name: 'account-profile',
path: 'profile',
file: '/path/to/1649/pages/account/profile.vue',
children: []
}
]
},
{
name: 'index',
path: '/',
file: '/path/to/1649/pages/index.vue',
children: []
}
]

const options = getNuxtOptions({
account: {
fr: '/compte'
},
'account-profile': {
fr: '/compte/profil'
},
'account-addresses': {
fr: '/compte/adresses'
}
})

vi.spyOn(fs, 'readFileSync').mockReturnValue('')

const srcDir = '/path/to/1649'
const pagesDir = 'pages'
const ctx: NuxtPageAnalyzeContext = {
stack: [],
srcDir,
pagesDir,
pages: new Map<NuxtPage, AnalyzedNuxtPageMeta>()
}

analyzeNuxtPages(ctx, pages)
const localizedPages = localizeRoutes(pages, {
...options,
includeUnprefixedFallback: false,
optionsResolver: getRouteOptionsResolver(ctx, options as Required<NuxtI18nOptions>)
} as Parameters<typeof localizeRoutes>[1])
expect(localizedPages).toMatchSnapshot()
})

0 comments on commit 3ba779c

Please sign in to comment.