NekR / offline-plugin

Offline plugin (ServiceWorker, AppCache) for webpack (https://webpack.js.org/)
MIT License
4.52k stars 295 forks source link

index.html will not be cached no matter what #429

Closed rafi16jan closed 5 years ago

rafi16jan commented 5 years ago

Hello, I was trying to deploy my SPA app especially to a dynamic subdirectory (means that I won't build a specific publicPath to every subdirectory I deploy, works with relativePaths) and it all works except if the user access the app with index.html it won't cache. I've already tried to add external ['index.html'] or ['/index.html'] or ['/'] or in the cache options I provide values like the former along with :rest:. And no success, everyting seems doesn't work even some of those makes the sw to cache / (the domain root) which is weird. Sometimes I can't force the user to just access the app like "/subdir/" sometimes they access it with "/subdir/index.html"

rafi16jan commented 5 years ago

Btw, I use Chrome DevTools to see the cached assets, in the application bar

rafi16jan commented 5 years ago

I found a weird case. If I put appShell: 'index.html' to the options. It still doesn't work after offline but the error are different, it's "This site can't be reached" its usually "No Internet" and when I check the network tab (on online) the index.html is stored from service worker, and it gives me an error from sw.js but from line 1 (because its minifed and prettifying it from devtools doesn't help)

![This is the network tab, we can see that the index.html is stored from service worker This is the network tab, we can see that the index.html is stored from service worker

This is if I don't put appShell on the options, everything is cached except index.html This is if I don't put appShell on the options, everything is cached except index.html

This is if I open the app without index.html, it works perfectly with or without appShell This is if I open the app without index.html, it works perfectly with or without appShell

This is the application tab, showing there are an error in sw installation This is the application tab, showing there are an error in sw installation

This is the console error This is the console error

rafi16jan commented 5 years ago

I found a fix, turnout if you open the dropdown in the FetchEvent warning, it will give you the line that causes the error. And when I prettify it and changes the following

,e.respondWith(f)

to

;try{e.respondWith(f)}catch(error){}

The app will work when offline.

I don't know if this is a bug or if I have a wrong configuration, but you should definitely look into this. FYI, maybe this is about the update strategy or something about that because the line above it reference to o === 'network-first' and when I console.log the o variable it prints cache-first

GGAlanSmithee commented 5 years ago

@rafi16jan

It's good that you found a solution to your problem, but I'm a bit worried that your fix is to swallow the error in the minified code. Firstly, if you ever update or rebuild offline-plugin, your problem will come back, and secondly, your fix might actually be hiding a real problem, that will come back later.

Regarding your problem, your case should be handled by the default rewrites setting. Try experimenting with it if you have a different need than the default behaviour.

Closing this, please re-open if you stil cannot solve it.