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

Trigger the URL_CHANGED event only if from/to URL are different #516

Merged
merged 2 commits into from
Feb 4, 2025
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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion src/linkpreviews.js
Original file line number Diff line number Diff line change
Expand Up @@ -273,7 +273,9 @@ export class LinkPreviewsElement extends LitElement {

_handleRootDOMChanged = (e) => {
// Trigger the setup again since the DOM has changed
this.setupTooltips();
if (this.config) {
this.setupTooltips();
}
};

connectedCallback() {
Expand Down
21 changes: 18 additions & 3 deletions src/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -214,6 +214,9 @@ export function setupHistoryEvents() {
for (const methodName of ["pushState", "replaceState"]) {
const originalMethod = history[methodName];
history[methodName] = function () {
// Save the from URL to compare against before triggering the event.
const fromURL = new URL(window.location.href);

const result = originalMethod.apply(this, arguments);

// Dispatch the event only when the third argument (url) is passed.
Expand All @@ -222,9 +225,21 @@ export function setupHistoryEvents() {
// https://developer.mozilla.org/en-US/docs/Web/API/History/pushState
// https://developer.mozilla.org/en-US/docs/Web/API/History/replaceState
if (arguments.length === 3) {
const event = new Event(EVENT_READTHEDOCS_URL_CHANGED);
event.arguments = arguments;
dispatchEvent(event);
const toURL = arguments[2];

// TODO: we can't import this here -- it has to be at the top.
// We can't import it at the top due to circular dependencies.
// I'm using the hardcoded name for now.
//
// import { DOCDIFF_URL_PARAM } from "./docdiff";
toURL.searchParams.delete("readthedocs-diff");

// Dispatch the event only if the new URL is not just the DOCDIFF_URL_PARAM added.
if (toURL.href !== fromURL.href) {
const event = new Event(EVENT_READTHEDOCS_URL_CHANGED);
event.arguments = arguments;
dispatchEvent(event);
}
}

return result;
Expand Down