Skip to content
New issue

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

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

Already on GitHub? Sign in to your account

feat(nuxt): support trailingSlashBehavior in defineNuxtLink #19458

Merged
Merged
Show file tree
Hide file tree
Changes from 13 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
2 changes: 2 additions & 0 deletions docs/3.api/2.components/4.nuxt-link.md
Original file line number Diff line number Diff line change
Expand Up @@ -115,6 +115,7 @@ defineNuxtLink({
activeClass?: string;
exactActiveClass?: string;
prefetchedClass?: string;
trailingSlash?: 'append' | 'remove'
}) => Component
```

Expand All @@ -123,6 +124,7 @@ defineNuxtLink({
- **activeClass**: A default class to apply on active links. Works the same as [Vue Router's `linkActiveClass` option](https://router.vuejs.org/api/#linkactiveclass). Defaults to Vue Router's default (`"router-link-active"`)
- **exactActiveClass**: A default class to apply on exact active links. Works the same as [Vue Router's `linkExactActiveClass` option](https://router.vuejs.org/api/#linkexactactiveclass). Defaults to Vue Router's default (`"router-link-exact-active"`)
- **prefetchedClass**: A default class to apply to links that have been prefetched.
- **trailingSlash**: An option to force trailing slash in all urls or remove by default. If option is not defined or different from `"append"`/`"remove"` it will be ignored.
danielroe marked this conversation as resolved.
Show resolved Hide resolved

::LinkExample{link="/docs/examples/routing/nuxt-link"}
::
36 changes: 33 additions & 3 deletions packages/nuxt/src/app/components/nuxt-link.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import type { PropType, DefineComponent, ComputedRef } from 'vue'
import { defineComponent, h, ref, resolveComponent, computed, onMounted, onBeforeUnmount } from 'vue'
import type { RouteLocationRaw } from 'vue-router'
import { hasProtocol, parseQuery, parseURL } from 'ufo'
import type { RouteLocation, RouteLocationRaw } from 'vue-router'
import { hasProtocol, parseQuery, parseURL, withoutTrailingSlash, withTrailingSlash } from 'ufo'

import { preloadRouteComponents } from '../composables/preload'
import { onNuxtReady } from '../composables/ready'
Expand All @@ -19,6 +19,7 @@ export type NuxtLinkOptions = {
activeClass?: string
exactActiveClass?: string
prefetchedClass?: string
trailingSlash?: 'append' | 'remove'
danielroe marked this conversation as resolved.
Show resolved Hide resolved
}

export type NuxtLinkProps = {
Expand Down Expand Up @@ -53,6 +54,33 @@ export function defineNuxtLink (options: NuxtLinkOptions) {
console.warn(`[${componentName}] \`${main}\` and \`${sub}\` cannot be used together. \`${sub}\` will be ignored.`)
}
}
const resolveTrailingSlashBehavior = (
to: RouteLocationRaw,
resolve: (to: RouteLocationRaw) => RouteLocation & { href?: string }
): RouteLocationRaw | RouteLocation => {
if (!to || (options.trailingSlash !== 'append' && options.trailingSlash !== 'remove')) {
return to
}

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

if ('path' in to) {
return {
...to,
path: normalizeTrailingSlash(to.path, true)
}
}

const route = resolve(to)
return {
...to,
name: undefined, // named routes would otherwise always override trailing slash behavior
path: normalizeTrailingSlash(route.path)
}
}

return defineComponent({
name: componentName,
Expand Down Expand Up @@ -148,7 +176,9 @@ export function defineNuxtLink (options: NuxtLinkOptions) {
const to: ComputedRef<string | RouteLocationRaw> = computed(() => {
checkPropConflicts(props, 'to', 'href')

return props.to || props.href || '' // Defaults to empty string (won't render any `href` attribute)
const path = props.to || props.href || '' // Defaults to empty string (won't render any `href` attribute)

return resolveTrailingSlashBehavior(path, router.resolve)
})

// Resolving link type
Expand Down
45 changes: 41 additions & 4 deletions packages/nuxt/test/nuxt-link.test.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { expect, describe, it, vi } from 'vitest'
import type { RouteLocationRaw } from 'vue-router'
import type { RouteLocation, RouteLocationRaw } from 'vue-router'
import type { NuxtLinkOptions, NuxtLinkProps } from '../src/app/components/nuxt-link'
import { defineNuxtLink } from '../src/app/components/nuxt-link'

Expand All @@ -15,7 +15,20 @@ vi.mock('vue', async () => {

// Mocks Nuxt `useRouter()`
vi.mock('../src/app/composables/router', () => ({
useRouter: () => ({ resolve: ({ to }: { to: string }) => ({ href: to }) })
useRouter: () => ({
resolve: (route: string | RouteLocation & { to?: string }): Partial<RouteLocation> & { href?: string } => {
if (typeof route === 'string') {
return { href: route, path: route }
}
return route.to
? { href: route.to }
: {
path: route.path || `/${route.name?.toString()}` || undefined,
query: route.query || undefined,
hash: route.hash || undefined
}
}
})
}))

// Helpers for test visibility
Expand All @@ -25,9 +38,9 @@ const INTERNAL = 'RouterLink'
// Renders a `<NuxtLink />`
const nuxtLink = (
props: NuxtLinkProps = {},
NuxtLinkOptions: Partial<NuxtLinkOptions> = {}
nuxtLinkOptions: Partial<NuxtLinkOptions> = {}
): { type: string, props: Record<string, unknown>, slots: unknown } => {
const component = defineNuxtLink({ componentName: 'NuxtLink', ...NuxtLinkOptions })
const component = defineNuxtLink({ componentName: 'NuxtLink', ...nuxtLinkOptions })

const [type, _props, slots] = (component.setup as unknown as (props: NuxtLinkProps, context: { slots: Record<string, () => unknown> }) =>
() => [string, Record<string, unknown>, unknown])(props, { slots: { default: () => null } })()
Expand Down Expand Up @@ -199,5 +212,29 @@ describe('nuxt-link:propsOrAttributes', () => {
expect(nuxtLink({ to: '/to', ariaCurrentValue: 'step' }).props.ariaCurrentValue).toBe('step')
})
})

describe('trailingSlashBehavior', () => {
it('append slash', () => {
const appendSlashOptions: NuxtLinkOptions = { trailingSlash: 'append' }

expect(nuxtLink({ to: '/to' }, appendSlashOptions).props.to).toEqual('/to/')
expect(nuxtLink({ to: '/to/' }, appendSlashOptions).props.to).toEqual('/to/')
expect(nuxtLink({ to: { name: 'to' } }, appendSlashOptions).props.to).toHaveProperty('path', '/to/')
expect(nuxtLink({ to: { path: '/to' } }, appendSlashOptions).props.to).toHaveProperty('path', '/to/')
expect(nuxtLink({ href: '/to' }, appendSlashOptions).props.to).toEqual('/to/')
expect(nuxtLink({ to: '/to?param=1' }, appendSlashOptions).props.to).toEqual('/to/?param=1')
})

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: { name: 'to' } }, removeSlashOptions).props.to).toHaveProperty('path', '/to')
expect(nuxtLink({ to: { path: '/to/' } }, removeSlashOptions).props.to).toHaveProperty('path', '/to')
expect(nuxtLink({ href: '/to/' }, removeSlashOptions).props.to).toEqual('/to')
expect(nuxtLink({ to: '/to/?param=1' }, removeSlashOptions).props.to).toEqual('/to?param=1')
})
})
})
})
72 changes: 72 additions & 0 deletions test/basic.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -282,6 +282,78 @@ describe('pages', () => {
})
})

describe('nuxt links', () => {
it('handles trailing slashes', async () => {
const html = await $fetch('/nuxt-link/trailing-slash')
const data: Record<string, string[]> = {}
for (const selector of ['nuxt-link', 'router-link', 'link-with-trailing-slash', 'link-without-trailing-slash']) {
data[selector] = []
for (const match of html.matchAll(new RegExp(`href="([^"]*)"[^>]*class="[^"]*\\b${selector}\\b`, 'g'))) {
data[selector].push(match[1])
}
}
expect(data).toMatchInlineSnapshot(`
{
"link-with-trailing-slash": [
"/",
"/nuxt-link/trailing-slash/",
"/nuxt-link/trailing-slash/",
"/nuxt-link/trailing-slash/?test=true&amp;thing=other/thing#thing-other",
"/nuxt-link/trailing-slash/?test=true&amp;thing=other/thing#thing-other",
"/nuxt-link/trailing-slash/",
"/nuxt-link/trailing-slash/?with-state=true",
"/nuxt-link/trailing-slash/?without-state=true",
],
"link-without-trailing-slash": [
"/",
"/nuxt-link/trailing-slash",
"/nuxt-link/trailing-slash",
"/nuxt-link/trailing-slash?test=true&amp;thing=other/thing#thing-other",
"/nuxt-link/trailing-slash?test=true&amp;thing=other/thing#thing-other",
"/nuxt-link/trailing-slash",
"/nuxt-link/trailing-slash?with-state=true",
"/nuxt-link/trailing-slash?without-state=true",
],
"nuxt-link": [
"/",
"/nuxt-link/trailing-slash",
"/nuxt-link/trailing-slash/",
"/nuxt-link/trailing-slash?test=true&amp;thing=other/thing#thing-other",
"/nuxt-link/trailing-slash/?test=true&amp;thing=other/thing#thing-other",
"/nuxt-link/trailing-slash",
"/nuxt-link/trailing-slash?with-state=true",
"/nuxt-link/trailing-slash?without-state=true",
],
"router-link": [
"/",
"/nuxt-link/trailing-slash",
"/nuxt-link/trailing-slash/",
"/nuxt-link/trailing-slash?test=true&amp;thing=other/thing#thing-other",
"/nuxt-link/trailing-slash/?test=true&amp;thing=other/thing#thing-other",
"/nuxt-link/trailing-slash",
"/nuxt-link/trailing-slash?with-state=true",
"/nuxt-link/trailing-slash?without-state=true",
],
}
`)
})

it('preserves route state', async () => {
const page = await createPage('/nuxt-link/trailing-slash')
await page.waitForLoadState('networkidle')

for (const selector of ['nuxt-link', 'router-link', 'link-with-trailing-slash', 'link-without-trailing-slash']) {
await page.locator(`.${selector}[href*=with-state]`).click()
await page.waitForLoadState('networkidle')
expect(await page.getByTestId('window-state').innerText()).toContain('bar')

await page.locator(`.${selector}[href*=without-state]`).click()
await page.waitForLoadState('networkidle')
expect(await page.getByTestId('window-state').innerText()).not.toContain('bar')
}
})
})

describe('head tags', () => {
it('should render tags', async () => {
const headHtml = await $fetch('/head')
Expand Down
2 changes: 1 addition & 1 deletion test/bundle.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ describe.skipIf(isWindows)('minimal nuxt application', () => {

it('default server bundle size', async () => {
stats.server = await analyzeSizes(['**/*.mjs', '!node_modules'], serverDir)
expect(stats.server.totalBytes).toBeLessThan(93000)
expect(stats.server.totalBytes).toBeLessThan(94000)

const modules = await analyzeSizes('node_modules/**/*', serverDir)
expect(modules.totalBytes).toBeLessThan(2722000)
Expand Down
73 changes: 73 additions & 0 deletions test/fixtures/basic/pages/nuxt-link/trailing-slash.vue
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
<script setup lang="ts">
const LinkWithTrailingSlash = defineNuxtLink({
trailingSlash: 'append'
})
const LinkWithoutTrailingSlash = defineNuxtLink({
trailingSlash: 'remove'
})
const links = [
'/',
'/nuxt-link/trailing-slash',
'/nuxt-link/trailing-slash/',
'/nuxt-link/trailing-slash?test=true&thing=other/thing#thing-other',
'/nuxt-link/trailing-slash/?test=true&thing=other/thing#thing-other',
{ name: 'nuxt-link-trailing-slash' },
{ query: { 'with-state': 'true' }, state: { foo: 'bar' } },
{ query: { 'without-state': 'true' } }
]

const route = useRoute()
const windowState = computed(() => {
console.log(route.fullPath)
danielroe marked this conversation as resolved.
Show resolved Hide resolved
return process.client ? window.history.state.foo : ''
})
</script>

<template>
<div>
<div data-testid="window-state">
<ClientOnly>
{{ windowState }}
</ClientOnly>
</div>
<ul>
<li v-for="(link, index) in links" :key="index">
<LinkWithTrailingSlash :to="link" class="link-with-trailing-slash">
<LinkWithTrailingSlash v-slot="{ href }" custom :to="link">
{{ href }}
</LinkWithTrailingSlash>
</LinkWithTrailingSlash>
</li>
</ul>
<hr>
<ul>
<li v-for="(link, index) in links" :key="index">
<LinkWithoutTrailingSlash :to="link" class="link-without-trailing-slash">
<LinkWithoutTrailingSlash v-slot="{ href }" custom :to="link">
{{ href }}
</LinkWithoutTrailingSlash>
</LinkWithoutTrailingSlash>
</li>
</ul>
<hr>
<ul>
<li v-for="(link, index) in links" :key="index">
<NuxtLink :to="link" class="nuxt-link">
<NuxtLink v-slot="{ href }" custom :to="link">
{{ href }}
</NuxtLink>
</NuxtLink>
</li>
</ul>
<hr>
<ul>
<li v-for="(link, index) in links" :key="index">
<RouterLink :to="link" class="router-link">
<RouterLink v-slot="{ href }" custom :to="link">
{{ href }}
</RouterLink>
</RouterLink>
</li>
</ul>
</div>
</template>