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

URL with URL-encoded characters like %23 gets decoded when changing locale #2909

Open
0x2c9 opened this issue Apr 12, 2024 · 4 comments
Open

Comments

@0x2c9
Copy link

0x2c9 commented Apr 12, 2024

Environment


  • Operating System: Linux
  • Node Version: v18.18.0
  • Nuxt Version: 3.11.2
  • CLI Version: 3.11.1
  • Nitro Version: 2.9.6
  • Package Manager: [email protected]
  • Builder: -
  • User Config: devtools, modules, i18n, ssr
  • Runtime Modules: @nuxtjs/[email protected]
  • Build Modules: -

Reproduction

https://stackblitz.com/edit/i18n-url-encoding-issue?file=app.vue

Describe the bug

If an URL contains an URL-encoded character like "%23" and we switch the language, the url gets decoded and converted into a "#" which breaks for example the $route.params usage.

  1. Open Reproduction
  2. Click "Go to my-blog-post%23number1-someid" link
  3. Change locale
  4. Observe $route.params

Before locale change the route params are: (which vue-router already decodes correctly)
{ "slug": "my-blog-post#number1-someid" }

after locale change:
{ "slug": "my-blog-post" }

and we get a route hash
#number1-someid

You can also open the preview in a new tab to see the decoding in the URL bar.

The only other issue I found which could be maybe related to this is: #2698

Additional context

No response

Logs

No response

@BobbieGoede
Copy link
Collaborator

It's been a while back since I dived into debugging and figuring out a solution for #2698 but from what I remember the core cause of your issue is the same, I should probably update the issue description.

The module internally uses $router.resolve to retrieve the localized route based on the passed argument. In some cases the resolve function is used more than once with a previous resolved result, unfortunately doing so loses information:

const router = useRouter()
const arg = '/en/blog/my-blog-post%23number1-someid'
const resolved1 = router.resolve(arg)
const resolved2 = router.resolve(resolved1)

console.table({ arg, resolved1: resolved1.fullPath, resolved2: resolved2.fullPath })

Would result in this

key value
arg /en/blog/my-blog-post%23number1-someid
resolved1 /en/blog/my-blog-post%23number1-someid
resolved2 /en/blog/my-blog-post#number1-someid

I have some working concepts that deal with this issue from a while ago but they involve some larger changes to the routing logic, it will take time to test and see which solution makes most sense. So unfortunately I won't have a solution in the short term! Will let you know if I find any workarounds to use instead.

@memic84
Copy link

memic84 commented Apr 19, 2024

Kind a same issue here, where the output of localePath now containers %3F at the end.

@BobbieGoede
Copy link
Collaborator

@memic84
What are you passing to localePath?

@memic84
Copy link

memic84 commented Apr 19, 2024

@BobbieGoede my mistake, for reason a question mark was appended in the route name. Fixed and not a module issue.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

3 participants