From 1f75648348b09da7d7d89f8fea8d714935eb37ff Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Mi=C5=A1ko=20Hevery?= Date: Tue, 24 Oct 2023 11:02:42 +0200 Subject: [PATCH] fix: action redirect accidentally stripped searchparams (#5349) Fix #5342 --- .../resolve-request-handlers.ts | 14 +++++++++-- .../resolve-request-handlers.unit.ts | 24 +++++++++++++++++++ 2 files changed, 36 insertions(+), 2 deletions(-) create mode 100644 packages/qwik-city/middleware/request-handler/resolve-request-handlers.unit.ts 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 9ec05ab38a4..e609eff17bf 100644 --- a/packages/qwik-city/middleware/request-handler/resolve-request-handlers.ts +++ b/packages/qwik-city/middleware/request-handler/resolve-request-handlers.ts @@ -377,10 +377,20 @@ export function isLastModulePageRoute(routeModules: RouteModule[]) { } export function getPathname(url: URL, trailingSlash: boolean | undefined) { + url = new URL(url); if (url.pathname.endsWith(QDATA_JSON)) { - return url.pathname.slice(0, -QDATA_JSON.length + (trailingSlash ? 1 : 0)) + url.search; + url.pathname = url.pathname.slice(0, -QDATA_JSON.length); } - return url.pathname; + if (trailingSlash) { + if (!url.pathname.endsWith('/')) { + url.pathname += '/'; + } + } else { + if (url.pathname.endsWith('/')) { + url.pathname = url.pathname.slice(0, -1); + } + } + return url.toString().substring(url.origin.length); } 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 new file mode 100644 index 00000000000..29a1e308fb4 --- /dev/null +++ b/packages/qwik-city/middleware/request-handler/resolve-request-handlers.unit.ts @@ -0,0 +1,24 @@ +import { describe, it, expect } from 'vitest'; +import { getPathname } from './resolve-request-handlers'; + +describe('resolve-request-handler', () => { + describe('getPathname', () => { + it('should remove q-data.json', () => { + expect(getPathname(new URL('http://server/path/q-data.json?foo=bar#hash'), true)).toBe( + '/path/?foo=bar#hash' + ); + expect(getPathname(new URL('http://server/path/q-data.json?foo=bar#hash'), false)).toBe( + '/path?foo=bar#hash' + ); + }); + + it('should pass non q-data.json through', () => { + expect(getPathname(new URL('http://server/path?foo=bar#hash'), true)).toBe( + '/path/?foo=bar#hash' + ); + expect(getPathname(new URL('http://server/path/?foo=bar#hash'), false)).toBe( + '/path?foo=bar#hash' + ); + }); + }); +});