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

getPathname with params #1274

Closed
ferares opened this issue Aug 21, 2024 · 6 comments · Fixed by #1275
Closed

getPathname with params #1274

ferares opened this issue Aug 21, 2024 · 6 comments · Fixed by #1275
Labels
enhancement New feature or request unconfirmed Needs triage.

Comments

@ferares
Copy link

ferares commented Aug 21, 2024

Is your feature request related to a problem? Please describe.

I'm using the sitemap generation for my app following the example provided here.

However when I try to run that script I get an error saying: Error: Insufficient params provided for localized pathname. because I have some pathnames that take in some route params.

Describe the solution you'd like

It'd be great if the getPathname function that we get from calling createLocalizedPathnamesNavigation would take in a params object like the router.replace method does: router.replace({ pathname, params, query }, { locale }) (i.e.: getPathname({locale, href, params}))

Describe alternatives you've considered

Generating the sitemap entries for routes that have params in a more manual fashion by putting together the urls with their params in each language. This would however lead to less maintainable code.

@ferares ferares added enhancement New feature or request unconfirmed Needs triage. labels Aug 21, 2024
@amannn
Copy link
Owner

amannn commented Aug 21, 2024

getPathname does indeed support params on the href property. href is the same that you're passing to router.replace for example.

This question has come up a few times already though, I've now clarified this a bit in #1275. Hope this helps!

amannn added a commit that referenced this issue Aug 21, 2024
@ferares
Copy link
Author

ferares commented Aug 21, 2024

That's great and I can confirm it does work as expected but I think there's some issue with the type definition for getPathname since I'm getting this type error:

Type error: Object literal may only specify known properties, and 'params' does not exist in type '{ pathname: string; } & { query?: Record<string, SearchParamValue> | undefined; }'.

when calling getPathname({ locale, href: { pathname, params } })

Which is actually the reason why I did not know that I could pass in a params object.

(Sidenote: since we're at it I caught a typo on the updated docs PR #1275: "If you're use the same pathnames for all locales...")

@amannn
Copy link
Owner

amannn commented Aug 21, 2024

Thanks, fixed the typo in 3ef6bea!

It's actually not a bug in the types, getPathname validates that only known string values can be passed to the href, not any kind of string.

E.g.:

const test: string = '/asdf';

// ❌ Type 'string' is not assignable to type '"/" | "/about"
getPathname({locale: 'en', href: test});

// ✅ Works
getPathname({locale: 'en', href: '/about'});

Similarly, getPathname validates that only pathnames with dynamic segments can receive the params object. The updated sitemap docs show valid cases where string values match with params.

In case you need to provide arbitrary string values, you might want to // @ts-ignore the expression.

@ferares
Copy link
Author

ferares commented Aug 21, 2024

Maybe I'm missing something on my setup then because I'm not using arbitrary strings and still getting that type error. Here's a minimal reproduction of the error following my current setup architecture:

import { createLocalizedPathnamesNavigation } from "next-intl/navigation"
import { Pathnames, LocalePrefix } from "next-intl/routing"

export const locales = ["es", "pt", "en"] as const

export const pathnames: Pathnames<typeof locales> = {
  "/": "/",
  "/companies/[slug]": {
    es: "/empresas/[slug]",
    pt: "/empresas/[slug]",
    en: "/companies/[slug]",
  },
}

export const localePrefix: LocalePrefix<typeof locales> = "always"

export const { Link, getPathname, redirect, usePathname, useRouter } = createLocalizedPathnamesNavigation({
  locales,
  pathnames,
  localePrefix,
})

type Href = Parameters<typeof getPathname>[0]["href"]

const test: Href = { pathname: "/companies/[slug]", params: { slug } }

const test2: Href = { pathname: "/test" }

I'm getting this error for the test const:

Object literal may only specify known properties, and 'params' does not exist in type '{ pathname: string; } & { query?: Record<string, SearchParamValue> | undefined; }'

And no error for test2 even though /test should not be a valid pathname value since it's not included in pathnames.

I'm also not getting any intellisense autocomplete suggestions for the pathname of the Href

@amannn
Copy link
Owner

amannn commented Aug 21, 2024

Oh right, I think you had the same issue that I just fixed in that PR before. See here: https://github.com/amannn/next-intl/pull/1275/files#diff-28315a1743287da414ba279f75543ef824afe446dcfbe5fa579f5c07df2485b1

The key is using satisfies for pathnames. The docs already had this correct but I noticed this error in the example.

@ferares
Copy link
Author

ferares commented Aug 21, 2024

Awesome I had missed that change, tysm!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request unconfirmed Needs triage.
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants