'Service worker: What to return if no network, and cache miss for a static resource (caching strategy)

I have various caching strategies, .e.g.

A GET for a page: Try network, then cache, else a default page.

However if it is a static resource like a .css or .js file. What should one return if it fails to get a response from the network? If it is not obtained within 4s I know the system is down, and a long timedout out fetch is not ideal.

At the moment I return a 404, but semantically this is not quite right, its not that the resource was not found, it was that the request never got to the server. But it seems like the most applicable 4XX series (client failure) code to me.

    get404Response(statusText = "") {
        const init = { status: 404, statusText: statusText };
        return new Response("", init);
    }

Alternatively I don't want to raise a fetch error, because in the chrome dev tools, under application for the service worker, it tracks how many errors your SW has raised, and I am trying to keep track of legitamate errors.



Solution 1:[1]

504 seems like a good fit: wikipedia:

504 Gateway Timeout The server was acting as a gateway or proxy and did not receive a timely response from the upstream server.

I stumped across it here: https://developer.mozilla.org/en-US/docs/Web/API/Request/cache#value

If one requests a cached response with only-if-cached, and there is no cached response, then the brower returns a 504.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 run_the_race