gildas-lormeau/SingleFile · error · Error

ERR_HOST_FETCH

ERR_HOST_FETCH

Error message

Host fetch error (SingleFile)

What it means

hostFetch() in SingleFile's content-fetch only works when running with a host/extension bridge available; when the environment can't provide it (the else branch), it throws ERR_HOST_FETCH with the message "Host fetch error (SingleFile)". Callers like fetchResource receive this when the privileged fetch channel is absent.

Source

Thrown at src/lib/single-file/fetch/content/content-fetch.js:124

						document.removeEventListener(FETCH_RESPONSE_EVENT, onResponseFetch, false);
						if (event.detail.response) {
							resolve({
								status: event.detail.status,
								headers: new Map(event.detail.headers),
								arrayBuffer: async () => event.detail.response
							});
						} else {
							reject(event.detail.error);
						}
					}
				} else {
					reject();
				}
			}
		});
		return result;
	} else {
		throw new Error(ERR_HOST_FETCH);
	}
}

export {
	fetchResource as fetch,
	frameFetch
};

async function fetchResource(url, options = {}, useHostFetch = true) {
	try {
		const fetchOptions = {
			cache: options.cache || "force-cache",
			headers: options.headers,
			referrerPolicy: options.referrerPolicy || "strict-origin-when-cross-origin"
		};
		let response;
		try {
			if ((options.referrer && !USE_HOST_FETCH) || !useHostFetch) {

View on GitHub (pinned to 517fb7c5cf)

Solutions

  1. Reload the tab (re-injects the content script) so it reconnects to the refreshed extension background context
  2. Ensure the background/service-worker listener for the fetch message is registered and alive before fetchResource runs
  3. Wrap hostFetch calls so the error propagates with ERR_HOST_FETCH and fall back to an in-page fetch where the host bridge is optional
  4. Update SingleFile to the latest version if extension updates repeatedly orphan contexts

Example fix

// before
const result = await hostFetch(url, options);
// after
let result;
try {
  result = await hostFetch(url, options);
} catch (e) {
  if (String(e.message).includes(ERR_HOST_FETCH)) result = await fallbackFetch(url, options);
  else throw e;
}
Defensive patterns

Strategy: fallback

Validate before calling

const hostAvailable = typeof browser !== "undefined" && browser.runtime && browser.runtime.id;
if (!hostAvailable) throw new Error("SingleFile host context unavailable; use in-page fetch");

Type guard

function canHostFetch() {
  try { return typeof browser !== "undefined" && !!browser.runtime?.id && typeof browser.runtime.sendMessage === "function"; }
  catch { return false; }
}

Try / catch

try {
  const resource = await fetch(url, options);
} catch (e) {
  if (String(e.message).includes("Host fetch error") || e.message === "ERR_HOST_FETCH") {
    resource = await plainFetch(url, options); // in-page fallback
  } else throw e;
}

Prevention

When it happens

Trigger: fetchResource calls hostFetch while the extension runtime/handler is unavailable: content script running without the background listener registered, extension context invalidated after an update/reload, or the library used outside the extension host context.

Common situations: Extension was auto-updated while a tab's content script was still alive (orphaned context); SingleFile used in a page/embedded context where browser.runtime messaging is unavailable; background service worker asleep or crashed in Manifest V3.

Related errors


AI-assisted analysis of gildas-lormeau/SingleFile@517fb7c5cf (2026-09-01). Data as JSON: /api/errors/650051cddb28c28f. Report an issue: GitHub.