Skip to content

Commit aa24d96

Browse files
authored
Merge pull request #7733 from maiieul/fix-link-and-redirect-wrong-url
2 parents 7843e78 + ff08b44 commit aa24d96

File tree

10 files changed

+114
-5
lines changed

10 files changed

+114
-5
lines changed

.changeset/every-socks-beg.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
'@builder.io/qwik-city': patch
3+
---
4+
5+
FIX: link/useNavigate with query params don't override loader/middleware redirect with query params anymore.

packages/qwik-city/src/buildtime/build-layout.unit.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ import { assert, testAppSuite } from '../utils/test-suite';
33
const test = testAppSuite('Build Layout');
44

55
test('total layouts', ({ ctx: { layouts } }) => {
6-
assert.equal(layouts.length, 11, JSON.stringify(layouts, null, 2));
6+
assert.equal(layouts.length, 12, JSON.stringify(layouts, null, 2));
77
});
88

99
test('nested named layout', ({ assertLayout }) => {

packages/qwik-city/src/runtime/src/qwik-city-component.tsx

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -383,8 +383,7 @@ export const QwikCityProvider = component$<QwikCityProps>((props) => {
383383
const pageModule = contentModules[contentModules.length - 1] as PageModule;
384384

385385
// Restore search params unless it's a redirect
386-
const isRedirect = navType === 'form' && !isSamePath(trackUrl, prevUrl);
387-
if (navigation.dest.search && !isRedirect) {
386+
if (navigation.dest.search && !!isSamePath(trackUrl, prevUrl)) {
388387
trackUrl.search = navigation.dest.search;
389388
}
390389

@@ -430,7 +429,8 @@ export const QwikCityProvider = component$<QwikCityProps>((props) => {
430429
(navigation.scroll &&
431430
(!navigation.forceReload || !isSamePath(trackUrl, prevUrl)) &&
432431
(navType === 'link' || navType === 'popstate')) ||
433-
isRedirect
432+
// Action might have responded with a redirect.
433+
(navType === 'form' && !isSamePath(trackUrl, prevUrl))
434434
) {
435435
// Mark next DOM render to scroll.
436436
(document as any).__q_scroll_restore__ = () =>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
import { useLocation } from "@builder.io/qwik-city";
2+
import { component$ } from "@builder.io/qwik";
3+
4+
export default component$(() => {
5+
const location = useLocation();
6+
7+
return (
8+
<div>
9+
<h1>
10+
Should <strong>not</strong> have searchParams
11+
</h1>
12+
<pre>{JSON.stringify(location.url.searchParams.get("id"))}</pre>
13+
</div>
14+
);
15+
});
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
import { routeAction$, Form } from "@builder.io/qwik-city";
2+
import { component$ } from "@builder.io/qwik";
3+
4+
export const useAction = routeAction$((_, event) => {
5+
throw event.redirect(
6+
302,
7+
"/qwikcity-test/action-redirect-without-search-params-target/",
8+
);
9+
});
10+
11+
export default component$(() => {
12+
const action = useAction();
13+
14+
return (
15+
<Form action={action}>
16+
<h1>Should have searchParams on the url</h1>
17+
<button type="submit">Submit</button>
18+
</Form>
19+
);
20+
});
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
import { component$ } from "@builder.io/qwik";
2+
3+
export default component$(() => {
4+
return <div>empty default route A</div>;
5+
});
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
import { component$ } from "@builder.io/qwik";
2+
import type { RequestHandler } from "@builder.io/qwik-city";
3+
4+
export const onGet: RequestHandler<void> = ({ redirect }) => {
5+
throw redirect(302, `/qwikcity-test/issue7732/c/?redirected=true`);
6+
};
7+
8+
export default component$(() => {
9+
return <div>B route with redirect</div>;
10+
});
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
import { component$ } from "@builder.io/qwik";
2+
3+
export default component$(() => {
4+
return <div>route C should have redirected=true params</div>;
5+
});
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
import { component$, Slot } from "@builder.io/qwik";
2+
import { Link } from "@builder.io/qwik-city";
3+
4+
export default component$(() => {
5+
return (
6+
<>
7+
<div style={{ display: "flex", gap: "10px" }}>
8+
<Link href="/issue7732/a/" id="issue7732-link-a">
9+
A
10+
</Link>
11+
12+
<Link
13+
href="/qwikcity-test/issue7732/b/?shouldOverrideRedirect=no"
14+
id="issue7732-link-b"
15+
>
16+
B
17+
</Link>
18+
<Link href="/issue7732/c/" id="issue7732-link-c">
19+
C
20+
</Link>
21+
</div>
22+
<Slot />
23+
</>
24+
);
25+
});

starters/e2e/qwikcity/nav.spec.ts

Lines changed: 25 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import { $ } from "bun";
12
import {
23
assertPage,
34
getScrollHeight,
@@ -447,7 +448,30 @@ test.describe("actions", () => {
447448
await expect(result).toHaveText("3");
448449
}
449450
});
450-
451+
test("issue7732 link/useNavigate with query params should not override loader/middleware redirect with query params", async ({
452+
page,
453+
}) => {
454+
await page.goto("/qwikcity-test/issue7732/a/");
455+
const link = page.locator("#issue7732-link-b");
456+
await link.click();
457+
await expect(page).toHaveURL(
458+
"/qwikcity-test/issue7732/c/?redirected=true",
459+
);
460+
});
461+
// TODO: Fix this test (currently not working because the action redirect adds a `/q-data.json` at the end of the path)
462+
test.fixme(
463+
"action with redirect without query params in a route with query param should redirect to route without query params",
464+
async ({ page }) => {
465+
await page.goto(
466+
"/qwikcity-test/action-redirect-without-search-params/?test=test",
467+
);
468+
const button = page.locator("button");
469+
await button.click();
470+
await page.waitForURL(
471+
"/qwikcity-test/action-redirect-without-search-params-target/",
472+
);
473+
},
474+
);
451475
test("media in home page", async ({ page }) => {
452476
await page.goto("/qwikcity-test/");
453477

0 commit comments

Comments
 (0)