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

Paraglide-SvelteKit Preserve query parameters when redirecting #3014

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
11 changes: 11 additions & 0 deletions .changeset/breezy-forks-check.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
---
"@inlang/paraglide-sveltekit": patch
---

fix: Preserve query parameters when redirecting ([inlang-paraglide-js#168](https://github.com/opral/inlang-paraglide-js/issues/168))

`i18n.handle` redirects requests if the pathname does not fit the detected language. Previously this would remove any query parameters from the URL. This is no longer the case.

```ts
redirect(303, "/login?from=/home") // will be redirected to /<lang>/login?from=/home
```
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
import { redirect } from "@sveltejs/kit"

export const prerender = false

export const actions = {
redirect: async () => {
redirect(303, "/?redirected=true")
},
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
<form method="post" action="?/redirect">
<button type="submit">Redirect</button>
</form>
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import type { I18nConfig } from "../adapter.server.js"
import type { RoutingStrategy } from "../strategy.js"
import type { ParaglideLocals } from "../locals.js"
import { ALSContext, GlobalContext, type Context } from "./utils.js"
import { getHrefBetween } from "../utils/diff-urls.js"

/**
* The default lang attribute string that's in SvelteKit's `src/app.html` file.
Expand Down Expand Up @@ -113,13 +114,19 @@ export const createHandle = <T extends string>(

if (lang !== langFromUrl && !i18n.exclude(localisedPath)) {
// redirect to the correct language

const localisedPathname = strategy.getLocalisedPath(localisedPath, lang)
const fullPath = serializeRoute(localisedPathname, base, suffix)

const to = new URL(event.url)
to.pathname = fullPath

const href = getHrefBetween(event.url, to)

return new Response(undefined, {
status: 302,
headers: {
Location: fullPath,
Location: href,
},
})
}
Expand Down
Loading