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

query parameters removed when using useLocalePath #2020

Closed
AwesomeAnders opened this issue Apr 21, 2023 · 11 comments · Fixed by #2251 or #2310
Closed

query parameters removed when using useLocalePath #2020

AwesomeAnders opened this issue Apr 21, 2023 · 11 comments · Fixed by #2251 or #2310
Assignees
Labels
bug 🐛 ❗ p4-important Priority 4: bugs that violate documented behavior, or significantly impact perf v8

Comments

@AwesomeAnders
Copy link

AwesomeAnders commented Apr 21, 2023

Environment


  • Operating System: Windows_NT
  • Node Version: v18.16.0
  • Nuxt Version: 3.4.2
  • Nitro Version: 2.3.3
  • Package Manager: [email protected]
  • Builder: vite
  • User Config: modules, i18n
  • Runtime Modules: @nuxtjs/[email protected]
  • Build Modules: -

Reproduction

https://github.com/AwesomeAnders/my-app

In above scenario - if you navigate from the index page to the about page, the query paramteres will disappear

Describe the bug

query paramteres will disappear when using useLocalePath

Additional context

This has been mentioned earlier:

#1967

Logs

No response

@BobbieGoede
Copy link
Collaborator

That's odd, I expanded on your reproduction to demonstrate which approach does and doesn't work here https://stackblitz.com/edit/github-mbabk8?file=pages/index.vue

It looks like there's some inconsistency with the links generated by localePath, I'll look into it! In the meantime I recommend using name instead of path in the objects passed to localePath (eg. localePath({ name: 'about', query: { query: 'query' } })), as that seems to be working as expected.

@BobbieGoede BobbieGoede added bug 🐛 v8 ❗ p4-important Priority 4: bugs that violate documented behavior, or significantly impact perf and removed pending triage labels Apr 21, 2023
@BobbieGoede BobbieGoede self-assigned this Apr 21, 2023
@ennioVisco
Copy link

ennioVisco commented Jun 29, 2023

That's odd, I expanded on your reproduction to demonstrate which approach does and doesn't work here https://stackblitz.com/edit/github-mbabk8?file=pages/index.vue

It looks like there's some inconsistency with the links generated by localePath, I'll look into it! In the meantime I recommend using name instead of path in the objects passed to localePath (eg. localePath({ name: 'about', query: { query: 'query' } })), as that seems to be working as expected.

@BobbieGoede Could be related to this change in [email protected]: https://nuxt.com/blog/v3-4#:~:text=To%20fix%20a%20bug%20with%20our%20vue%2Drouter%20integration%2C%20we%20now%20generate%20a%20slightly%20different%20path%20matching%20syntax.%20If%20you%20were%20relying%20on%20the%20exact%20path%20generated%2C%20have%20a%20look%20at%20https%3A//github.com/nuxt/nuxt/pull/19902%20for%20more%20information.

@Red-Asuka
Copy link

I encountered the same problem. Using :to="localePath({ name: 'try', query: { product: 'ee' } })" can get the correct link, but using :to="localePath('/try?product=ee')" will return a URL without query parameters.

@maxdzin
Copy link

maxdzin commented Jul 20, 2023

@Red-Asuka same problem. :to="localePath('/books?page=2&author=edgar')" returns this URL: /books

@Red-Asuka
Copy link

@kazupon Hello, I noticed that the 8.0.0-rc.1 release notes indicate that this issue has been fixed. I tried to upgrade to this version, but the test results show that the above issue still exists.

I encountered the same problem. Using :to="localePath({ name: 'try', query: { product: 'ee' } })" can get the correct link, but using :to="localePath('/try?product=ee')" will return a URL without query parameters.

@adesombergh
Copy link

+1 Having the same issue with localePath('/some-route?foo=bar') returns '/en/some-route' and we lose the query params. Tested on version 8.0.0-rc.2

@adesombergh
Copy link

Did some investigation, found the potential reason, a possible solution and created new issue in the intlify routing repo

@Red-Asuka
Copy link

I have encapsulated the localePath function and temporarily solved this issue.

utils/i18nLocalePath.ts

import type { RawLocation } from '@intlify/vue-router-bridge'
import type { RouteLocation } from '.nuxt/vue-router'

function getUrlQueryStr(originalUrl: string) {
  const { $config: { public: { baseUrl } } } = useNuxtApp()
  const urlObj = new URL(originalUrl, baseUrl)
  const queryStr = urlObj.search + urlObj.hash
  return queryStr
}

export function i18nLocalePath(path: RawLocation | RouteLocation, lang?: Lang) {
  const { $i18n: { locale } } = useNuxtApp()
  const localePath = useLocalePath()
  const loc = lang ?? locale.value
  let queryStr = ''
  if (typeof path === 'string' && (path.includes('?') || path.includes('#')))
    queryStr = getUrlQueryStr(path)
  const url = localePath(path, loc)
  return `${url}${queryStr}`
}

@adesombergh
Copy link

Wow thanks, that was fast!

@Red-Asuka
Copy link

@kazupon When the path is the /, with only has hash and no query, localePath will be parsed incorrectly, as shown below.

const localePath = useLocalePath()

console.log(localePath('/'))
console.log(localePath('/#hash'))
console.log(localePath('/?test=1#hash'))

// result:
// /en
// /en/
// /en?test=1%23hash

@kazupon
Copy link
Collaborator

kazupon commented Aug 25, 2023

@Red-Asuka
you would open it as new issue, please 🙏

BobbieGoede added a commit to BobbieGoede/i18n that referenced this issue Mar 21, 2024
DarthGigi pushed a commit to DarthGigi/i18n that referenced this issue Apr 16, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug 🐛 ❗ p4-important Priority 4: bugs that violate documented behavior, or significantly impact perf v8
Projects
None yet
7 participants