-
Notifications
You must be signed in to change notification settings - Fork 36
/
pages.js
46 lines (40 loc) · 1.31 KB
/
pages.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
import {Route} from 'workbox-routing/Route.mjs';
import {strategy as streamsStrategy} from 'workbox-streams/strategy.mjs';
import {contentStrategy} from './content.js';
import {precacheHandler} from '../precache.js';
const pagesMatcher = ({url}) => {
return url.hostname === location.hostname &&
(url.pathname === '/' ||
url.pathname.match(/^\/(?:about|articles)\/([\w-]+\/)?$/));
};
const contentHandler = ({event, url}) => {
return contentStrategy.handle({
request: new Request(`${url.pathname}index.content.html`),
event,
});
};
const streamHandler = streamsStrategy([
({event}) => precacheHandler({
request: new Request('/shell-start.html'),
event,
}),
contentHandler,
({event}) => precacheHandler({
request: new Request('/shell-end.html'),
event,
}),
]);
const pagesHandler = (opts) => {
// If the request is a navigation request, assume it's going to be consumed
// by a browser and return the full stream. Otherwise assume it's from
// either an SPA load or a CACHE_URLS message, so only the content partial
// needs to be returned.
if (opts.request && opts.request.mode === 'navigate') {
return streamHandler(opts);
} else {
return contentHandler(opts);
}
};
export const createPagesRoute = () => {
return new Route(pagesMatcher, pagesHandler);
};