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
fix(nuxt): resolve internal target: blank
links with base
#23751
Conversation
Run & review this pull request in StackBlitz Codeflow. |
target=_blank
nuxt link should join baseURLtarget=_blank
nuxt link should join baseURL
Hi! I happen to be researching this issue as well. Would you like to consider some other ideas? let href = typeof to.value === 'object' ? router.resolve(to.value)?.href ?? null : to.value || null
if (href && href.startsWith('/') && !href.startsWith('//')) {
const _base = withLeadingSlash(withTrailingSlash(baseURL))
if (_base !== '/' && !href.startsWith(_base)) {
href = joinURL(_base, href)
}
} This way, the following examples will work as expected: // nuxt.config.ts
export default defineNuxtConfig({
app: {
baseURL: '/app/'
}
}) <template>
<NuxtLink to="http://localhost:3000/app/about" target="_blank">
link with target and protocol
</NuxtLink>
<!--
Output:
<a href="http://localhost:3000/app/about" rel="noopener noreferrer" target="_blank">
link with target and baseURL
</a>
-->
<NuxtLink to="//localhost:3000/app/about" target="_blank">
link with target and starts with `//`
</NuxtLink>
<!--
Output:
<a href="//localhost:3000/app/about" rel="noopener noreferrer" target="_blank">
link with target and baseURL
</a>
-->
<NuxtLink to="/app/about" target="_blank">
link with target and baseURL
</NuxtLink>
<!--
Output:
<a href="/app/about" rel="noopener noreferrer" target="_blank">
link with target and baseURL
</a>
-->
<NuxtLink to="/about" target="_blank">
link with target without baseURL
</NuxtLink>
<!--
Output:
<a href="/app/about" rel="noopener noreferrer" target="_blank">
link without target
</a>
-->
</template> |
Yeah, your idea is better. I modified this pull request with reference to your implementation... Thank you! |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Would you be able to add some tests to cover this?
Co-authored-by: Alex Liu <dsa1314@gmail.com>
I've added the test cases! |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
LGTM! just a small concern with the checks, and some optional style suggestions for the tests :)
Thanks for making this!
Co-authored-by: Lucie <25330882+lihbr@users.noreply.github.com>
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
LGTM, thanks!
- move `useRuntimeConfig()` out of render function - respect `trailingSlashBehavior` - don't reimplement `hasProtocol`
target=_blank
nuxt link should join baseURLtarget: blank
links with base
π Linked issue
β Type of change
π Description
Now, such a link will automatically append the baseURL.
This should be a breaking change. because it's possible that someone could explicitly add baseURL to the
to
attribute to get around the bug.π Checklist