I'm using the application cache to make my website also work offline.
My manifest looks something like this and is embedded using an <iframe>:
CACHE:
favicon.ico
assets/css/style.css
assets/js/libs/zepto.js
assets/js/app.js
NETWORK:
*
FALLBACK:
/ offline.html
it works as expected when I cut the internet connection -> everything is served via the offline.html page.
but just now I found out that every 404 (e.g. an URL that never existed on the server) also uses the FALLBACK section in the manifest on displays the offline.html.
is there a way to distinguish between being offline (in which case the offline.html is the right choice) and the server returning a 404 (in which case I just want to display the 404 error)?
Appcache will FALLBACK if the original request...
...results in a redirect to a resource with another origin (indicative of a captive portal), or a 4xx or 5xx status code or equivalent, or if there were network errors
(from http://www.whatwg.org/specs/web-apps/current-work/multipage/offline.html#changesToNetworkingModel)
Unfortunately, when a fallback occurs, there's no why to find out why. You can't differentiate between a 500, 404, redirect or failed connection.
Here's a hacky workaround:
FALLBACK:
/connection-test.js connection-failed.js
/ offline.html
You could embed connection-test.js in your offline.html page. connection-test.js loading would indicate the user has a connection, connection-failed.js would indicate there's no connection.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With