fix(nuxt): detect nested pages with /index segments (#23404)

This commit is contained in:
Daniel Roe 2023-09-26 02:09:12 +02:00 committed by GitHub
parent 0844735187
commit a6f845d1ec
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 24 additions and 2 deletions

View File

@ -1,6 +1,6 @@
import fs from 'node:fs' import fs from 'node:fs'
import { extname, normalize, relative, resolve } from 'pathe' import { extname, normalize, relative, resolve } from 'pathe'
import { encodePath } from 'ufo' import { encodePath, joinURL, withLeadingSlash } from 'ufo'
import { logger, resolveFiles, useNuxt } from '@nuxt/kit' import { logger, resolveFiles, useNuxt } from '@nuxt/kit'
import { genArrayFromRaw, genDynamicImport, genImport, genSafeVariableName } from 'knitwork' import { genArrayFromRaw, genDynamicImport, genImport, genSafeVariableName } from 'knitwork'
import escapeRE from 'escape-string-regexp' import escapeRE from 'escape-string-regexp'
@ -85,7 +85,7 @@ export async function generateRoutesFromFiles (files: ScannedFile[], shouldExtra
route.name += (route.name && '/') + segmentName route.name += (route.name && '/') + segmentName
// ex: parent.vue + parent/child.vue // ex: parent.vue + parent/child.vue
const path = route.path + getRoutePath(tokens).replace(/\/index$/, '/') const path = withLeadingSlash(joinURL(route.path, getRoutePath(tokens).replace(/\/index$/, '/')))
const child = parent.find(parentRoute => parentRoute.name === route.name && parentRoute.path === path) const child = parent.find(parentRoute => parentRoute.name === route.name && parentRoute.path === path)
if (child && child.children) { if (child && child.children) {

View File

@ -437,6 +437,28 @@ describe('pages:generateRoutesFromFiles', () => {
path: '/wrapper-expose/other' path: '/wrapper-expose/other'
} }
] ]
},
{
description: 'should handle trailing slashes with index routes',
files: [
{ path: `${pagesDir}/index/index.vue` },
{ path: `${pagesDir}/index/index/all.vue` }
],
output: [
{
children: [
{
children: [],
file: `${pagesDir}/index/index/all.vue`,
name: 'index-index-all',
path: 'all'
}
],
file: `${pagesDir}/index/index.vue`,
name: 'index',
path: '/'
}
]
} }
] ]