Skip to content

Commit d089dda

Browse files
authored
fix(pwa): add config option to omit files from precache [LIBS-482] (#793)
* fix(pwa): add config option to omit files from precache * chore: add some precache test materials to pwa-app
1 parent cc20ff1 commit d089dda

File tree

9 files changed

+22
-2
lines changed

9 files changed

+22
-2
lines changed

cli/config/d2.pwa.config.js

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,18 @@ module.exports = {
3838
* https://developers.google.com/web/tools/workbox/modules/workbox-precaching#explanation_of_the_precache_list
3939
*/
4040
additionalManifestEntries: [],
41+
/**
42+
* By default, all the contents of the `build` folder are added to
43+
* the precache to give the app the best chances of functioning
44+
* completely while offline. Developers may choose to omit some
45+
* of these files (for example, thousands of font or image files)
46+
* if they cause cache bloat and the app can work fine without
47+
* them precached. See LIBS-482
48+
*
49+
* The globs should be relative to the public dir of the built app.
50+
* Used in injectPrecacheManifest.js
51+
*/
52+
globsToOmitFromPrecache: [],
4153
},
4254
},
4355
}

cli/src/lib/pwa/injectPrecacheManifest.js

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -42,9 +42,14 @@ module.exports = function injectPrecacheManifest(paths, config) {
4242
swDest: paths.shellBuildServiceWorker,
4343
globDirectory: paths.shellBuildOutput,
4444
globPatterns: ['**/*'],
45-
// Skip index.html and `static` directory;
45+
// Skip index.html, (plugin.html,) and `static` directory;
4646
// CRA's workbox-webpack-plugin handles it smartly
47-
globIgnores: ['static/**/*', paths.launchPath],
47+
globIgnores: [
48+
'static/**/*',
49+
paths.launchPath,
50+
paths.pluginLaunchPath,
51+
...config.pwa.caching.globsToOmitFromPrecache,
52+
],
4853
additionalManifestEntries: config.pwa.caching.additionalManifestEntries,
4954
injectionPoint: 'self.__WB_BUILD_MANIFEST',
5055
// Skip revision hashing for files with hash or semver in name:

docs/pwa/pwa.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ You can opt in to PWA features using options in `d2.config.js`. Here are the opt
1818
| `pwa.caching.patternsToOmitFromCacheableSections` | Array of RegExps or Strings | Similar to the above setting, except this is a list of URL patterns to omit from _cacheable (recorded) sections_. Requests with URLs that are filtered out from cacheable sections can still be cached in the app shell cache, unless they are filtered out from the app shell as well using the setting above. When choosing these URL filters, note that it is better to _cache too many things_ than to risk _not caching an important part of the section_ which could break the offline functionality of the section, so choose your filter patterns accordingly. |
1919
| `pwa.caching.patternsToOmit` | Array of RegExps or Strings | Deprecated; superceded by `patternsToOmitFromAppShell`. The new option takes precedence. |
2020
| `pwa.caching.additionalManifestEntries` | Array of Objects with signature `{ revision: String, url: String }` | A list of files that can be added to the precache manifest. Note that the service worker uses Workbox to precache all static assets that end up in the ‘build’ folder after the CRA compilation and build step during the d2-app-scripts build process. The format of this list must match the [required format for Workbox precache manifests](https://developers.google.com/web/tools/workbox/modules/workbox-precaching#explanation_of_the_precache_list), i.e. it must include a revision hash to inform when that file needs to be updated in the precache. |
21+
| `pwa.caching.globsToOmitFromPrecache` | An array of **glob** Strings | A list of globs that will cause matching files to be omitted from precaching. The globs should be **relative to the 'public' directory** of the built app. For example, if you have a folder of fonts in your app in `./public/fonts/` that you want to omit from precaching, you can use the glob `'fonts/**'` in this array. The omitted files can still be cached at runtime later. |
2122

2223
### Offline caching
2324

examples/pwa-app/d2.config.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,8 @@ const config = {
66
caching: {
77
// For the purposes of this demo, to simulate dashboard content:
88
patternsToOmitFromAppShell: ['visualizations'],
9+
// To test precache filtering: (relative to PUBLIC_DIR)
10+
globsToOmitFromPrecache: ['exclude-from-precache/**'],
911
},
1012
},
1113

examples/pwa-app/public/exclude-from-precache/test-file-1.png

Loading

examples/pwa-app/public/exclude-from-precache/test-file-2.png

Loading

examples/pwa-app/public/exclude-from-precache/test-file-3.png

Loading

examples/pwa-app/public/include-in-precache/new-file-1.ico

Whitespace-only changes.

examples/pwa-app/public/include-in-precache/new-file.json

Whitespace-only changes.

0 commit comments

Comments
 (0)