Skip to content

Instantly share code, notes, and snippets.

@noamr
Last active November 29, 2022 01:48
Show Gist options
  • Save noamr/c9efc56220820d5dd716eb6f64c11146 to your computer and use it in GitHub Desktop.
Save noamr/c9efc56220820d5dd716eb6f64c11146 to your computer and use it in GitHub Desktop.
addEventListener("fetch", e => {
if (e.request.destination !== "image" || // Only do this when requesting an image
request.mode === "no-cors") // We don't know the status of no-cors images
return;
e.respondWith((async () => {
try {
const response = await fetch(e.request);
if (response.ok)
return response;
} catch {
}
return Response.redirect("/your/fallback/url.svg");
})());
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment