Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Merged
merged 1 commit into from
May 9, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -21,25 +21,31 @@ export default function applyRouterHomepageOverrides(router) {
}
}

const homepageRewriteParam = "_discourse_homepage_rewrite";

/**
* Returns a magic URL which `discovery-index` will redirect to.
* We watch for this, and then perform the rewrite in the router.
*/
export function homepageDestination() {
return `/${defaultHomepage()}?_discourse_homepage_rewrite=1`;
return `/${defaultHomepage()}?${homepageRewriteParam}=1`;
}

function rewriteIfNeeded(url, transition) {
const intentUrl = transition?.intent?.url;
if (
intentUrl?.startsWith(homepageDestination()) ||
(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 = "/";
if (params) {
url += `?${params}`;
const searchParams = new URLSearchParams(params);
searchParams.delete(homepageRewriteParam);
if (searchParams.size) {
url += `?${searchParams.toString()}`;
}
}
}
return url;
Expand Down
14 changes: 14 additions & 0 deletions app/assets/javascripts/discourse/tests/acceptance/homepage-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -74,4 +74,18 @@ acceptance("Dynamic homepage handling", function () {
await click(".nav-item_categories a");
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");
});
});