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

#6649: Exclude navigation events that should not trigger page updates #7178

Merged
merged 8 commits into from
Dec 22, 2023
Merged
Show file tree
Hide file tree
Changes from 3 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
2 changes: 1 addition & 1 deletion jest.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ const config = {
"^.+\\.txt$": "<rootDir>/src/testUtils/rawJestTransformer.mjs",
},
transformIgnorePatterns: [
"node_modules/(?!@cfworker|escape-string-regex|filename-reserved-regex|filenamify|idb|webext-|p-timeout|p-retry|p-defer|p-memoize|serialize-error|strip-outer|trim-repeated|mimic-fn|urlpattern-polyfill|url-join|uuid|nanoid|use-debounce|copy-text-to-clipboard|linkify-urls|create-html-element|stringify-attributes|escape-goat|stemmer|uint8array-extras)",
"node_modules/(?!@cfworker|escape-string-regex|filename-reserved-regex|filenamify|idb|webext-|p-timeout|p-retry|p-defer|p-memoize|serialize-error|strip-outer|trim-repeated|mimic-fn|urlpattern-polyfill|url-join|uuid|nanoid|use-debounce|copy-text-to-clipboard|linkify-urls|create-html-element|stringify-attributes|escape-goat|stemmer|uint8array-extras|one-event)",
],
setupFiles: [
"dotenv/config",
Expand Down
12 changes: 12 additions & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,7 @@
"mustache": "^4.2.0",
"nunjucks": "^3.2.4",
"object-hash": "^2.2.0",
"one-event": "^4.2.0",
"one-mutation": "^2.1.0",
"p-defer": "^4.0.0",
"p-memoize": "^7.0.0",
Expand Down
5 changes: 2 additions & 3 deletions src/contentScript/contentScriptCore.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ import registerMessenger from "@/contentScript/messenger/registration";
import registerBuiltinBricks from "@/bricks/registerBuiltinBricks";
import registerContribBlocks from "@/contrib/registerContribBlocks";
import brickRegistry from "@/bricks/registry";
import { handleNavigate, initNavigation } from "@/contentScript/lifecycle";
import { initNavigation } from "@/contentScript/lifecycle";
import { initTelemetry } from "@/background/messenger/api";
import { ENSURE_CONTENT_SCRIPT_READY } from "@/contentScript/ready";
import { initToaster } from "@/utils/notify";
Expand Down Expand Up @@ -65,8 +65,7 @@ export async function init(): Promise<void> {
initTelemetry();
initToaster();

await handleNavigate();
initNavigation();
Copy link
Contributor Author

Choose a reason for hiding this comment

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

initNavigation should deal with this directly

Kinda suggested here: #7030 (comment)

void initNavigation();

void initSidebarActivation();

Expand Down
33 changes: 28 additions & 5 deletions src/contentScript/lifecycle.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ import { NAVIGATION_RULES } from "@/contrib/navigationRules";
import { testMatchPatterns } from "@/bricks/available";
import reportError from "@/telemetry/reportError";
import { compact, groupBy, intersection, uniq } from "lodash";
import oneEvent from "one-event";
import { resolveExtensionInnerDefinitions } from "@/registry/internal";
import { traces } from "@/background/messenger/api";
import { isDeploymentActive } from "@/utils/deploymentUtils";
Expand Down Expand Up @@ -639,9 +640,31 @@ export async function reactivateTab(): Promise<void> {
await handleNavigate({ force: true });
}

export function initNavigation() {
window.navigation?.addEventListener("navigate", async (event) => {
// Delay slightly to avoid catching events that navigate away from the page
setTimeout(handleNavigate, 0);
});
// Ideally we only want to catch local URL changes, but there's no way to discern
// navigation events that cause the current document to unload in the `navigate ` event.
async function onNavigate(event: NavigateEvent): Promise<void> {
// Ignore navigations to external pages
if (
!event.destination.url.startsWith(location.origin) ||
event.downloadRequest !== null // Specifically `null` and not `''`
) {
return;
}

try {
await oneEvent(window, "beforeunload", {
signal: AbortSignal.timeout(0),
});
} catch {
// It timed out before the "beforeunload" event, so this is a same-document navigation
await handleNavigate();
}
}

export async function initNavigation() {
// Initiate PB for the current page
await handleNavigate();

// Listen to page URL changes
window.navigation?.addEventListener("navigate", onNavigate);
}
Loading