diff --git a/.changeset/breezy-forks-check.md b/.changeset/breezy-forks-check.md new file mode 100644 index 0000000000..fced09cdc9 --- /dev/null +++ b/.changeset/breezy-forks-check.md @@ -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 //login?from=/home +``` \ No newline at end of file diff --git a/inlang/source-code/paraglide/paraglide-sveltekit/example/src/routes/redirect/+page.server.ts b/inlang/source-code/paraglide/paraglide-sveltekit/example/src/routes/redirect/+page.server.ts new file mode 100644 index 0000000000..13d994b98d --- /dev/null +++ b/inlang/source-code/paraglide/paraglide-sveltekit/example/src/routes/redirect/+page.server.ts @@ -0,0 +1,9 @@ +import { redirect } from "@sveltejs/kit" + +export const prerender = false + +export const actions = { + redirect: async () => { + redirect(303, "/?redirected=true") + }, +} diff --git a/inlang/source-code/paraglide/paraglide-sveltekit/example/src/routes/redirect/+page.svelte b/inlang/source-code/paraglide/paraglide-sveltekit/example/src/routes/redirect/+page.svelte new file mode 100644 index 0000000000..53589995a0 --- /dev/null +++ b/inlang/source-code/paraglide/paraglide-sveltekit/example/src/routes/redirect/+page.svelte @@ -0,0 +1,3 @@ +
+ +
\ No newline at end of file diff --git a/inlang/source-code/paraglide/paraglide-sveltekit/src/runtime/hooks/handle.server.ts b/inlang/source-code/paraglide/paraglide-sveltekit/src/runtime/hooks/handle.server.ts index c0b2a04411..d60719c557 100644 --- a/inlang/source-code/paraglide/paraglide-sveltekit/src/runtime/hooks/handle.server.ts +++ b/inlang/source-code/paraglide/paraglide-sveltekit/src/runtime/hooks/handle.server.ts @@ -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. @@ -113,13 +114,19 @@ export const createHandle = ( 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, }, }) }