Skip to content

Commit d8889ce

Browse files
authored
feat: add is external logic from NuxtLink component (#2273)
* Add is external logic from NuxtLink component * Add test * Linting * Use NuxtLinkLocale for external links
1 parent 03b020b commit d8889ce

File tree

4 files changed

+32
-1
lines changed

4 files changed

+32
-1
lines changed

specs/basic_usage.spec.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,9 @@ test('basic usage', async () => {
4343
expect(await page.locator('#nuxt-link-locale-usages .object-with-named a').getAttribute('href')).toEqual(
4444
'/category/nintendo'
4545
)
46+
expect(await page.locator('#nuxt-link-locale-usages .external-url a').getAttribute('href')).toEqual(
47+
'https://nuxt.com/'
48+
)
4649

4750
// Language switching path localizing with `useSwitchLocalePath`
4851
expect(await page.locator('#switch-locale-path-usages .switch-to-en a').getAttribute('href')).toEqual('/')

specs/fixtures/basic_usage/pages/index.vue

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,9 @@ function onClick() {
7878
{{ category.title }}
7979
</NuxtLinkLocale>
8080
</li>
81+
<li class="external-url">
82+
<NuxtLinkLocale :to="'https://nuxt.com/'">Nuxt.com</NuxtLinkLocale>
83+
</li>
8184
</ul>
8285
</section>
8386
<section id="switch-locale-path-usages">

src/runtime/components/NuxtLinkLocale.ts

Lines changed: 25 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import { useLocalePath } from '#i18n'
22
import { defineComponent, computed, defineNuxtLink, h } from '#imports'
3+
import { hasProtocol } from 'ufo'
34

45
import type { PropType } from 'vue'
56
import type { RawLocation, RouteLocation } from '@intlify/vue-router-bridge'
@@ -25,6 +26,29 @@ export default defineComponent({
2526
const localePath = useLocalePath()
2627
const resolvedPath = computed(() => (props.to != null ? localePath(props.to, props.locale) : props.to))
2728

28-
return () => h(NuxtLinkLocale, { ...props, to: resolvedPath }, slots.default)
29+
// Resolving link type
30+
const isExternal = computed<boolean>(() => {
31+
// External prop is explicitly set
32+
if (props.external) {
33+
return true
34+
}
35+
36+
// When `target` prop is set, link is external
37+
if (props.target && props.target !== '_self') {
38+
return true
39+
}
40+
41+
// When `to` is a route object then it's an internal link
42+
if (typeof props.to === 'object') {
43+
return false
44+
}
45+
46+
return props.to === '' || hasProtocol(props.to, { acceptRelative: true })
47+
})
48+
49+
return () =>
50+
isExternal.value
51+
? h(NuxtLinkLocale, props, slots.default)
52+
: h(NuxtLinkLocale, { ...props, to: resolvedPath }, slots.default)
2953
}
3054
})

tsconfig.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@
3131
"paths": {
3232
"#app": ["./node_modules/nuxt/dist/app"],
3333
"#app/*": ["./node_modules/nuxt/dist/app/*"],
34+
"#components": ["./.nuxt/components"],
3435
"#imports": ["./.nuxt/imports.d.ts"],
3536
"#pages": ["./node_modules/nuxt/dist/pages/runtime/index.d.ts"],
3637
"#head": ["./node_modules/nuxt/dist/index.d.ts"],

0 commit comments

Comments
 (0)