Skip to content
This repository has been archived by the owner on May 17, 2019. It is now read-only.

Commit

Permalink
switch to cacheInvalidatingPaths
Browse files Browse the repository at this point in the history
  • Loading branch information
angus-c committed Mar 7, 2019
1 parent 8c4365f commit 3c4db28
Show file tree
Hide file tree
Showing 4 changed files with 18 additions and 13 deletions.
23 changes: 14 additions & 9 deletions src/handlers.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ export default function getHandlers(assetInfo: AssetInfo) {
const {
precachePaths,
cacheablePaths,
cacheableDomains,
cacheInvalidatingPatterns,
maxCacheDurationMs = defaultMaxCacheDuration,
} = assetInfo;
return {
Expand Down Expand Up @@ -51,12 +51,15 @@ export default function getHandlers(assetInfo: AssetInfo) {
onFetch: (event: FetchEvent) => {
try {
const expectsHtml = requestExpectsHtml(event.request);
if (
expectsHtml &&
!requestingFromCacheableDomain(event, cacheableDomains)
) {
if (useNetwork(event, expectsHtml, cacheInvalidatingPatterns)) {
// clear cache then bypass service worker, use network
return caches.delete(cacheName);
return caches
.delete(cacheName)
.then(() =>
debug.log(
`[debug] navigation to ${event.request.url}, bypassed cache`
)
);
}
if (requestNotCacheable(expectsHtml, cacheablePaths, event)) {
// bypass service worker, use network
Expand Down Expand Up @@ -158,10 +161,12 @@ function cacheHasExpired(cachedResponse, maxCacheDurationMs) {
: false;
}

function requestingFromCacheableDomain(event, cacheableDomains) {
function useNetwork(event, expectsHTML, cacheInvalidatingPatterns) {
return (
!cacheableDomains ||
cacheableDomains.some(domain => domain.indexOf(event.request.url) > -1)
cacheInvalidatingPatterns &&
cacheInvalidatingPatterns.some(
pattern => event.request.url.indexOf(pattern) > -1
)
);
}

Expand Down
4 changes: 2 additions & 2 deletions src/server.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ export default ((__NODE__ &&
},
middleware: ({
templateFn,
options: {cacheableDomains, maxCacheDurationMs} = {},
options: {cacheInvalidatingPatterns, maxCacheDurationMs} = {},
}) => {
return async (ctx, next) => {
if (__NODE__) {
Expand All @@ -42,7 +42,7 @@ export default ((__NODE__ &&
precachePaths: chunkUrls.filter(url =>
hasSameHostName(url, ctx.url)
),
cacheableDomains,
cacheInvalidatingPatterns,
maxCacheDurationMs,
});
} catch (e) {
Expand Down
2 changes: 1 addition & 1 deletion src/tokens.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ import type {Token} from 'fusion-core';
import type {ConfigTokenType, SWLoggerTokenType} from './types.js';

type Options = {
cacheableDomains?: Array<string>,
cacheInvalidatingPatterns?: Array<string>,
maxCacheDurationMs?: number,
};

Expand Down
2 changes: 1 addition & 1 deletion src/types.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ export type PluginServiceType = {
export type AssetInfo = {
precachePaths: Array<RequestInfo>,
cacheablePaths: Array<RequestInfo>,
cacheableDomains?: Array<string>,
cacheInvalidatingPatterns?: Array<string>,
maxCacheDurationMs?: number,
};

Expand Down

0 comments on commit 3c4db28

Please sign in to comment.