-
Notifications
You must be signed in to change notification settings - Fork 98
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
Implement precaching #25
Comments
To elaborate, the precaching should be done for all registered scripts and styles, not just the ones that are enqueued. We'd also need to include the offline page (#23) among the precached URLs. In that regard, one thing that comes to mind in regards to precaching the offline page is making sure that all assets linked to from the offline page are also included in the precache manifest. @jeffposnick Other than just loading the offline page in the browser to do runtime caching, is there any facility for Workbox to identify such assets to add them for precaching? I assume not, and it would seem more practical in this case to load the offline page in a hidden iframe on the client instead. The key point here is that themes and plugins will be adding an arbitrary number of scripts and styles in addition to the images that a user may be adding in the CMS to the offline page's content, so there isn't a static list of files we can rely on as part of a build process. |
See also WordPress/WordPress-Coding-Standards#1439 as there is |
Existing SolutionsIn looking at existing WordPress plugins that implement offline support, here is a look at existing solutions based on the doc created as part of #2: Offline ContentUses runtime caching and only deletes stale caches upon installation (ref). Offline ShellDoes do precaching but it requires the user to select the the list of assets to cache: The assets listed are just taken by grabbing all files in the active theme. This is not ideal because:
Super Progressive Web AppsAs far as I can see, only the requests for the start page and the offline page are cached: https://github.com/SuperPWA/Super-Progressive-Web-Apps/blob/a06f52e1b0ddb74057ffb6d22545a9ae285ffe12/public/sw.php#L109-L125 The assets that these pages load (e.g. scripts, styles, images) are not cached for offline. Progressive WordPressIn the same way as SuperPWA, only the requests for the home page and offline page are cached upon installation:
The assets that the offline page depends on do not appear to be cached upon installation. LH Web ApplicationOffline page is presented as a field to provide a page ID: This the URL of the offline is then pre-cached but not the assets it depends on. Minimum Configuration WordPress PWAUses Workbox. Admin offers precaching screen which allows you to cache arbitrary URLs, including a facility to scan static asset URLs to pre-populate the manifest: There is an admin screen for selecting an offline page: Only the offline page URL is precached. No assets are precached. Other plugins? |
As @westonruter mentioned "Progressive WordPress" won't cache any assets on installation. But since it caches everything "network first", the chance is pretty high the required resources for the offline page are stored as well after the first session. However I think we need to find a more stable solution for our usecase. Even if I really like how "Offline Shell" does it I think thats too much configuration for most admins. Scan the offline page This function will run if you save the settings and as a WP-Cron (to detect any changes). But we could still run into problems if the site changes (cache refresh, updates) or the scan fails. Scan inside the ServiceWorker |
Yeah, it's a pretty good guess that the assets used on the offline page, in particular the scripts and stylesheets, would also be loaded on the homepage. So if those assets are cached with the network first strategy then it should be OK. What isn't accounted for yet, however, are the assets that are unique to the offline page. We're talking about images and other media that are used on the page. For example, GitHub has their cool imagery that is served on the 500 error page: Scanning the page for assets would only work for assets that are at the top-level. It wouldn't account for, say, background images that are referenced in stylesheet assets. Maybe a good middle ground would be to just look at the offline page post in the DB and locate any images and other assets that are linked to in the content, as well as the featured image that is assigned. These assets could then be easily included in the precache manifest, with the scripts and stylesheet assets being cached thereafter at runtime. There could be additional assets that are linked to from the admin screen that wouldn't get cached here, but I think we'd be covering the 80% scenario, and a site owner should then be able to manually include other assets via an API to account for the remaining 20%. How does that sound?
I agree with you 100%. The WordPress philosophy of “decisions not options” does not align with having this UI for users to manually select the assets to include in the precache manifest. It should work out of the box for the majority of use cases without there needing to be any configuation. |
Sounds good to me. So you are implementing a network first caching for assets anyway? |
Hey All—Chiming in from the Workbox side of things. Dynamically determining which subresources were loaded by a page is something that we're actively working on. It's not so much in the context of precaching, though—Workbox assumes that the preache manifest of URLs is determined ahead of time, during a build process. But we want to determine assets used at runtime in order to "backfill" runtime caches with requests that were made on the initial page load, prior to the service worker taking control. That would then assume that you'd use a runtime caching strategy (cache-first, stale-while-revalidate, etc.) inside the service worker for handling subsequent requests for those subresources. There's more info on our plans for this in GoogleChrome/workbox#368 CC: @philipwalton and @prateekbh who are both looking into implementing this in Workbox. |
@jeffposnick Thanks, that looks interesting. It makes sense that there wouldn't be a straightforward way to discover the subresources required by a given URL without just going ahead and loading document. It's great to know that Workbox is looking to add to the cache the assets loaded on the initial page load. That seems it would potentially reduce the need for precaching a list of URLs obtained by a build process (which again isn't really feasible in this CMS context) other than the URL to the offline page itself. Again, this assumes most of the assets used on the offline page would be common with the page on which the service worker was first installed.
@nico-martin That's yet to be determined, but I think it would make sense as a default. It would need to be configurable so that a theme/plugin could opt-in to
Yeah, my concern is the over-engineering vs the value gained. There is the concern of knowing when to re-scrape the offline page output to determine when new assets need to be cached (e.g. from header images, sidebar widgets, etc). On the other hand, if we just rely on the |
Don't know if it helps - I tried something similar to this a few years ago with appcache, which uses the asset queue and scans the active theme directory: https://wordpress.org/plugins/appcachify/ Scanning the active theme & plugins for non-js/css assets covers some of the cases like assets referenced in CSS. Of course it depends on how an offline page is put together, anything dynamic/editable would be missed. Also the scanning operation can potentially take a huge amount of time if plugins are included so unless it can be run in the background it's a non-starter. Perhaps another option to consider though. |
@roborourke Thank you for that. I think we should consider that for the future. For the time being, however, we can consider (what I assume is) the 80% case where the assets used on the homepage will also be used on the offline page, with the exception of the featured image and media in the offline page's content which we should precache along with the offline page itself. For the remaining 20% I think it would be better for now to defer to theme and plugin authors to explicitly precache the routes they know are needed, as I don't think even with parsing the response we won't be able to get 100% of the URLs that are referenced by the offline page. For one thing, there are the assets dynamically-injected by JS. The other thing is when a theme or plugin changes this will surely mean changes to the assets returned on the offline page, so we'd be needing to continually scan the offline page for assets. But since we couldn't get 100% coverage anyway, I think it is better to just leave that out for now. |
Hi @westonruter - thanks for generating this ticket and making progress! I've added a user story and two basic ACs here. Based on the discussion, it sounds like coming up with a way to manage caches might also need to be added as an additional AC, as well as coming up with a logical way to manage sub-assets. |
I've taken a first pass at precaching based on the needs of serving an offline page in #48. This will need to be further iterated on, but at the moment it is precaching any scripts and styles that are enqueued, as well as the custom background image, custom header image, and custom logo mage. And it is precaching the offline page URL itself and its featured image. It then uses runtime caching to accumulate any image and font assets that are linked to in the theme. |
There's one more thing I want to do, and that is related to specifying the query vars: GoogleChrome/workbox#1613 (comment) |
We've made enough progress here so I'm closing for now, though this comment is a future enhancement to make caching easier. |
As a site admin, I should have a way to precache important assets to give my users a faster perceived experience of using my WordPress site.
AC1: Propose a user interface to select which assets should be pre-cached.AC2: As site admins might need to declare precached asset support in different ways, also propose a theme function and/or wp-cli script to select which assets should be precached.
Note: additional ACs likely to be added.
As noted by @jeffposnick in #5 (comment):
Precaching is indeed something we haven't talked about yet, but I think that WordPress has some good tooling we can use here. For example, external scripts and stylesheets get registered with dependency system where each URL get a stable handle that does not change. Each dependency gets a version as well which is used to cache-bust the URL, but in the case of precaching we could strip the version (
ver
) parameter when generating the response since the service worker could handle the cache busting itself.The text was updated successfully, but these errors were encountered: