Replies: 3 comments 1 reply
-
I've started playing around with this, in order to populate the planets pages of pwa-last-stack. Are there events I can listen for which pass the ServiceWorkerCompiler? Then I can hook into that and pass my routes. My thought it to create two listeners, one for processing controller attributes, the other for this, generating a list of routes from a database. This is probably not the right, but it's an idea. The Subscriber should be allowed to return an object with cacheName and networkTimeoutSeconds as well as the routes.. // ServiceWorkerCompiler.php
private function processWorkboxPageCacheEvents(Workbox $workbox, string $body): string
{
if ($workbox->pageCache->enabled === false) {
return $body;
}
$routes = []; // really should be an object with routes, cachName and networkTimeout, and possibly strategy.
$event = new WorkboxPageCacheEvent($routes);
$this->but->dispatch($event);
// maybe the event returns an array of these, so loop through?
$declaration = <<<PAGE_CACHE_RULE_STRATEGY
workbox.recipes.pageCache({
cacheName: '{$workbox->pageCache->cacheName}',
networkTimeoutSeconds: {$workbox->pageCache->networkTimeout},
warmCache: {$routes}
});
PAGE_CACHE_RULE_STRATEGY;
return $body . PHP_EOL . PHP_EOL . trim($declaration);
} |
Beta Was this translation helpful? Give feedback.
-
My idea to help this:
// ServiceWorkerCompiler.php
private function processPageWithAttributesCache(Workbox $workbox, string $body): string
{
$urls = $this->getAllRoutesMarkedWithAnAttribute();
$swrUrl = array_filter(...Only routes with strategy SWR);
$nfUrl = array_filter(...Only routes with strategy NF);
// we include the JS just after
} |
Beta Was this translation helpful? Give feedback.
-
I think I can add the EventSubscriber itself, but I'm still not sure how to add a precached URL via PHP rather than the yaml file. I think we need a demo with more pages and routes. The homepage is the only GET route we have. The examples in the documentation refer to /articles, and indeed we talked about a HackerNews PWA, which makes a lot of sense for caching. Anyone building an app that the want to run offline will want a way to pre-populate the cache programmatically. |
Beta Was this translation helpful? Give feedback.
-
I'd like to be able to create an EventListener that could add urls from a database to the PageCache. There's a sitemap-bundle that does this, we could borrow that idea to pre-cache pages generated from a database.
https://github.com/prestaconcept/PrestaSitemapBundle/blob/4.x/doc/4-dynamic-routes-usage.md#eventlistener-class
For example, suppose we were migrating DogSafeFoods (https://github.com/nikkifurls/dogsafefoods), a 100% static PWA to a dynamic one using Symfony. I imagine that data came from a database, which was then used to generate a static site and the manifest.
There's some overlap between generating a sitemap and caching pages for offline use, as anything I'd list in the manifest for caching I'd also want in the sitemap, and much of what I'd want in the sitemap I'd also want in the cache.
Let's do that in this bundle, too -- I imagine it's a pretty common situation.
It's related to using attributes to define caching strategy for routes, though different because of the dynamic nature.
Beta Was this translation helpful? Give feedback.
All reactions