Skip to content

Commit

Permalink
fix(nuxt): only add/remove trailing slash for http protocols (#23296)
Browse files Browse the repository at this point in the history
  • Loading branch information
manniL committed Dec 12, 2023
1 parent 70dea8b commit 5d0e7e9
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 3 deletions.
16 changes: 13 additions & 3 deletions packages/nuxt/src/app/components/nuxt-link.ts
Original file line number Diff line number Diff line change
Expand Up @@ -67,17 +67,16 @@ export function defineNuxtLink (options: NuxtLinkOptions) {
return to
}

const normalizeTrailingSlash = options.trailingSlash === 'append' ? withTrailingSlash : withoutTrailingSlash
if (typeof to === 'string') {
return normalizeTrailingSlash(to, true)
return applyTrailingSlashBehavior(to, options.trailingSlash)
}

const path = 'path' in to ? to.path : resolve(to).path

return {
...to,
name: undefined, // named routes would otherwise always override trailing slash behavior
path: normalizeTrailingSlash(path, true)
path: applyTrailingSlashBehavior(path, options.trailingSlash)
}
}

Expand Down Expand Up @@ -345,6 +344,17 @@ export function defineNuxtLink (options: NuxtLinkOptions) {

export default defineNuxtLink(nuxtLinkDefaults)

// -- NuxtLink utils --
function applyTrailingSlashBehavior (to: string, trailingSlash: NuxtLinkOptions['trailingSlash']): string {
const normalizeFn = trailingSlash === 'append' ? withTrailingSlash : withoutTrailingSlash
// Until https://github.com/unjs/ufo/issues/189 is resolved
const hasProtocolDifferentFromHttp = hasProtocol(to) && !to.startsWith('http')
if (hasProtocolDifferentFromHttp) {
return to
}
return normalizeFn(to, true)
}

// --- Prefetching utils ---
type CallbackFn = () => void
type ObserveFn = (element: Element, callback: CallbackFn) => () => void
Expand Down
9 changes: 9 additions & 0 deletions packages/nuxt/test/nuxt-link.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -263,21 +263,30 @@ describe('nuxt-link:propsOrAttributes', () => {

expect(nuxtLink({ to: '/to' }, appendSlashOptions).props.to).toEqual('/to/')
expect(nuxtLink({ to: '/to/' }, appendSlashOptions).props.to).toEqual('/to/')
expect(nuxtLink({ to: '/to#abc' }, appendSlashOptions).props.to).toEqual('/to/#abc')
expect(nuxtLink({ to: { name: 'to' } }, appendSlashOptions).props.to).toHaveProperty('path', '/to/')
expect(nuxtLink({ to: { path: '/to' } }, appendSlashOptions).props.to).toHaveProperty('path', '/to/')
expect(nuxtLink({ to: { path: '/to#abc' } }, appendSlashOptions).props.to).toHaveProperty('path', '/to/#abc')
expect(nuxtLink({ href: '/to' }, appendSlashOptions).props.to).toEqual('/to/')
expect(nuxtLink({ href: '/to#abc' }, appendSlashOptions).props.to).toEqual('/to/#abc')
expect(nuxtLink({ to: '/to?param=1' }, appendSlashOptions).props.to).toEqual('/to/?param=1')
expect(nuxtLink({ to: '/to?param=1#abc' }, appendSlashOptions).props.to).toEqual('/to/?param=1#abc')
expect(nuxtLink({ href: 'mailto:test@example.com' }, appendSlashOptions).props.href).toEqual('mailto:test@example.com')
})

it('remove slash', () => {
const removeSlashOptions: NuxtLinkOptions = { trailingSlash: 'remove' }

expect(nuxtLink({ to: '/to' }, removeSlashOptions).props.to).toEqual('/to')
expect(nuxtLink({ to: '/to/' }, removeSlashOptions).props.to).toEqual('/to')
expect(nuxtLink({ to: '/to/#abc' }, removeSlashOptions).props.to).toEqual('/to#abc')
expect(nuxtLink({ to: { name: 'to' } }, removeSlashOptions).props.to).toHaveProperty('path', '/to')
expect(nuxtLink({ to: { path: '/to/' } }, removeSlashOptions).props.to).toHaveProperty('path', '/to')
expect(nuxtLink({ to: { path: '/to/#abc' } }, removeSlashOptions).props.to).toHaveProperty('path', '/to#abc')
expect(nuxtLink({ href: '/to/' }, removeSlashOptions).props.to).toEqual('/to')
expect(nuxtLink({ to: '/to/?param=1' }, removeSlashOptions).props.to).toEqual('/to?param=1')
expect(nuxtLink({ to: '/to/?param=1#abc' }, removeSlashOptions).props.to).toEqual('/to?param=1#abc')
expect(nuxtLink({ href: 'mailto:test@example.com' }, removeSlashOptions).props.href).toEqual('mailto:test@example.com')
})
})
})
Expand Down

0 comments on commit 5d0e7e9

Please sign in to comment.