Skip to content

Commit

Permalink
fix(qwik-city): remove internal params in qdata
Browse files Browse the repository at this point in the history
  • Loading branch information
wmertens committed Jul 18, 2024
1 parent 4e1f31e commit 6a7e2f3
Show file tree
Hide file tree
Showing 6 changed files with 49 additions and 16 deletions.
5 changes: 5 additions & 0 deletions .changeset/rich-hairs-worry.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@builder.io/qwik-city': patch
---

strip internal search parameters in canonical URLs
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
}
}
}
}
};
Expand Down Expand Up @@ -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();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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'
);
});
});
});
1 change: 1 addition & 0 deletions packages/qwik-city/runtime/src/qwik-city-component.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -264,6 +264,7 @@ export const QwikCityProvider = component$<QwikCityProps>((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,
Expand Down
2 changes: 1 addition & 1 deletion starters/apps/qwikcity-test/src/routes/issue6660/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ export default component$(() => {
Submit
</button>

{action.value?.ok && <span id="#status">Submitted</span>}
{action.value?.ok && <span id="status">Submitted</span>}
</Form>
);
});
26 changes: 21 additions & 5 deletions starters/e2e/qwikcity/nav.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
});
}

Expand Down

0 comments on commit 6a7e2f3

Please sign in to comment.