i18n icon indicating copy to clipboard operation
i18n copied to clipboard

query parameters removed when using useLocalePath

Open AwesomeAnders opened this issue 3 years ago • 11 comments

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:

https://github.com/nuxt-modules/i18n/issues/1967

Logs

No response

AwesomeAnders avatar Apr 21 '23 12:04 AwesomeAnders

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 avatar Apr 21 '23 13:04 BobbieGoede

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.

ennioVisco avatar Jun 29 '23 13:06 ennioVisco

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.

Red-Asuka avatar Jul 20 '23 07:07 Red-Asuka

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

maxdzin avatar Jul 20 '23 10:07 maxdzin

@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.

Red-Asuka avatar Jul 31 '23 06:07 Red-Asuka

+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 avatar Aug 10 '23 13:08 adesombergh

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

adesombergh avatar Aug 11 '23 09:08 adesombergh

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}`
}

Red-Asuka avatar Aug 11 '23 09:08 Red-Asuka

Wow thanks, that was fast!

adesombergh avatar Aug 13 '23 21:08 adesombergh

@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

Red-Asuka avatar Aug 25 '23 05:08 Red-Asuka

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

kazupon avatar Aug 25 '23 06:08 kazupon