Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit ba6a413

Browse files
committed
fix(nuxt): handle undefined paths in resolveTrailingSlashBehavior
1 parent 3fc4231 commit ba6a413

File tree

2 files changed

+17
-9
lines changed

2 files changed

+17
-9
lines changed

packages/nuxt/src/app/components/nuxt-link.ts

Lines changed: 13 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -88,7 +88,7 @@ export interface NuxtLinkProps extends Omit<RouterLinkProps, 'to'> {
8888
noPrefetch?: boolean
8989
}
9090

91-
/*@__NO_SIDE_EFFECTS__*/
91+
/*@__NO_SIDE_EFFECTS__*/
9292
export function defineNuxtLink (options: NuxtLinkOptions) {
9393
const componentName = options.componentName || 'NuxtLink'
9494

@@ -100,7 +100,8 @@ export function defineNuxtLink (options: NuxtLinkOptions) {
100100

101101
function resolveTrailingSlashBehavior (to: string, resolve: Router['resolve']): string
102102
function resolveTrailingSlashBehavior (to: RouteLocationRaw, resolve: Router['resolve']): Exclude<RouteLocationRaw, string>
103-
function resolveTrailingSlashBehavior (to: RouteLocationRaw, resolve: Router['resolve']): RouteLocationRaw | RouteLocation {
103+
function resolveTrailingSlashBehavior (to: undefined, resolve: Router['resolve']): undefined
104+
function resolveTrailingSlashBehavior (to: RouteLocationRaw | undefined, resolve: Router['resolve']): RouteLocationRaw | RouteLocation | undefined {
104105
if (!to || (options.trailingSlash !== 'append' && options.trailingSlash !== 'remove')) {
105106
return to
106107
}
@@ -109,13 +110,18 @@ export function defineNuxtLink (options: NuxtLinkOptions) {
109110
return applyTrailingSlashBehavior(to, options.trailingSlash)
110111
}
111112

112-
const path = 'path' in to ? to.path : resolve(to).path
113+
const path = 'path' in to && to.path !== undefined ? to.path : resolve(to).path
113114

114-
return {
115+
const resolvedPath = {
115116
...to,
116-
name: undefined, // named routes would otherwise always override trailing slash behavior
117117
path: applyTrailingSlashBehavior(path, options.trailingSlash)
118118
}
119+
120+
if ('name' in resolvedPath) {
121+
delete resolvedPath.name
122+
}
123+
124+
return resolvedPath
119125
}
120126

121127
return defineComponent({
@@ -326,8 +332,8 @@ export function defineNuxtLink (options: NuxtLinkOptions) {
326332
const href = typeof to.value === 'object'
327333
? router.resolve(to.value)?.href ?? null
328334
: (to.value && !props.external && !isAbsoluteUrl.value)
329-
? resolveTrailingSlashBehavior(joinURL(config.app.baseURL, to.value), router.resolve) as string
330-
: to.value || null
335+
? resolveTrailingSlashBehavior(joinURL(config.app.baseURL, to.value), router.resolve) as string
336+
: to.value || null
331337

332338
// Resolves `target` value
333339
const target = props.target || null

packages/nuxt/src/pages/runtime/plugins/router.ts

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -237,9 +237,11 @@ const plugin: Plugin<{ router: Router }> = defineNuxtPlugin({
237237

238238
nuxtApp.hooks.hookOnce('app:created', async () => {
239239
try {
240+
const to = router.resolve(initialURL)
241+
// #4920, #4982
242+
if ('name' in to) { delete to.name }
240243
await router.replace({
241-
...router.resolve(initialURL),
242-
name: undefined, // #4920, #4982
244+
...to,
243245
force: true
244246
})
245247
// reset scroll behavior to initial value

0 commit comments

Comments
 (0)