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

Legger til custom event for history.pushState #265

Merged
merged 4 commits into from
May 6, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
23 changes: 23 additions & 0 deletions packages/client/src/events.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ export type CustomEvents = {
menuclosed: void;
clearsearch: void;
closemenus: void; // Currently fired only from other apps
historyPush: void;
};

export type MessageEvents =
Expand All @@ -43,3 +44,25 @@ export function createEvent<TName extends keyof CustomEvents>(
export const analyticsReady = new CustomEvent("analytics-ready-event", {
bubbles: true,
});

// Emits events on navigation in SPAs
export const initHistoryEvents = () => {
const pushStateActual = window.history.pushState.bind(window.history);
let currentPathname = window.location.pathname;

window.history.pushState = (
...args: Parameters<typeof window.history.pushState>
) => {
// Delay slightly to allow SPAs to update their state
setTimeout(() => {
const newPathname = window.location.pathname;
if (newPathname !== currentPathname) {
dispatchEvent(createEvent("historyPush", {}));
currentPathname = newPathname;
console.log(`Navigated to ${newPathname}`);
}
}, 250);

return pushStateActual(...args);
};
};
3 changes: 2 additions & 1 deletion packages/client/src/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ import "./views/chatbot-wrapper";
import "./views/sticky";
import "./views/user-menu";
import { addFaroMetaData } from "./faro";
import { analyticsReady, createEvent } from "./events";
import { analyticsReady, createEvent, initHistoryEvents } from "./events";
import { type ParamKey } from "decorator-shared/params";
import { param, hasParam, updateDecoratorParams, env } from "./params";
import { makeEndpointFactory } from "decorator-shared/urls";
Expand Down Expand Up @@ -125,6 +125,7 @@ window.addEventListener("activecontext", (event) => {
});

const init = async () => {
initHistoryEvents();
initAnalytics();

api.checkAuth().then((authResponse) => {
Expand Down
2 changes: 2 additions & 0 deletions packages/client/src/views/ops-messages.ts
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,8 @@ class OpsMessages extends HTMLElement {
this.messages = opsMessages;
this.render();
});

window.addEventListener("historyPush", () => this.render());
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
window.addEventListener("historyPush", () => this.render());
window.addEventListener("historyPush", this.render);

}

render() {
Expand Down
Loading