Skip to content
New issue

Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? # to your account

fix(unplugin-vue-router)!: skip top-level route layout for index route children #137

Merged
merged 2 commits into from
Dec 27, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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