vite icon indicating copy to clipboard operation
vite copied to clipboard

Language switcher force-opens links to language subdomains in new tab

Open mootari opened this issue 1 month ago • 7 comments

Documentation is

  • [ ] Missing
  • [ ] Outdated
  • [x] Confusing
  • [ ] Not sure?

Explain in Detail

Clicking a link in the language switcher dropdown opens the link in a new tab. I'm guessing it's because the links to the per-language subdomains get detected as external.

Your Suggestion for Changes

Selecting a language should change the location of the current tab.

Reproduction

https://vite.dev/guide/

Steps to reproduce

No response

mootari avatar Nov 12 '25 21:11 mootari

If anyone would like to take a proper crack at this, it's worth checking if it can be fixed in VitePress directly as other docs site may benefit from this too, like https://router.vuejs.org.

bluwy avatar Nov 17 '25 17:11 bluwy

Looking at Vitepress, the current implementation feels fairly messy to me:

  • The markdown link plugin calls isExternal() (which just tests against EXTERNAL_URL_RE) to then define a list of attributes for the link
  • The VPLink component tests against EXTERNAL_URL_RE or checks for target=_blank (previously set by the link plugin).

I suspect that the easiest shortterm solution might be to just override the VPLink component and customize the detection there.

mootari avatar Nov 17 '25 20:11 mootari

I don't think we need to do that for all the links in markdown, I was thinking updating it for the navbar is enough. It should require https://github.com/vuejs/vitepress/blob/179ee621d99b3c14e2e098e3b786465cbeaeab9a/src/client/theme-default/components/VPNavBarTranslations.vue to pass target="_self" to the VPLink.

bluwy avatar Nov 18 '25 02:11 bluwy

to pass target="_self" to the VPLink

That would still not make it past the EXTERNAL_URL_RE.test(props.href) check in VPLink though?

mootari avatar Nov 18 '25 14:11 mootari

You can override it https://github.com/vuejs/vitepress/blob/5e0d7dab832044d89c8610b9800172d37e6201e7/src/client/theme-default/components/VPLink.vue#L32

bluwy avatar Nov 18 '25 14:11 bluwy

Right, but the link will still be considered as external, which doesn't seem ideal because it gets the external marker and has referrers disabled.

I don't feel strongly, but my recommendation would be to strive for a solution that treats those links as internal instead of only patching the target.

mootari avatar Nov 18 '25 15:11 mootari

Then, introduce a new option for VPLink to force mark internal? There's a lot of ways around it that you can explore. Maybe fixing the external check globally within VitePress is worthwhile? You can check with the maintainers there what their preference are.

I'm suggesting that it's possible to fix in VitePress that will benefit other projects. This issue isn't urgent that it needs a short-term fix.

bluwy avatar Nov 18 '25 15:11 bluwy