-
-
Notifications
You must be signed in to change notification settings - Fork 2
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
document workbox caching strategies #78
Comments
I've read, and re-read, this: https://stackoverflow.com/questions/46830493/is-there-any-way-to-cache-all-files-of-defined-folder-path-in-service-worker But am still trying to understand how phpwa.yaml works in conjunction with the manifest. For example, in the phpwa-demo, there's a hello_controller.js in assets. Even though it's not being used in the demo, I'd like to cache it so if it is, it's available to be run offline. |
Hmm, it looks like it does cache the controllers, so they're available offline. How do I cache an API route? I want the app to grab the existing items (tasks) when first loading, and serve that or the most recent version when it's offline. serviceworker:
enabled: true
src: "sw.js"
skip_waiting: true
workbox:
warm_cache_urls:
- 'app_homepage'
- '_api_/items{._format}_get_collection'
page_fallback: 'app_homepage' I added the route to warm_cache_urls, but that's not right. (Also, shouldn't it be called warm_cache_routes?) |
https://developer.chrome.com/docs/workbox/modules/workbox-strategies I guess what I'm looking for is how to map phpwa.yaml configuration to the workbox caching strategies. |
The assets are not put into the manifest file. Those files are served by the service worker if Workbox is used.
There are two types of cached pages: the ones the user visits and the ones listed under this configuration key. You can use the route name (with or without parameters) or any paths.
|
Thanks, that's helpful. What do you think of allowing the user to define the caching strategy based on the url/route? I think assets should be CacheOnly, and they should be cached on startup (because of the hash). That would get rid of a lot of extra network requests. Then I'd like to be able to define the cache for the API calls. My route in the above example isn't caching at all ('api/items{._format}_get_collection'), is there something I need to do. What do you think of a phpwa:dump or :debug to show some details? Where can I look to see what routes are cached and how? |
|
Is there a way to configure:
As far as dump, even dumping the service worker (with comments?) would be fine. I know its dynamically created, but I'm having a hard time even seeing it or debugging it, as I'm not sure what actually triggers the ServiceWorkerCompiler. |
It seems possible to me by adding several rules based on the path. import {registerRoute} from 'workbox-routing';
import {CacheOnly, CacheFirst, NetworkFirst, StaleWhileRevalidate} from 'workbox-strategies';
registerRoute(({url}) => url.pathname.startsWith('/assets'), new CacheOnly());
registerRoute(({url}) => url.pathname.startsWith('/api'), new NetworkFirst());
registerRoute(({url}) => url.pathname.endsWith('.json'), new StaleWhileRevalidate());
|
Since all assets have a hash, we don't need the stale-while-revalidate, it causes lots of network requests when the app is offline. I think /assets should be cache only, I think you already warm it up. Static resources cache |
I need to acheive a similar scenario that @tacman is exposing, but I'm quite confused about where to put this chunk of code. Can you specify where is the right place? Inside |
What about an attribute with an optional caching strategy? #[PWA(cachingStrategy: PWA::CacheFirst)]
#[Route('/terms')] OR #[Route('/terms', ['options' => ['pwa_strategy' => PWA::CacheOnly)] If an attribute, it can even be done at the class level, then all controllers in that class would have the same strategy. #[PWA(cachingStrategy: PWA::CacheFirst)]
class PageController extends AbstractController I wrote something similar (checks attributes at the class and route level), and could submit a similar PR if you're interested. https://github.com/survos/BootstrapBundle/blob/main/src/SurvosBootstrapBundle.php#L66-L106 You'd have to do the magic of injecting the routes with the approriate cache strategy, as I haven't figured that out yet! Obviously, it's an enhancement to listing the routes/urls or adding the preg, both of which seem to be somewhat complicated at the moment, though could be fixed with documentation. |
In the last bug release
Your configuration file is correct and I can see the route in the cache list.
I really like it. Having an attribute at the same level of a route looks great. I am not sure how to manage that for now... |
@davidromani I didn't realize either that you can simply add your own javascript to the sw.js file. I'll add an issue to request that pwa:create:sw generate a more complete file with some comments. |
This is a good idea. The command just copy-paste the file src/Resources/sw-skeleton.js |
Hi @tacman, Can you explain how attribute detection works?
With all those questions in mind, I am wondering if the way to go be simplified. For example and to be in line with the existing caching system from the configuration file: #[PWA(strategy: 'staleWhileRevalidate', regex: '\/foo/bar/\d+$', cacheName: 'foobars', broadcast: true, broadcastChannel: 'foobar-updates')]
class PageController extends AbstractController In this case, we only need to find all |
Since I'm particularly interested in offline capacity, I was mostly thinking of the attributes to facilitate warming the URL cache. To that end:
The regular caching strategies (not focused on warming the cache), my idea for the attribute would be to find the route and parameters and pass them to the compiler, so that #[Pwa(strategy: 'NetworkOnly')
#[Route('/onlineStatus', name: 'app_status')
#[Pwa(strategy: 'NetworkThenCache')
#[Route('/{_locale}/projectStatus/{projectCode}', name: 'project_status') would be the same as pwa:
serviceworker:
enabled: true
src: "sw.js"
workbox:
page_caches:
NetworkThenCache:
urls:
- 'project_status' # Any route that matches, regardless of parameters???
NetworkOnly:
enabled: true
network_timeout: 30
urls:
- 'app_status' # Simple route name
I'm pretty sure we can pass the route match regex from the Symfony router to the compiler as well. |
I'll mock something up for the attributes and put it in the debug toolbar, all the attributes are processed during the compiler pass and saved to /var/cache. |
https://github.com/survos/SurvosPwaExtraBundle/blob/main/src/SurvosPwaExtraBundle.php#L89-L129 |
Are you familiar with using the CacheWarmer? symfony/symfony#54237 I started the implementation (evidently the compilerPass in the wrong place for what I'm doing), I suspect I'll eventually figure it out. |
I close this issue as the page cahce evolved a lot. I opened a dedicated one for the CacheWarmer question. |
This thread has been automatically locked since there has not been any recent activity after it was closed. Please open a new issue for related bugs. |
Description
Because AssetMapper generates a hash for the js/css files, they can be safely cached in the manifest. This is true for both dev and prod.
What's the configuration it for putting the /assets path in the manifest, so those assets are only loaded once?
The text was updated successfully, but these errors were encountered: