Skip to content

Commit

Permalink
Merge pull request #3006 from opral/lorissigrist/parjs-172-paraglide-…
Browse files Browse the repository at this point in the history
…next-edits-paths-segments-that-start-with

Paraglide-Next: Fix paths segments that start with language tags being edited
  • Loading branch information
LorisSigrist authored Jul 4, 2024
2 parents d446099 + 7dfecf1 commit 504c637
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 3 deletions.
7 changes: 7 additions & 0 deletions .changeset/plenty-roses-grab.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
---
"@inlang/paraglide-next": patch
---

fix issue where path-segments that start with a language tag confused the router.

Eg: `/entropy` would match the language `en` & be resolved to `/tropy`
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,10 @@ describe("getCanonicalPath", () => {
expect(getCanonicalPath("/en/translated/1", "en")).toBe("/canonical-translated/1")
expect(getCanonicalPath("/de-CH/uebersetzt/1", "de-CH")).toBe("/canonical-translated/1")
})

it("does not get confused by a path that starts with a language tag", () => {
expect(getCanonicalPath("/de-something-else", "de")).toBe("/de-something-else")
})
})

describe("getLocalisedPath", () => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -103,8 +103,10 @@ export function PrefixStrategy<T extends string>({
// get the prefix for this language
const prefix = resolvedPrefixes[locale] ?? locale

const pathWithoutLocale: `/${string}` = localisedPath.startsWith(`/${prefix}`)
? ((localisedPath.replace(`/${prefix}`, "") || "/") as `/${string}`)
const pathWithoutLocale: `/${string}` = localisedPath.startsWith(`/${prefix}/`)
? (localisedPath.replace(`/${prefix}`, "") as `/${string}`)
: localisedPath === `/${prefix}`
? "/"
: localisedPath

for (const [canonicalPathDefinition, translationsForPath] of Object.entries(
Expand Down Expand Up @@ -166,7 +168,10 @@ export function PrefixStrategy<T extends string>({
const pathWithoutBase = request.nextUrl.pathname

const entries = Object.entries(resolvedPrefixes) as [T, string][]
const entry = entries.find(([, prefix]) => pathWithoutBase.startsWith(`/${prefix}`))
const entry = entries.find(
([, prefix]) =>
pathWithoutBase.startsWith(`/${prefix}/`) || pathWithoutBase === `/${prefix}`
)
const detected = entry?.[0]

// If no prefix is detected and prefixDefault is "never" -> use default language
Expand Down

0 comments on commit 504c637

Please sign in to comment.