From 6a7e2f39304ae2329fb110c4f45f434ee5406866 Mon Sep 17 00:00:00 2001 From: Wout Mertens Date: Thu, 18 Jul 2024 11:36:48 +0200 Subject: [PATCH] fix(qwik-city): remove internal params in qdata --- .changeset/rich-hairs-worry.md | 5 ++++ .../resolve-request-handlers.ts | 22 +++++++++------- .../resolve-request-handlers.unit.ts | 9 +++++++ .../runtime/src/qwik-city-component.tsx | 1 + .../src/routes/issue6660/index.tsx | 2 +- starters/e2e/qwikcity/nav.spec.ts | 26 +++++++++++++++---- 6 files changed, 49 insertions(+), 16 deletions(-) create mode 100644 .changeset/rich-hairs-worry.md diff --git a/.changeset/rich-hairs-worry.md b/.changeset/rich-hairs-worry.md new file mode 100644 index 00000000000..e03b1be88e0 --- /dev/null +++ b/.changeset/rich-hairs-worry.md @@ -0,0 +1,5 @@ +--- +'@builder.io/qwik-city': patch +--- + +strip internal search parameters in canonical URLs diff --git a/packages/qwik-city/middleware/request-handler/resolve-request-handlers.ts b/packages/qwik-city/middleware/request-handler/resolve-request-handlers.ts index d7865931f46..7156a9ad2ff 100644 --- a/packages/qwik-city/middleware/request-handler/resolve-request-handlers.ts +++ b/packages/qwik-city/middleware/request-handler/resolve-request-handlers.ts @@ -144,15 +144,15 @@ const _resolveRequestHandlers = ( } if (collectActions) { - const loaders = Object.values(routeModule).filter((e) => - checkBrand(e, 'server_loader') - ) as LoaderInternal[]; - routeLoaders.push(...loaders); - - const actions = Object.values(routeModule).filter((e) => - checkBrand(e, 'server_action') - ) as ActionInternal[]; - routeActions.push(...actions); + for (const module of Object.values(routeModule)) { + if (typeof module === 'function') { + if (module.__brand === 'server_loader') { + routeLoaders.push(module as LoaderInternal); + } else if (module.__brand === 'server_action') { + routeActions.push(module as ActionInternal); + } + } + } } } }; @@ -407,7 +407,9 @@ export function getPathname(url: URL, trailingSlash: boolean | undefined) { url.pathname = url.pathname.slice(0, -1); } } - return url.toString().substring(url.origin.length); + // strip internal search params + const search = url.search.slice(1).replaceAll(/&?q(action|data|func)=[^&]+/g, ''); + return `${url.pathname}${search ? `?${search}` : ''}${url.hash}`; } export const encoder = /*#__PURE__*/ new TextEncoder(); diff --git a/packages/qwik-city/middleware/request-handler/resolve-request-handlers.unit.ts b/packages/qwik-city/middleware/request-handler/resolve-request-handlers.unit.ts index 29a1e308fb4..154399a5896 100644 --- a/packages/qwik-city/middleware/request-handler/resolve-request-handlers.unit.ts +++ b/packages/qwik-city/middleware/request-handler/resolve-request-handlers.unit.ts @@ -20,5 +20,14 @@ describe('resolve-request-handler', () => { '/path?foo=bar#hash' ); }); + + it('should remove internal search params', () => { + expect(getPathname(new URL('http://server/path?qaction=123&qdata=data'), true)).toBe( + '/path/' + ); + expect(getPathname(new URL('http://server/path?foo=1&qfunc=f&bar=2'), false)).toBe( + '/path?foo=1&bar=2' + ); + }); }); }); diff --git a/packages/qwik-city/runtime/src/qwik-city-component.tsx b/packages/qwik-city/runtime/src/qwik-city-component.tsx index 3706cb103c6..e0385e63568 100644 --- a/packages/qwik-city/runtime/src/qwik-city-component.tsx +++ b/packages/qwik-city/runtime/src/qwik-city-component.tsx @@ -264,6 +264,7 @@ export const QwikCityProvider = component$((props) => { const newHref = pageData.href; const newURL = new URL(newHref, trackUrl); if (!isSamePath(newURL, trackUrl)) { + // Change our path to the canonical path in the response. trackUrl = newURL; loadRoutePromise = loadRoute( qwikCity.routes, diff --git a/starters/apps/qwikcity-test/src/routes/issue6660/index.tsx b/starters/apps/qwikcity-test/src/routes/issue6660/index.tsx index b461230db33..e0f83d8cf79 100644 --- a/starters/apps/qwikcity-test/src/routes/issue6660/index.tsx +++ b/starters/apps/qwikcity-test/src/routes/issue6660/index.tsx @@ -12,7 +12,7 @@ export default component$(() => { Submit - {action.value?.ok && Submitted} + {action.value?.ok && Submitted} ); }); diff --git a/starters/e2e/qwikcity/nav.spec.ts b/starters/e2e/qwikcity/nav.spec.ts index d607c6e1fdc..2300733a5e5 100644 --- a/starters/e2e/qwikcity/nav.spec.ts +++ b/starters/e2e/qwikcity/nav.spec.ts @@ -155,17 +155,33 @@ test.describe("actions", () => { }); }); - test("issue 6660 internal params should no trigger navigation", async ({ + test("issue 6660 internal params should not trigger navigation", async ({ page, }) => { await page.goto("/qwikcity-test/issue6660/"); + await expect(page.locator("#status")).toBeHidden(); - const startUrl = page.url(); + { + const startUrl = page.url(); - await page.getByText("Submit").click(); - await page.waitForSelector("span"); + await page.getByText("Submit").click(); + await page.waitForSelector("#status"); + + expect(page.url()).toBe(startUrl); + } + + await page.goto("/qwikcity-test/issue6660/?var=1&hello"); + await expect(page.locator("#status")).toBeHidden(); + + { + const startUrl = page.url(); + expect(startUrl).toContain("var=1&hello"); + + await page.getByText("Submit").click(); + await page.waitForSelector("#status"); - expect(page.url()).toBe(startUrl); + expect(page.url()).toBe(startUrl); + } }); }