From d16fb6f4a6f6a5a58c28afa58ee553e8f15a9f9b Mon Sep 17 00:00:00 2001 From: markthree <1801982702@qq.com> Date: Tue, 26 Dec 2023 19:58:09 +0800 Subject: [PATCH 1/2] fix(unplugin-vue-router): skip top-level route layout for routing group, close #134 --- .../unplugin-vue-router/src/pages/second.vue | 10 ------- .../unplugin-vue-router/typed-router.d.ts | 1 - src/RouteLayout.ts | 25 +++++++++++------ src/clientSide.ts | 27 ++++++++++++------- 4 files changed, 35 insertions(+), 28 deletions(-) delete mode 100644 examples/unplugin-vue-router/src/pages/second.vue diff --git a/examples/unplugin-vue-router/src/pages/second.vue b/examples/unplugin-vue-router/src/pages/second.vue deleted file mode 100644 index b588dbd..0000000 --- a/examples/unplugin-vue-router/src/pages/second.vue +++ /dev/null @@ -1,10 +0,0 @@ - - -{ - meta: { - layout: false - } -} - diff --git a/examples/unplugin-vue-router/typed-router.d.ts b/examples/unplugin-vue-router/typed-router.d.ts index 9599dcb..d3f703e 100644 --- a/examples/unplugin-vue-router/typed-router.d.ts +++ b/examples/unplugin-vue-router/typed-router.d.ts @@ -51,7 +51,6 @@ declare module 'vue-router/auto/routes' { '/news/': RouteRecordInfo<'/news/', '/news', Record, Record>, 'named-news-page': RouteRecordInfo<'named-news-page', '/news/Today', Record, Record>, '/nolayout': RouteRecordInfo<'/nolayout', '/nolayout', Record, Record>, - '/second': RouteRecordInfo<'/second', '/second', Record, Record>, '/second/': RouteRecordInfo<'/second/', '/second', Record, Record>, '/sublayout': RouteRecordInfo<'/sublayout', '/sublayout', Record, Record>, } diff --git a/src/RouteLayout.ts b/src/RouteLayout.ts index c571262..e039bdd 100644 --- a/src/RouteLayout.ts +++ b/src/RouteLayout.ts @@ -1,4 +1,4 @@ -import { ResolvedOptions } from './types' +import type { ResolvedOptions } from './types' function getClientCode(importCode: string, options: ResolvedOptions) { const code = ` @@ -18,13 +18,22 @@ export function setupLayouts(routes) { route.children = deepSetupLayout(route.children, false) } - if (top && route.meta?.layout !== false) { - return { - path: route.path, - component: layouts[route.meta?.layout || '${options.defaultLayout}'], - children: [ {...route, path: ''} ], - meta: { - isLayout: true + if (top) { + // unplugin-vue-router adds a top-level route to the routing group, which we should skip. + const skipLayout = !route.component && route.children?.find(r => (r.path === '' || r.path === '/') && r.meta?.isLayout) + + if (skipLayout) { + return route + } + + if (route.meta?.layout !== false) { + return { + path: route.path, + component: layouts[route.meta?.layout || '${options.defaultLayout}'], + children: [ {...route, path: ''} ], + meta: { + isLayout: true + } } } } diff --git a/src/clientSide.ts b/src/clientSide.ts index f4fd9ea..0967d90 100644 --- a/src/clientSide.ts +++ b/src/clientSide.ts @@ -1,4 +1,4 @@ -import { posix } from 'path' +import { posix } from 'node:path' function normalizePath(path: string) { path = path.startsWith('/') ? path : `/${path}` @@ -55,14 +55,23 @@ export async function createVirtualModuleCode( if (route.children?.length > 0) { route.children = deepSetupLayout(route.children, false) } - - if (top && route.meta?.layout !== false) { - return { - path: route.path, - component: layouts[route.meta?.layout || '${defaultLayout}'], - children: [ {...route, path: ''} ], - meta: { - isLayout: true + + if (top) { + // unplugin-vue-router adds a top-level route to the routing group, which we should skip. + const skipLayout = !route.component && route.children?.find(r => (r.path === '' || r.path === '/') && r.meta?.isLayout) + + if (skipLayout) { + return route + } + + if (route.meta?.layout !== false) { + return { + path: route.path, + component: layouts[route.meta?.layout || '${defaultLayout}'], + children: [ {...route, path: ''} ], + meta: { + isLayout: true + } } } } From 209a3b62b1a6367262bc935ebf0f2061c8bcee4b Mon Sep 17 00:00:00 2001 From: markthree <1801982702@qq.com> Date: Wed, 27 Dec 2023 16:11:19 +0800 Subject: [PATCH 2/2] fix: avoiding misses #97 --- src/RouteLayout.ts | 2 +- src/clientSide.ts | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/src/RouteLayout.ts b/src/RouteLayout.ts index e039bdd..9ed1766 100644 --- a/src/RouteLayout.ts +++ b/src/RouteLayout.ts @@ -30,7 +30,7 @@ export function setupLayouts(routes) { return { path: route.path, component: layouts[route.meta?.layout || '${options.defaultLayout}'], - children: [ {...route, path: ''} ], + children: route.path === '/' ? [route] : [{...route, path: ''}], meta: { isLayout: true } diff --git a/src/clientSide.ts b/src/clientSide.ts index 0967d90..ea45721 100644 --- a/src/clientSide.ts +++ b/src/clientSide.ts @@ -68,7 +68,7 @@ export async function createVirtualModuleCode( return { path: route.path, component: layouts[route.meta?.layout || '${defaultLayout}'], - children: [ {...route, path: ''} ], + children: route.path === '/' ? [route] : [{...route, path: ''}], meta: { isLayout: true }