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

Don't retry on discarded tabs #163

Merged
merged 3 commits into from
Oct 13, 2023
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
41 changes: 11 additions & 30 deletions package-lock.json

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

3 changes: 1 addition & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,7 @@
"p-retry": "^6.0.0",
"serialize-error": "^11.0.2",
"type-fest": "^4.3.1",
"webext-detect-page": "^4.1.1",
"webext-tools": "^1.1.4"
"webext-detect-page": "^4.1.1"
},
"devDependencies": {
"@parcel/config-webextension": "^2.6.2",
Expand Down
41 changes: 25 additions & 16 deletions source/sender.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import pRetry from "p-retry";
import { isBackground } from "webext-detect-page";
import { doesTabExist } from "webext-tools";
import { deserializeError } from "serialize-error";

import {
Expand All @@ -27,6 +26,7 @@ const _errorTargetClosedEarly =
export const errorTargetClosedEarly =
"The target was closed before receiving a response";
export const errorTabDoesntExist = "The tab doesn't exist";
export const errorTabWasDiscarded = "The tab was discarded";

function isMessengerResponse(response: unknown): response is MessengerResponse {
return isObject(response) && response["__webextMessenger"] === true;
Expand Down Expand Up @@ -120,25 +120,34 @@ async function manageMessage(
}

if (
// Don't retry sending to the background page unless it really hasn't loaded yet
(target.page !== "background" && error instanceof MessengerError) ||
// Page or its content script not yet loaded
error.message === _errorNonExistingTarget ||
// `registerMethods` not yet loaded
String(error.message).startsWith("No handlers registered in ")
!(
// If NONE of these conditions is true, stop retrying
// Don't retry sending to the background page unless it really hasn't loaded yet
(
(target.page !== "background" &&
error instanceof MessengerError) ||
// Page or its content script not yet loaded
error.message === _errorNonExistingTarget ||
// `registerMethods` not yet loaded
String(error.message).startsWith("No handlers registered in ")
)
)
) {
if (
browser.tabs &&
typeof target.tabId === "number" &&
!(await doesTabExist(target.tabId))
) {
throw error;
}

if (browser.tabs && typeof target.tabId === "number") {
try {
const tabInfo = await browser.tabs.get(target.tabId);
if (tabInfo.discarded) {
throw new Error(errorTabWasDiscarded);
}
} catch {
throw new Error(errorTabDoesntExist);
}

log.debug(type, seq, "will retry. Attempt", error.attemptNumber);
} else {
throw error;
}

log.debug(type, seq, "will retry. Attempt", error.attemptNumber);
},
}
).catch((error: Error) => {
Expand Down