-
Notifications
You must be signed in to change notification settings - Fork 6
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Is workbox BroadcastCacheUpdate supported? #4
Comments
You could inject you own code with
So you would be better off using Here is the documentation from workbox (last is a demo, look at the code in devtools) |
The inject approach would indeed work. But the API that comes with this plugin actually goes quite a long way. So for example: This does in fact build successfully. And the output to sw.js is non-breaking. But unfortunately it passes the broadcastUpdate object as is. So this then would be a feature request, if this is doable. Take this object:
... and produce:
...in the output sw.js as per the documentation you have mentioned. I think this would be very much in the spirit of this plugin, in that it is producing a fully functional SW out of just the json config in package.json But you know... only if it's not too much bother :-) |
This plugin is only an interface to workbox-build, so it basically only calls However, I think that this is already in the workbox-build configuration: (from https://developers.google.com/web/tools/workbox/modules/workbox-build#full_generatesw_config)
If this code doesn't work, open an issue with workbox and tell them that either the docs are wrong or that something isn't working properly. |
What I am saying is that some options passed to generateSW work properly, while others do not. So this works:
But this does not:
So the plug-in does not interface with that aspect of generateSW. If given the latter config, the sw.js is not produced in ./dist/ |
I will look into this. |
Seems to be a problem with the documentation and workbox itself: GoogleChrome/workbox/issues/1289 |
This will get fixed in the next workbox release.
|
Ah... I see. I will leave this issue open until we hear from upstream and we can test it. Thanks! |
There is another issue with workbox 😞 (GoogleChrome/workbox#1334). @goofblob The fix is in workbox beta.1. Could you please test it? "cache": {
"inDev": true,
"runtimeCaching": [
{
"urlPattern": "https://raw.githubusercontent.com/parcel-bundler/website/01a1f7dd/src/assets/parcel@3x.png",
"handler": "cacheFirst",
"options": {
"broadcastUpdate": {
"channelName": "my-update-channel"
}
}
}
]
} |
Hmm... I do not get that specific log error, but:
The reason I believe this might be the case is the following. The Workbox spec says that the broadast channel should be registered with the following syntax:
But the plugin generates:
The difference is For the sake of completeness, I listed to the broadcast within an onLoad listener on the index page as follows:
This is more or less taken as is from the Workbox documentation. |
That |
The service worker generation works with {
"urlPattern": "...",
"handler": "cacheFirst",
"options": {
"broadcastUpdate": {
"channelName": "api-updates"
},
"cacheableResponse": {
"statuses": [
0
]
}
}
} and workbox-3-beta1. I however can't test the actual functionality. The logger is fixed in version 0.2.0 |
Hmm... In order to test it, we need to listen to the broadcastUpdate event:
Problem is that if I put this in |
It worked for me: config: "cache": {
"runtimeCaching": [
{
"urlPattern": "http://localhost:1234/cache.jpg",
"handler": "staleWhileRevalidate",
"options": {
"broadcastUpdate": {
"channelName": "api-updates"
}
}
}
]
} index.js const updatesChannel = new BroadcastChannel('api-updates');
updatesChannel.addEventListener('message', event => {
console.log(event);
}); index.html contained Did you use |
Hmm... Still not working for me. Yes, I did use In
And in
But when I make changes to the API, and go through the reloads of the site nothing happens. So I get served the stale .json from the Service Worker cache just fine on the first go, and then the refreshed one on the second go. But I do not get the "API Updated." console message on the first go when the .json has been successfully refreshed in the background. Or the payload. If it works in your tests, then that must mean that the functionality does work. There is something else about my setup that interferes. So if you can see nothing wrong with my code here, I think you can close the issue. I'll just have to bash my head against my code a bit more until it works. Thanks again for your work on the plugin! |
Workbox checks the headers to determine whether the file updated, by default With my example: The event fires on the first reload (after getting the old response) and the new response gets returned on the second reload. |
Right. That's how it is intended to work. What happens in my use case is that the update does happen in the background, and that is reflected in the second reload. But the updatesChannel event listener does not get triggered to give me the console output it should on the first load. My intended use for this is that I want to serve stale data from the service worker cache on the first load, and then automatically update that data without reload when/if the background service worker process finds fresh API data. But if the event does not get triggered, I cannot do this live API data update. It's not mission critical, but that is pretty much the standard use case for staleWhileRevalidate, and it's bugging me that I have not yet managed to get it working. But as I say, if the event triggers in your example, there is something I'm doing wrong on my end, so this would not be an issue with the plugin. |
If so, how is it implemented in package.json, and how would a page listen to the notification?
The text was updated successfully, but these errors were encountered: