From a6f845d1ec593eb113a1bf99d12c1e84ff262139 Mon Sep 17 00:00:00 2001 From: Daniel Roe Date: Tue, 26 Sep 2023 02:09:12 +0200 Subject: [PATCH] fix(nuxt): detect nested pages with `/index` segments (#23404) --- packages/nuxt/src/pages/utils.ts | 4 ++-- packages/nuxt/test/pages.test.ts | 22 ++++++++++++++++++++++ 2 files changed, 24 insertions(+), 2 deletions(-) diff --git a/packages/nuxt/src/pages/utils.ts b/packages/nuxt/src/pages/utils.ts index f01586c1f5..ab8866e7c9 100644 --- a/packages/nuxt/src/pages/utils.ts +++ b/packages/nuxt/src/pages/utils.ts @@ -1,6 +1,6 @@ import fs from 'node:fs' 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 { genArrayFromRaw, genDynamicImport, genImport, genSafeVariableName } from 'knitwork' import escapeRE from 'escape-string-regexp' @@ -85,7 +85,7 @@ export async function generateRoutesFromFiles (files: ScannedFile[], shouldExtra route.name += (route.name && '/') + segmentName // 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) if (child && child.children) { diff --git a/packages/nuxt/test/pages.test.ts b/packages/nuxt/test/pages.test.ts index 3a62a4b236..074d6c7366 100644 --- a/packages/nuxt/test/pages.test.ts +++ b/packages/nuxt/test/pages.test.ts @@ -437,6 +437,28 @@ describe('pages:generateRoutesFromFiles', () => { 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: '/' + } + ] } ]