FIX: Don't unnecessarily scrub query params from homepage (#25665)

Seems like an accident that the homepage route will always strip all query params from the URL.. This fixes that :)
This commit is contained in:
Mark VanLandingham 2024-02-13 14:50:27 -06:00 committed by GitHub
parent d1ebca90ff
commit 4f75cee6d2
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 24 additions and 4 deletions

View File

@ -21,12 +21,14 @@ export default function applyRouterHomepageOverrides(router) {
} }
} }
const homepageRewriteParam = "_discourse_homepage_rewrite";
/** /**
* Returns a magic URL which `discovery-index` will redirect to. * Returns a magic URL which `discovery-index` will redirect to.
* We watch for this, and then perform the rewrite in the router. * We watch for this, and then perform the rewrite in the router.
*/ */
export function homepageDestination() { export function homepageDestination() {
return `/${defaultHomepage()}?_discourse_homepage_rewrite=1`; return `/${defaultHomepage()}?${homepageRewriteParam}=1`;
} }
function rewriteIfNeeded(url, transition) { function rewriteIfNeeded(url, transition) {
@ -34,12 +36,16 @@ function rewriteIfNeeded(url, transition) {
if ( if (
intentUrl?.startsWith(homepageDestination()) || intentUrl?.startsWith(homepageDestination()) ||
(transition?.intent.name === `discovery.${defaultHomepage()}` && (transition?.intent.name === `discovery.${defaultHomepage()}` &&
transition?.intent.queryParams["_discourse_homepage_rewrite"]) transition?.intent.queryParams[homepageRewriteParam])
) { ) {
const params = url.split("?", 2)[1]; const params = (intentUrl || url).split("?", 2)[1];
url = "/"; url = "/";
if (params) { if (params) {
url += `?${params}`; const searchParams = new URLSearchParams(params);
searchParams.delete(homepageRewriteParam);
if (searchParams.size) {
url += `?${searchParams.toString()}`;
}
} }
} }
return url; return url;

View File

@ -74,4 +74,18 @@ acceptance("Dynamic homepage handling", function () {
await click(".nav-item_categories a"); await click(".nav-item_categories a");
assertOnCategories("/categories"); assertOnCategories("/categories");
}); });
test("it passes query params through", async function (assert) {
await visit("/?test-one=true");
const router = getOwner(this).lookup("service:router");
assert.strictEqual(router.currentURL, "/?test-one=true");
});
test("it removes the _discourse_homepage_rewrite param from the URL", async function (assert) {
await visit("/?_discourse_homepage_rewrite=1&test-one=true");
const router = getOwner(this).lookup("service:router");
assert.strictEqual(router.currentURL, "/?test-one=true");
});
}); });