Skip to content

Commit 2bd4d93

Browse files
committed
improve sw
1 parent b0c530a commit 2bd4d93

File tree

4 files changed

+54
-7
lines changed

4 files changed

+54
-7
lines changed

src/PrecacheSsgManifestPlugin.js

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
// we need to precache some assets from ssg too
2+
// they're previously handled by require('./src/utilities/find-files-in-dist')(['.css', '.ico', '.svg'])
3+
4+
const { Compilation, sources } = require('webpack');
5+
const getManifestEntriesFromCompilation = require('workbox-webpack-plugin/build/lib/get-manifest-entries-from-compilation');
6+
7+
module.exports = class PrecacheSsgManifestPlugin {
8+
apply(compiler) {
9+
compiler.hooks.thisCompilation.tap(
10+
'PrecacheSsgManifestPlugin',
11+
(compilation) => {
12+
compilation.hooks.processAssets.tapPromise(
13+
{
14+
name: 'PrecacheSsgManifestPlugin',
15+
stage: Compilation.PROCESS_ASSETS_STAGE_SUMMARIZE,
16+
},
17+
async () => {
18+
const { sortedEntries } = await getManifestEntriesFromCompilation(
19+
compilation,
20+
{
21+
// we don't want to include all html pages
22+
// as that would take too many storages
23+
// svg excluded as it's already included with InjectManifest
24+
include: [/\.(ico|css)/i, /app-shell/i],
25+
}
26+
);
27+
compilation.emitAsset(
28+
'ssg-manifest.json',
29+
new sources.RawSource(JSON.stringify(sortedEntries))
30+
);
31+
}
32+
);
33+
}
34+
);
35+
}
36+
};

src/sw.js

Lines changed: 16 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,17 @@
11
import { precacheAndRoute } from 'workbox-precaching/precacheAndRoute';
22
import { registerRoute } from 'workbox-routing/registerRoute';
3-
import { CacheFirst } from 'workbox-strategies';
3+
import { CacheFirst, NetworkOnly } from 'workbox-strategies';
44
import { ExpirationPlugin } from 'workbox-expiration/ExpirationPlugin';
5+
import { NavigationRoute } from 'workbox-routing/NavigationRoute';
6+
import { createHandlerBoundToURL } from 'workbox-precaching/createHandlerBoundToURL';
7+
import { setDefaultHandler, setCatchHandler } from 'workbox-routing';
8+
import ssgManifest from '../dist/ssg-manifest.json';
59

610
// Precache assets built with webpack
711
precacheAndRoute(self.__WB_MANIFEST);
812

13+
precacheAndRoute(ssgManifest);
14+
915
// Cache Google Fonts
1016
registerRoute(
1117
/https:\/\/fonts\.gstatic\.com/,
@@ -21,6 +27,12 @@ registerRoute(
2127
})
2228
);
2329

24-
// TODO Cache /app-shell/index.html
25-
26-
// TODO Cache /index.html, /concepts/index.html, etc.
30+
setDefaultHandler(new NetworkOnly());
31+
setCatchHandler(({ event }) => {
32+
switch (event.request.destination) {
33+
case 'document':
34+
return caches.match('/app-shell/index.html');
35+
default:
36+
return Response.error();
37+
}
38+
});

webpack.prod.js

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,6 @@ const path = require('path');
77
// Load Common Configuration
88
const common = require('./webpack.common.js');
99

10-
// find [css, ico, svg] versioned (hashed) files emitted by SSG run
11-
const hashedAssetsBySSGRun = require('./src/utilities/find-files-in-dist')(['.css', '.ico', '.svg']);
12-
1310
module.exports = env => merge(common(env), {
1411
mode: 'production',
1512
target: 'web',

webpack.ssg.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ const contentTree = require('./src/_content.json');
1111

1212
// Load Common Configuration
1313
const common = require('./webpack.common.js');
14+
const PrecacheSsgManifestPlugin = require('./src/PrecacheSsgManifestPlugin');
1415

1516
// content tree to path array
1617
const paths = [
@@ -131,5 +132,6 @@ module.exports = env => merge(common(env), {
131132
},
132133
],
133134
}),
135+
new PrecacheSsgManifestPlugin()
134136
]
135137
});

0 commit comments

Comments
 (0)