FIX: remove trailing slashes and query params on meta-tag-updater's canonical url (#24445)

* FIX: remove trailing slashes and query params on meta-tag-updater's canonical url
This commit is contained in:
Renato Atilio 2023-11-27 12:28:07 -03:00 committed by GitHub
parent ac9e804dbe
commit d93be8c7a1
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 62 additions and 3 deletions

View File

@ -1,3 +1,4 @@
import { getCanonicalUrl } from "discourse/lib/url";
import { getAbsoluteURL } from "discourse-common/lib/get-url";
export default {
@ -21,7 +22,9 @@ export default {
twitterTitle?.setAttribute("content", title);
twitterUrl?.setAttribute("content", absoluteUrl);
canonicalUrl?.setAttribute("href", absoluteUrl);
if (canonicalUrl) {
canonicalUrl.setAttribute("href", getCanonicalUrl(absoluteUrl));
}
});
},
};

View File

@ -39,6 +39,9 @@ const SERVER_SIDE_ONLY = [
// scrolling to "suggested topics".
const JUMP_END_BUFFER = 250;
const ALLOWED_CANONICAL_PARAMS = ["page"];
const TRAILING_SLASH_REGEX = /\/$/;
export function rewritePath(path) {
const params = path.split("?");
@ -506,4 +509,22 @@ export function getEditCategoryUrl(category, subcategories, tab) {
return getURL(url);
}
export function getCanonicalUrl(absoluteUrl) {
const canonicalUrl = new URL(absoluteUrl);
canonicalUrl.pathname = canonicalUrl.pathname.replace(
TRAILING_SLASH_REGEX,
""
);
const allowedSearchParams = new URLSearchParams();
for (const [key, value] of canonicalUrl.searchParams) {
if (ALLOWED_CANONICAL_PARAMS.includes(key)) {
allowedSearchParams.append(key, value);
}
}
canonicalUrl.search = allowedSearchParams.toString();
return canonicalUrl.toString();
}
export default _urlInstance;

View File

@ -148,7 +148,7 @@ acceptance("Admin - Site Settings", function (needs) {
});
test("category name is preserved", async function (assert) {
await visit("admin/site_settings/category/basic?filter=menu");
await visit("/admin/site_settings/category/basic?filter=menu");
assert.strictEqual(
currentURL(),
"/admin/site_settings/category/basic?filter=menu"
@ -156,7 +156,7 @@ acceptance("Admin - Site Settings", function (needs) {
});
test("shows all_results if current category has none", async function (assert) {
await visit("admin/site_settings");
await visit("/admin/site_settings");
await click(".admin-nav .basic a");
assert.strictEqual(currentURL(), "/admin/site_settings/category/basic");

View File

@ -2,6 +2,7 @@ import { setupTest } from "ember-qunit";
import { module, test } from "qunit";
import sinon from "sinon";
import DiscourseURL, {
getCanonicalUrl,
getCategoryAndTagUrl,
prefixProtocol,
userPath,
@ -178,4 +179,38 @@ module("Unit | Utility | url", function (hooks) {
"in-page anchors call replaceState with the url fragment"
);
});
test("getCanonicalUrl", function (assert) {
assert.strictEqual(
getCanonicalUrl("http://eviltrout.com/t/this-is-a-test/1/"),
"http://eviltrout.com/t/this-is-a-test/1",
"trailing slashes are removed"
);
assert.strictEqual(
getCanonicalUrl(
"http://eviltrout.com/t/this-is-a-test/1/?page=2&u=john&not_allowed=true"
),
"http://eviltrout.com/t/this-is-a-test/1?page=2",
"disallowed query params are removed"
);
assert.strictEqual(
getCanonicalUrl("http://eviltrout.com/t/this-is-a-test/2"),
"http://eviltrout.com/t/this-is-a-test/2",
"canonical urls are not modified"
);
assert.strictEqual(
getCanonicalUrl("http://eviltrout.com/t/this-is-a-test/2/?"),
"http://eviltrout.com/t/this-is-a-test/2",
"trailing /? are removed"
);
assert.strictEqual(
getCanonicalUrl("http://eviltrout.com/t/this-is-a-test/2?"),
"http://eviltrout.com/t/this-is-a-test/2",
"trailing ? are removed"
);
});
});