Skip to content

Commit

Permalink
Merge pull request #3014 from opral/lorissigrist/parjs-173-svelte-kit…
Browse files Browse the repository at this point in the history
…-server-handle-removes-query-parameters

Paraglide-SvelteKit Preserve query parameters when redirecting
  • Loading branch information
LorisSigrist authored Jul 8, 2024
2 parents 07c48e7 + 65d1be3 commit 1dea8ad
Show file tree
Hide file tree
Showing 4 changed files with 31 additions and 1 deletion.
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

0 comments on commit 1dea8ad

Please sign in to comment.