File tree Expand file tree Collapse file tree 4 files changed +54
-7
lines changed Expand file tree Collapse file tree 4 files changed +54
-7
lines changed Original file line number Diff line number Diff line change
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 : [ / \. ( i c o | c s s ) / i, / a p p - s h e l l / i] ,
25
+ }
26
+ ) ;
27
+ compilation . emitAsset (
28
+ 'ssg-manifest.json' ,
29
+ new sources . RawSource ( JSON . stringify ( sortedEntries ) )
30
+ ) ;
31
+ }
32
+ ) ;
33
+ }
34
+ ) ;
35
+ }
36
+ } ;
Original file line number Diff line number Diff line change 1
1
import { precacheAndRoute } from 'workbox-precaching/precacheAndRoute' ;
2
2
import { registerRoute } from 'workbox-routing/registerRoute' ;
3
- import { CacheFirst } from 'workbox-strategies' ;
3
+ import { CacheFirst , NetworkOnly } from 'workbox-strategies' ;
4
4
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' ;
5
9
6
10
// Precache assets built with webpack
7
11
precacheAndRoute ( self . __WB_MANIFEST ) ;
8
12
13
+ precacheAndRoute ( ssgManifest ) ;
14
+
9
15
// Cache Google Fonts
10
16
registerRoute (
11
17
/ h t t p s : \/ \/ f o n t s \. g s t a t i c \. c o m / ,
@@ -21,6 +27,12 @@ registerRoute(
21
27
} )
22
28
) ;
23
29
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
+ } ) ;
Original file line number Diff line number Diff line change @@ -7,9 +7,6 @@ const path = require('path');
7
7
// Load Common Configuration
8
8
const common = require ( './webpack.common.js' ) ;
9
9
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
-
13
10
module . exports = env => merge ( common ( env ) , {
14
11
mode : 'production' ,
15
12
target : 'web' ,
Original file line number Diff line number Diff line change @@ -11,6 +11,7 @@ const contentTree = require('./src/_content.json');
11
11
12
12
// Load Common Configuration
13
13
const common = require ( './webpack.common.js' ) ;
14
+ const PrecacheSsgManifestPlugin = require ( './src/PrecacheSsgManifestPlugin' ) ;
14
15
15
16
// content tree to path array
16
17
const paths = [
@@ -131,5 +132,6 @@ module.exports = env => merge(common(env), {
131
132
} ,
132
133
] ,
133
134
} ) ,
135
+ new PrecacheSsgManifestPlugin ( )
134
136
]
135
137
} ) ;
You can’t perform that action at this time.
0 commit comments