Skip to content

Commit

Permalink
fix(unplugin-vue-router)!: skip top-level route layout for index rout…
Browse files Browse the repository at this point in the history
…e children (#137)

* fix(unplugin-vue-router): skip top-level route layout for routing group, close #134

* fix: avoiding misses #97
  • Loading branch information
markthree authored Dec 27, 2023
1 parent c861280 commit bb751cb
Show file tree
Hide file tree
Showing 4 changed files with 35 additions and 28 deletions.
10 changes: 0 additions & 10 deletions examples/unplugin-vue-router/src/pages/second.vue

This file was deleted.

1 change: 0 additions & 1 deletion examples/unplugin-vue-router/typed-router.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,6 @@ declare module 'vue-router/auto/routes' {
'/news/': RouteRecordInfo<'/news/', '/news', Record<never, never>, Record<never, never>>,
'named-news-page': RouteRecordInfo<'named-news-page', '/news/Today', Record<never, never>, Record<never, never>>,
'/nolayout': RouteRecordInfo<'/nolayout', '/nolayout', Record<never, never>, Record<never, never>>,
'/second': RouteRecordInfo<'/second', '/second', Record<never, never>, Record<never, never>>,
'/second/': RouteRecordInfo<'/second/', '/second', Record<never, never>, Record<never, never>>,
'/sublayout': RouteRecordInfo<'/sublayout', '/sublayout', Record<never, never>, Record<never, never>>,
}
Expand Down
25 changes: 17 additions & 8 deletions src/RouteLayout.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { ResolvedOptions } from './types'
import type { ResolvedOptions } from './types'

function getClientCode(importCode: string, options: ResolvedOptions) {
const code = `
Expand All @@ -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 === '/' ? [route] : [{...route, path: ''}],
meta: {
isLayout: true
}
}
}
}
Expand Down
27 changes: 18 additions & 9 deletions src/clientSide.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { posix } from 'path'
import { posix } from 'node:path'

function normalizePath(path: string) {
path = path.startsWith('/') ? path : `/${path}`
Expand Down Expand Up @@ -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 === '/' ? [route] : [{...route, path: ''}],
meta: {
isLayout: true
}
}
}
}
Expand Down

0 comments on commit bb751cb

Please sign in to comment.