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

Fix trailing slash #34

Merged
merged 3 commits into from
Sep 5, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .changeset/grumpy-sloths-shout.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@astrolicious/i18n": patch
---

Fixes an issue where locales would not be properly loaded depending on Astro `trailingSlash` value
2 changes: 1 addition & 1 deletion package/assets/middleware.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ const extractLocaleFromUrl = (pathname: string) => {
} else if (options.strategy === "prefixExceptDefault") {
if (
locale !== options.defaultLocale &&
pathname.startsWith(`/${locale}/`)
pathname.startsWith(`/${locale}`)
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@amosbastian perhaps you can try to use

const locale = 'fr';
const regex = new RegExp(`^/${locale}(/|$|/[^/]+)`);

console.log(regex.test('/fr'));      // true
console.log(regex.test('/fr/'));     // true
console.log(regex.test('/fr/boo'));  // true
console.log(regex.test('/freedom')); // false

instead of pathname.startsWith

) {
return locale;
}
Expand Down
5 changes: 1 addition & 4 deletions package/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -38,10 +38,7 @@
"./components/I18nClient.astro": "./assets/components/I18nClient.astro",
"./components/I18nHead.astro": "./assets/components/I18nHead.astro"
},
"files": [
"dist",
"assets"
],
"files": ["dist", "assets"],
"scripts": {
"dev": "tsup --watch",
"build": "tsup"
Expand Down
Loading