Comments (6)
Hi @willdurand,
Yes, you can have custom entry file for SW where you can handle fetch
events as you want, but you cannot override behavior for assets listed in caches
option. i.e. if you do some API request and want to handle them in SW, just set ServiceWorker.entry
option to some JS file, like this:
var OfflinePlugin = require('offline-plugin');
module.exports = {
// ...
plugins: [
// ... other plugins
new OfflinePlugin({
// All options are optional
caches: 'all',
scope: '/',
updateStrategy: 'all',
version: 'v1',
ServiceWorker: {
output: 'sw.js',
entry: 'sw-handler.js'
},
AppCache: {
directory: 'appcache/'
}
})
]
// ...
}
and in sw-handler.js
:
self.addEventListener('fetch', () => {
// ...
});
Is it what you wanted?
from offline-plugin.
Okay, I see what you do in your fork. So basically your SPA and for all (some?) requests server returns contents of /
and you want same for SW, right? Yeah, it would be unfortunate to cache page for every such request separately.
from offline-plugin.
So basically you SPA and for all (some?) requests server returns contents of / and you want same for SW, right? Yeah, it would be unfortunate to cache page for every such request separately.
Exactly! The SPA has a single entrypoint (/
) and uses the URI to determine which data to load (e.g. /123
). This works well online, but I want to be able to refresh the page offline, and still get the content. That is why redirecting any request matching (/<identifier>
) to the cached version of /
is interesting to me.
Should the sw-handler.js
file be a modified version of sw-template.js
?
from offline-plugin.
Should the sw-handler.js file be a modified version of sw-template.js?
No, it's an additional file to generated SW, it does not replace anything. Unfortunately there is no way at a moment to alter sw-template.js
😒
from offline-plugin.
Ok, great. Thank you for your answers. I am not sure to be able to only leverage the sw-handler.js
file right now, but it is definitely good to know :)
from offline-plugin.
Tracking cache mapping issue here now: #59
Not sure about custom templates though, it reduces whole point of this plugin then since it's an internal thing which depends on particular version of the plugin. I guess just forking plugin and adopt template for your app is fine.
from offline-plugin.
Related Issues (20)
- Question - SW caching all assets site-wide HOT 1
- In Angular 9, files inside directories are not being cached
- Ideas for Documentation to Help with Troubleshooting
- Some files missing in cache on SW first load/install HOT 1
- Caching subpath urls
- Caching fails to download assets in Chrome 85 HOT 1
- Issue with default path
- [question] Having Multiple runtime installations
- Problem with Webpack 5.0 HOT 17
- Micro Front-ends structure with single-spa
- Auto update doesn't work
- After removing redux-offline, how do we ensure that client's who have old versions get the latest update?
- `onInstalling` doesn't seem to be called, because `ignoreInstalling` is always true
- offline-plugin does not serve assets from service worker
- Webpack 5 compatibility HOT 3
- The "path" argument must be of type string. Received undefined HOT 4
- Cache Images from AWS S3
- Pass ServiceWorker version to the install events
- Terser Plugin issue while building with webpack-cli HOT 3
- upgrade ejs package | CVE-2022-29078 HOT 1
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from offline-plugin.