@serwist/sw 9.0.0-preview.2 → 9.0.0-preview.20
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/abstractions/Serwist.d.ts +82 -0
- package/dist/abstractions/Serwist.d.ts.map +1 -0
- package/dist/{disableDevLogs.d.ts → abstractions/disableDevLogs.d.ts} +2 -0
- package/dist/abstractions/disableDevLogs.d.ts.map +1 -0
- package/dist/{fallbacks.d.ts → abstractions/fallbacks.d.ts} +9 -20
- package/dist/abstractions/fallbacks.d.ts.map +1 -0
- package/dist/{handlePrecaching.d.ts → abstractions/handlePrecaching.d.ts} +8 -10
- package/dist/abstractions/handlePrecaching.d.ts.map +1 -0
- package/dist/abstractions/installSerwist.d.ts +15 -0
- package/dist/abstractions/installSerwist.d.ts.map +1 -0
- package/dist/abstractions/navigationPreload.d.ts +20 -0
- package/dist/abstractions/navigationPreload.d.ts.map +1 -0
- package/dist/{registerRuntimeCaching.d.ts → abstractions/registerRuntimeCaching.d.ts} +2 -0
- package/dist/abstractions/registerRuntimeCaching.d.ts.map +1 -0
- package/dist/abstractions/types.d.ts +29 -0
- package/dist/abstractions/types.d.ts.map +1 -0
- package/dist/chunks/NavigationRoute.js +54 -0
- package/dist/chunks/NetworkOnly.js +193 -0
- package/dist/chunks/PrecacheFallbackPlugin.js +573 -0
- package/dist/chunks/Strategy.js +410 -0
- package/dist/chunks/precacheAndRoute.js +113 -0
- package/dist/chunks/registerRoute.js +7 -0
- package/dist/chunks/singletonPrecacheController.js +433 -0
- package/dist/chunks/singletonRouter.js +435 -0
- package/dist/index.d.ts +11 -9
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +121 -124
- package/dist/index.plugins.d.ts +41 -0
- package/dist/index.plugins.d.ts.map +1 -0
- package/dist/index.plugins.js +671 -0
- package/dist/index.precaching.d.ts +25 -0
- package/dist/index.precaching.d.ts.map +1 -0
- package/dist/index.precaching.js +24 -0
- package/dist/index.routing.d.ts +15 -0
- package/dist/index.routing.d.ts.map +1 -0
- package/dist/index.routing.js +19 -0
- package/dist/index.strategies.d.ts +22 -0
- package/dist/index.strategies.d.ts.map +1 -0
- package/dist/index.strategies.js +146 -0
- package/dist/plugins/backgroundSync/BackgroundSyncPlugin.d.ts +23 -0
- package/dist/plugins/backgroundSync/BackgroundSyncPlugin.d.ts.map +1 -0
- package/dist/plugins/backgroundSync/Queue.d.ts +166 -0
- package/dist/plugins/backgroundSync/Queue.d.ts.map +1 -0
- package/dist/plugins/backgroundSync/QueueDb.d.ts +90 -0
- package/dist/plugins/backgroundSync/QueueDb.d.ts.map +1 -0
- package/dist/plugins/backgroundSync/QueueStore.d.ts +75 -0
- package/dist/plugins/backgroundSync/QueueStore.d.ts.map +1 -0
- package/dist/plugins/backgroundSync/StorableRequest.d.ts +51 -0
- package/dist/plugins/backgroundSync/StorableRequest.d.ts.map +1 -0
- package/dist/plugins/broadcastUpdate/BroadcastCacheUpdate.d.ts +45 -0
- package/dist/plugins/broadcastUpdate/BroadcastCacheUpdate.d.ts.map +1 -0
- package/dist/plugins/broadcastUpdate/BroadcastUpdatePlugin.d.ts +27 -0
- package/dist/plugins/broadcastUpdate/BroadcastUpdatePlugin.d.ts.map +1 -0
- package/dist/plugins/broadcastUpdate/constants.d.ts +5 -0
- package/dist/plugins/broadcastUpdate/constants.d.ts.map +1 -0
- package/dist/plugins/broadcastUpdate/responsesAreSame.d.ts +11 -0
- package/dist/plugins/broadcastUpdate/responsesAreSame.d.ts.map +1 -0
- package/dist/plugins/broadcastUpdate/types.d.ts +34 -0
- package/dist/plugins/broadcastUpdate/types.d.ts.map +1 -0
- package/dist/plugins/cacheableResponse/CacheableResponse.d.ts +40 -0
- package/dist/plugins/cacheableResponse/CacheableResponse.d.ts.map +1 -0
- package/dist/plugins/cacheableResponse/CacheableResponsePlugin.d.ts +27 -0
- package/dist/plugins/cacheableResponse/CacheableResponsePlugin.d.ts.map +1 -0
- package/dist/plugins/expiration/CacheExpiration.d.ts +66 -0
- package/dist/plugins/expiration/CacheExpiration.d.ts.map +1 -0
- package/dist/plugins/expiration/ExpirationPlugin.d.ts +116 -0
- package/dist/plugins/expiration/ExpirationPlugin.d.ts.map +1 -0
- package/dist/plugins/expiration/models/CacheTimestampsModel.d.ts +73 -0
- package/dist/plugins/expiration/models/CacheTimestampsModel.d.ts.map +1 -0
- package/dist/plugins/googleAnalytics/constants.d.ts +10 -0
- package/dist/plugins/googleAnalytics/constants.d.ts.map +1 -0
- package/dist/plugins/googleAnalytics/initialize.d.ts +34 -0
- package/dist/plugins/googleAnalytics/initialize.d.ts.map +1 -0
- package/dist/plugins/precaching/PrecacheFallbackPlugin.d.ts +54 -0
- package/dist/plugins/precaching/PrecacheFallbackPlugin.d.ts.map +1 -0
- package/dist/plugins/rangeRequests/RangeRequestsPlugin.d.ts +19 -0
- package/dist/plugins/rangeRequests/RangeRequestsPlugin.d.ts.map +1 -0
- package/dist/plugins/rangeRequests/createPartialResponse.d.ts +18 -0
- package/dist/plugins/rangeRequests/createPartialResponse.d.ts.map +1 -0
- package/dist/plugins/rangeRequests/utils/calculateEffectiveBoundaries.d.ts +14 -0
- package/dist/plugins/rangeRequests/utils/calculateEffectiveBoundaries.d.ts.map +1 -0
- package/dist/plugins/rangeRequests/utils/parseRangeHeader.d.ts +12 -0
- package/dist/plugins/rangeRequests/utils/parseRangeHeader.d.ts.map +1 -0
- package/dist/precaching/PrecacheController.d.ts +145 -0
- package/dist/precaching/PrecacheController.d.ts.map +1 -0
- package/dist/precaching/PrecacheRoute.d.ts +20 -0
- package/dist/precaching/PrecacheRoute.d.ts.map +1 -0
- package/dist/precaching/PrecacheStrategy.d.ts +68 -0
- package/dist/precaching/PrecacheStrategy.d.ts.map +1 -0
- package/dist/precaching/addPlugins.d.ts +8 -0
- package/dist/precaching/addPlugins.d.ts.map +1 -0
- package/dist/precaching/addRoute.d.ts +15 -0
- package/dist/precaching/addRoute.d.ts.map +1 -0
- package/dist/precaching/cleanupOutdatedCaches.d.ts +6 -0
- package/dist/precaching/cleanupOutdatedCaches.d.ts.map +1 -0
- package/dist/precaching/createHandlerBoundToURL.d.ts +17 -0
- package/dist/precaching/createHandlerBoundToURL.d.ts.map +1 -0
- package/dist/precaching/getCacheKeyForURL.d.ts +20 -0
- package/dist/precaching/getCacheKeyForURL.d.ts.map +1 -0
- package/dist/precaching/matchPrecache.d.ts +14 -0
- package/dist/precaching/matchPrecache.d.ts.map +1 -0
- package/dist/precaching/precache.d.ts +19 -0
- package/dist/precaching/precache.d.ts.map +1 -0
- package/dist/precaching/precacheAndRoute.d.ts +14 -0
- package/dist/precaching/precacheAndRoute.d.ts.map +1 -0
- package/dist/precaching/singletonPrecacheController.d.ts +38 -0
- package/dist/precaching/singletonPrecacheController.d.ts.map +1 -0
- package/dist/precaching/types.d.ts +37 -0
- package/dist/precaching/types.d.ts.map +1 -0
- package/dist/precaching/utils/PrecacheCacheKeyPlugin.d.ts +17 -0
- package/dist/precaching/utils/PrecacheCacheKeyPlugin.d.ts.map +1 -0
- package/dist/precaching/utils/PrecacheInstallReportPlugin.d.ts +15 -0
- package/dist/precaching/utils/PrecacheInstallReportPlugin.d.ts.map +1 -0
- package/dist/precaching/utils/createCacheKey.d.ts +16 -0
- package/dist/precaching/utils/createCacheKey.d.ts.map +1 -0
- package/dist/precaching/utils/deleteOutdatedCaches.d.ts +18 -0
- package/dist/precaching/utils/deleteOutdatedCaches.d.ts.map +1 -0
- package/dist/precaching/utils/generateURLVariations.d.ts +12 -0
- package/dist/precaching/utils/generateURLVariations.d.ts.map +1 -0
- package/dist/precaching/utils/getCacheKeyForURL.d.ts +14 -0
- package/dist/precaching/utils/getCacheKeyForURL.d.ts.map +1 -0
- package/dist/precaching/utils/printCleanupDetails.d.ts +6 -0
- package/dist/precaching/utils/printCleanupDetails.d.ts.map +1 -0
- package/dist/precaching/utils/printInstallDetails.d.ts +7 -0
- package/dist/precaching/utils/printInstallDetails.d.ts.map +1 -0
- package/dist/precaching/utils/removeIgnoredSearchParams.d.ts +12 -0
- package/dist/precaching/utils/removeIgnoredSearchParams.d.ts.map +1 -0
- package/dist/routing/NavigationRoute.d.ts +57 -0
- package/dist/routing/NavigationRoute.d.ts.map +1 -0
- package/dist/routing/RegExpRoute.d.ts +24 -0
- package/dist/routing/RegExpRoute.d.ts.map +1 -0
- package/dist/routing/Route.d.ts +33 -0
- package/dist/routing/Route.d.ts.map +1 -0
- package/dist/routing/Router.d.ts +150 -0
- package/dist/routing/Router.d.ts.map +1 -0
- package/dist/routing/parseRoute.d.ts +16 -0
- package/dist/routing/parseRoute.d.ts.map +1 -0
- package/dist/routing/registerRoute.d.ts +15 -0
- package/dist/routing/registerRoute.d.ts.map +1 -0
- package/dist/routing/setCatchHandler.d.ts +9 -0
- package/dist/routing/setCatchHandler.d.ts.map +1 -0
- package/dist/routing/setDefaultHandler.d.ts +12 -0
- package/dist/routing/setDefaultHandler.d.ts.map +1 -0
- package/dist/routing/singletonRouter.d.ts +47 -0
- package/dist/routing/singletonRouter.d.ts.map +1 -0
- package/dist/routing/unregisterRoute.d.ts +8 -0
- package/dist/routing/unregisterRoute.d.ts.map +1 -0
- package/dist/routing/utils/constants.d.ts +15 -0
- package/dist/routing/utils/constants.d.ts.map +1 -0
- package/dist/routing/utils/normalizeHandler.d.ts +10 -0
- package/dist/routing/utils/normalizeHandler.d.ts.map +1 -0
- package/dist/strategies/CacheFirst.d.ts +23 -0
- package/dist/strategies/CacheFirst.d.ts.map +1 -0
- package/dist/strategies/CacheOnly.d.ts +20 -0
- package/dist/strategies/CacheOnly.d.ts.map +1 -0
- package/dist/strategies/NetworkFirst.d.ts +61 -0
- package/dist/strategies/NetworkFirst.d.ts.map +1 -0
- package/dist/strategies/NetworkOnly.d.ts +32 -0
- package/dist/strategies/NetworkOnly.d.ts.map +1 -0
- package/dist/strategies/StaleWhileRevalidate.d.ts +35 -0
- package/dist/strategies/StaleWhileRevalidate.d.ts.map +1 -0
- package/dist/strategies/Strategy.d.ts +83 -0
- package/dist/strategies/Strategy.d.ts.map +1 -0
- package/dist/strategies/StrategyHandler.d.ts +189 -0
- package/dist/strategies/StrategyHandler.d.ts.map +1 -0
- package/dist/strategies/plugins/cacheOkAndOpaquePlugin.d.ts +3 -0
- package/dist/strategies/plugins/cacheOkAndOpaquePlugin.d.ts.map +1 -0
- package/dist/strategies/utils/messages.d.ts +5 -0
- package/dist/strategies/utils/messages.d.ts.map +1 -0
- package/package.json +42 -19
- package/src/abstractions/Serwist.ts +177 -0
- package/src/abstractions/disableDevLogs.ts +10 -0
- package/src/abstractions/fallbacks.ts +65 -0
- package/src/abstractions/handlePrecaching.ts +65 -0
- package/src/abstractions/installSerwist.ts +28 -0
- package/src/abstractions/navigationPreload.ts +64 -0
- package/src/abstractions/registerRuntimeCaching.ts +17 -0
- package/src/abstractions/types.ts +29 -0
- package/src/index.plugins.ts +95 -0
- package/src/index.precaching.ts +41 -0
- package/src/index.routing.ts +28 -0
- package/src/index.strategies.ts +26 -0
- package/src/index.ts +31 -9
- package/src/plugins/backgroundSync/BackgroundSyncPlugin.ts +39 -0
- package/src/plugins/backgroundSync/Queue.ts +438 -0
- package/src/plugins/backgroundSync/QueueDb.ts +176 -0
- package/src/plugins/backgroundSync/QueueStore.ts +161 -0
- package/src/plugins/backgroundSync/StorableRequest.ts +142 -0
- package/src/plugins/broadcastUpdate/BroadcastCacheUpdate.ts +159 -0
- package/src/plugins/broadcastUpdate/BroadcastUpdatePlugin.ts +43 -0
- package/src/plugins/broadcastUpdate/constants.ts +12 -0
- package/src/plugins/broadcastUpdate/responsesAreSame.ts +48 -0
- package/src/plugins/broadcastUpdate/types.ts +37 -0
- package/src/plugins/cacheableResponse/CacheableResponse.ts +141 -0
- package/src/plugins/cacheableResponse/CacheableResponsePlugin.ts +46 -0
- package/src/plugins/expiration/CacheExpiration.ts +192 -0
- package/src/plugins/expiration/ExpirationPlugin.ts +297 -0
- package/src/plugins/expiration/models/CacheTimestampsModel.ts +184 -0
- package/src/plugins/googleAnalytics/constants.ts +22 -0
- package/src/plugins/googleAnalytics/initialize.ts +213 -0
- package/src/plugins/precaching/PrecacheFallbackPlugin.ts +86 -0
- package/src/plugins/rangeRequests/RangeRequestsPlugin.ts +39 -0
- package/src/plugins/rangeRequests/createPartialResponse.ts +92 -0
- package/src/plugins/rangeRequests/utils/calculateEffectiveBoundaries.ts +58 -0
- package/src/plugins/rangeRequests/utils/parseRangeHeader.ts +54 -0
- package/src/precaching/PrecacheController.ts +332 -0
- package/src/precaching/PrecacheRoute.ts +50 -0
- package/src/precaching/PrecacheStrategy.ts +238 -0
- package/src/precaching/addPlugins.ts +21 -0
- package/src/precaching/addRoute.ts +30 -0
- package/src/precaching/cleanupOutdatedCaches.ts +33 -0
- package/src/precaching/createHandlerBoundToURL.ts +30 -0
- package/src/precaching/getCacheKeyForURL.ts +33 -0
- package/src/precaching/matchPrecache.ts +25 -0
- package/src/precaching/precache.ts +31 -0
- package/src/precaching/precacheAndRoute.ts +27 -0
- package/src/precaching/singletonPrecacheController.ts +57 -0
- package/src/precaching/types.ts +46 -0
- package/src/precaching/utils/PrecacheCacheKeyPlugin.ts +36 -0
- package/src/precaching/utils/PrecacheInstallReportPlugin.ts +49 -0
- package/src/precaching/utils/createCacheKey.ts +68 -0
- package/src/precaching/utils/deleteOutdatedCaches.ts +40 -0
- package/src/precaching/utils/generateURLVariations.ts +55 -0
- package/src/precaching/utils/getCacheKeyForURL.ts +36 -0
- package/src/precaching/utils/printCleanupDetails.ts +38 -0
- package/src/precaching/utils/printInstallDetails.ts +53 -0
- package/src/precaching/utils/removeIgnoredSearchParams.ts +29 -0
- package/src/routing/NavigationRoute.ts +119 -0
- package/src/routing/RegExpRoute.ts +74 -0
- package/src/routing/Route.ts +68 -0
- package/src/routing/Router.ts +481 -0
- package/src/routing/parseRoute.ts +78 -0
- package/src/routing/registerRoute.ts +27 -0
- package/src/routing/setCatchHandler.ts +21 -0
- package/src/routing/setDefaultHandler.ts +24 -0
- package/src/routing/singletonRouter.ts +76 -0
- package/src/routing/unregisterRoute.ts +11 -0
- package/src/routing/utils/constants.ts +24 -0
- package/src/routing/utils/normalizeHandler.ts +40 -0
- package/src/strategies/CacheFirst.ts +87 -0
- package/src/strategies/CacheOnly.ts +58 -0
- package/src/strategies/NetworkFirst.ts +228 -0
- package/src/strategies/NetworkOnly.ts +96 -0
- package/src/strategies/StaleWhileRevalidate.ts +109 -0
- package/src/strategies/Strategy.ts +202 -0
- package/src/strategies/StrategyHandler.ts +557 -0
- package/src/strategies/plugins/cacheOkAndOpaquePlugin.ts +26 -0
- package/src/strategies/utils/messages.ts +20 -0
- package/dist/disableDevLogs.d.ts.map +0 -1
- package/dist/fallbacks.d.ts.map +0 -1
- package/dist/handlePrecaching.d.ts.map +0 -1
- package/dist/installSerwist.d.ts +0 -21
- package/dist/installSerwist.d.ts.map +0 -1
- package/dist/registerRuntimeCaching.d.ts.map +0 -1
- package/dist/types.d.ts +0 -110
- package/dist/types.d.ts.map +0 -1
- package/src/disableDevLogs.ts +0 -10
- package/src/fallbacks.ts +0 -83
- package/src/handlePrecaching.ts +0 -65
- package/src/installSerwist.ts +0 -106
- package/src/registerRuntimeCaching.ts +0 -84
- package/src/types.ts +0 -113
|
@@ -0,0 +1,82 @@
|
|
|
1
|
+
import { type GoogleAnalyticsInitializeOptions } from "../plugins/googleAnalytics/initialize.js";
|
|
2
|
+
import { PrecacheController } from "../precaching/PrecacheController.js";
|
|
3
|
+
import { Router } from "../routing/Router.js";
|
|
4
|
+
import type { FallbacksOptions } from "./fallbacks.js";
|
|
5
|
+
import { type HandlePrecachingOptions } from "./handlePrecaching.js";
|
|
6
|
+
import type { RuntimeCaching } from "./types.js";
|
|
7
|
+
export interface SerwistOptions {
|
|
8
|
+
/**
|
|
9
|
+
* An optional `PrecacheController` instance. If not provided, the singleton
|
|
10
|
+
* `PrecacheController` will be used.
|
|
11
|
+
*/
|
|
12
|
+
precacheController?: PrecacheController;
|
|
13
|
+
/**
|
|
14
|
+
* An optional `Router` instance. If not provided, the singleton `Router`
|
|
15
|
+
* will be used.
|
|
16
|
+
*/
|
|
17
|
+
router?: Router;
|
|
18
|
+
}
|
|
19
|
+
export interface SerwistInstallOptions extends HandlePrecachingOptions {
|
|
20
|
+
/**
|
|
21
|
+
* Forces the waiting service worker to become the active one.
|
|
22
|
+
*
|
|
23
|
+
* @see https://developer.mozilla.org/en-US/docs/Web/API/ServiceWorkerGlobalScope/skipWaiting
|
|
24
|
+
*/
|
|
25
|
+
skipWaiting?: boolean;
|
|
26
|
+
/**
|
|
27
|
+
* Imports external scripts. They are executed in the order they
|
|
28
|
+
* are passed.
|
|
29
|
+
*
|
|
30
|
+
* @see https://developer.mozilla.org/en-US/docs/Web/API/WorkerGlobalScope/importScripts
|
|
31
|
+
*/
|
|
32
|
+
importScripts?: string[];
|
|
33
|
+
/**
|
|
34
|
+
* Enables navigation preloading if it is supported.
|
|
35
|
+
*
|
|
36
|
+
* @see https://developer.mozilla.org/en-US/docs/Web/API/ServiceWorkerRegistration/navigationPreload
|
|
37
|
+
*/
|
|
38
|
+
navigationPreload?: boolean;
|
|
39
|
+
/**
|
|
40
|
+
* Modifies the prefix of the default cache names used by Serwist packages.
|
|
41
|
+
*/
|
|
42
|
+
cacheId?: string | undefined;
|
|
43
|
+
/**
|
|
44
|
+
* Claims any currently available clients once the service worker
|
|
45
|
+
* becomes active. This is normally used in conjunction with `skipWaiting()`.
|
|
46
|
+
*
|
|
47
|
+
* @default false
|
|
48
|
+
*/
|
|
49
|
+
clientsClaim?: boolean;
|
|
50
|
+
/**
|
|
51
|
+
* A list of caching strategies.
|
|
52
|
+
*
|
|
53
|
+
* @see https://serwist.pages.dev/docs/sw/runtime-caching
|
|
54
|
+
*/
|
|
55
|
+
runtimeCaching?: RuntimeCaching[];
|
|
56
|
+
/**
|
|
57
|
+
* Your configuration for `@serwist/google-analytics`. This plugin is
|
|
58
|
+
* only initialized when this option is not `undefined` or `false`.
|
|
59
|
+
*/
|
|
60
|
+
offlineAnalyticsConfig?: GoogleAnalyticsInitializeOptions | boolean;
|
|
61
|
+
/**
|
|
62
|
+
* Disables Serwist's logging in development mode.
|
|
63
|
+
*
|
|
64
|
+
* @default false
|
|
65
|
+
*/
|
|
66
|
+
disableDevLogs?: boolean;
|
|
67
|
+
/**
|
|
68
|
+
* Precaches routes so that they can be used as a fallback when
|
|
69
|
+
* a Strategy fails to generate a response.
|
|
70
|
+
* Note: This option mutates `runtimeCaching`!
|
|
71
|
+
*
|
|
72
|
+
* @see https://serwist.pages.dev/docs/sw/fallbacks
|
|
73
|
+
*/
|
|
74
|
+
fallbacks?: Omit<FallbacksOptions, "runtimeCaching">;
|
|
75
|
+
}
|
|
76
|
+
export declare class Serwist {
|
|
77
|
+
private _precacheController;
|
|
78
|
+
private _router;
|
|
79
|
+
constructor({ precacheController, router }?: SerwistOptions);
|
|
80
|
+
install({ precacheEntries, precacheOptions, cleanupOutdatedCaches, navigateFallback, navigateFallbackAllowlist, navigateFallbackDenylist, skipWaiting, importScripts, navigationPreload, cacheId, clientsClaim, runtimeCaching, offlineAnalyticsConfig, disableDevLogs, fallbacks, }?: SerwistInstallOptions): void;
|
|
81
|
+
}
|
|
82
|
+
//# sourceMappingURL=Serwist.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"Serwist.d.ts","sourceRoot":"","sources":["../../src/abstractions/Serwist.ts"],"names":[],"mappings":"AACA,OAAO,EAAE,KAAK,gCAAgC,EAAc,MAAM,0CAA0C,CAAC;AAC7G,OAAO,EAAE,kBAAkB,EAAE,MAAM,qCAAqC,CAAC;AAMzE,OAAO,EAAE,MAAM,EAAE,MAAM,sBAAsB,CAAC;AAK9C,OAAO,KAAK,EAAE,gBAAgB,EAAE,MAAM,gBAAgB,CAAC;AACvD,OAAO,EAAE,KAAK,uBAAuB,EAAE,MAAM,uBAAuB,CAAC;AAErE,OAAO,KAAK,EAAE,cAAc,EAAE,MAAM,YAAY,CAAC;AAIjD,MAAM,WAAW,cAAc;IAC7B;;;OAGG;IACH,kBAAkB,CAAC,EAAE,kBAAkB,CAAC;IACxC;;;OAGG;IACH,MAAM,CAAC,EAAE,MAAM,CAAC;CACjB;AAED,MAAM,WAAW,qBAAsB,SAAQ,uBAAuB;IACpE;;;;OAIG;IACH,WAAW,CAAC,EAAE,OAAO,CAAC;IACtB;;;;;OAKG;IACH,aAAa,CAAC,EAAE,MAAM,EAAE,CAAC;IACzB;;;;OAIG;IACH,iBAAiB,CAAC,EAAE,OAAO,CAAC;IAC5B;;OAEG;IACH,OAAO,CAAC,EAAE,MAAM,GAAG,SAAS,CAAC;IAC7B;;;;;OAKG;IACH,YAAY,CAAC,EAAE,OAAO,CAAC;IACvB;;;;OAIG;IACH,cAAc,CAAC,EAAE,cAAc,EAAE,CAAC;IAClC;;;OAGG;IACH,sBAAsB,CAAC,EAAE,gCAAgC,GAAG,OAAO,CAAC;IACpE;;;;OAIG;IACH,cAAc,CAAC,EAAE,OAAO,CAAC;IACzB;;;;;;OAMG;IACH,SAAS,CAAC,EAAE,IAAI,CAAC,gBAAgB,EAAE,gBAAgB,CAAC,CAAC;CACtD;AAED,qBAAa,OAAO;IAClB,OAAO,CAAC,mBAAmB,CAAqB;IAChD,OAAO,CAAC,OAAO,CAAS;gBACZ,EAAE,kBAAkB,EAAE,MAAM,EAAE,GAAE,cAAmB;IAI/D,OAAO,CAAC,EACN,eAAe,EACf,eAAe,EACf,qBAAqB,EACrB,gBAAgB,EAChB,yBAAyB,EACzB,wBAAwB,EACxB,WAAmB,EACnB,aAAa,EACb,iBAAyB,EACzB,OAAO,EACP,YAAoB,EACpB,cAAc,EACd,sBAAsB,EACtB,cAAsB,EACtB,SAAS,GACV,GAAE,qBAA0B;CA8D9B"}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"disableDevLogs.d.ts","sourceRoot":"","sources":["../../src/abstractions/disableDevLogs.ts"],"names":[],"mappings":"AAEA;;;;GAIG;AACH,eAAO,MAAM,cAAc,QAAO,IAEjC,CAAC"}
|
|
@@ -1,28 +1,13 @@
|
|
|
1
1
|
import type { HandlerDidErrorCallbackParam } from "@serwist/core";
|
|
2
|
-
import type {
|
|
2
|
+
import type { PrecacheFallbackEntry } from "../plugins/precaching/PrecacheFallbackPlugin.js";
|
|
3
|
+
import type { PrecacheRouteOptions } from "../precaching/types.js";
|
|
3
4
|
import type { RuntimeCaching } from "./types.js";
|
|
4
5
|
export type FallbackMatcher = (_: HandlerDidErrorCallbackParam) => boolean;
|
|
5
|
-
export interface FallbackEntry {
|
|
6
|
-
/**
|
|
7
|
-
* The matcher, which checks whether the fallback entry can be used
|
|
8
|
-
* for a Request.
|
|
9
|
-
*/
|
|
10
|
-
matcher: FallbackMatcher;
|
|
11
|
-
/**
|
|
12
|
-
* The fallback URL.
|
|
13
|
-
*/
|
|
14
|
-
url: URL | string;
|
|
6
|
+
export interface FallbackEntry extends PrecacheFallbackEntry {
|
|
15
7
|
/**
|
|
16
8
|
* The revision used for precaching.
|
|
17
9
|
*/
|
|
18
10
|
revision: string;
|
|
19
|
-
/**
|
|
20
|
-
* How the Response in the cache should be matched.
|
|
21
|
-
*
|
|
22
|
-
* @default
|
|
23
|
-
* { ignoreSearch: true }
|
|
24
|
-
*/
|
|
25
|
-
cacheMatchOptions?: MultiCacheQueryOptions;
|
|
26
11
|
}
|
|
27
12
|
export interface FallbacksOptions {
|
|
28
13
|
/**
|
|
@@ -41,10 +26,14 @@ export interface FallbacksOptions {
|
|
|
41
26
|
}
|
|
42
27
|
/**
|
|
43
28
|
* Precaches routes so that they can be used as a fallback when
|
|
44
|
-
* a
|
|
29
|
+
* a Strategy fails to generate a response.
|
|
30
|
+
*
|
|
31
|
+
* Note: This function mutates `runtimeCaching`!
|
|
32
|
+
*
|
|
33
|
+
* @see https://serwist.pages.dev/docs/sw/fallbacks
|
|
45
34
|
* @param options
|
|
46
35
|
* @returns The modified `RuntimeCaching` array. Using this value
|
|
47
|
-
* is not needed, as it is simply the
|
|
36
|
+
* is not needed, as it is simply the array in `options`.
|
|
48
37
|
*/
|
|
49
38
|
export declare const fallbacks: ({ runtimeCaching, entries, precacheOptions }: FallbacksOptions) => RuntimeCaching[];
|
|
50
39
|
//# sourceMappingURL=fallbacks.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"fallbacks.d.ts","sourceRoot":"","sources":["../../src/abstractions/fallbacks.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,4BAA4B,EAAE,MAAM,eAAe,CAAC;AAElE,OAAO,KAAK,EAAE,qBAAqB,EAAE,MAAM,iDAAiD,CAAC;AAE7F,OAAO,KAAK,EAAE,oBAAoB,EAAE,MAAM,wBAAwB,CAAC;AAEnE,OAAO,KAAK,EAAE,cAAc,EAAE,MAAM,YAAY,CAAC;AAEjD,MAAM,MAAM,eAAe,GAAG,CAAC,CAAC,EAAE,4BAA4B,KAAK,OAAO,CAAC;AAE3E,MAAM,WAAW,aAAc,SAAQ,qBAAqB;IAC1D;;OAEG;IACH,QAAQ,EAAE,MAAM,CAAC;CAClB;AAED,MAAM,WAAW,gBAAgB;IAC/B;;OAEG;IACH,cAAc,EAAE,cAAc,EAAE,CAAC;IACjC;;OAEG;IACH,OAAO,EAAE,aAAa,EAAE,CAAC;IACzB;;;OAGG;IACH,eAAe,CAAC,EAAE,oBAAoB,CAAC;CACxC;AAED;;;;;;;;;;GAUG;AACH,eAAO,MAAM,SAAS,iDAAkD,gBAAgB,KAAG,cAAc,EAoBxG,CAAC"}
|
|
@@ -1,5 +1,5 @@
|
|
|
1
|
-
import type { PrecacheEntry, PrecacheRouteOptions } from "
|
|
2
|
-
export
|
|
1
|
+
import type { PrecacheEntry, PrecacheRouteOptions } from "../precaching/types.js";
|
|
2
|
+
export interface HandlePrecachingOptions {
|
|
3
3
|
/**
|
|
4
4
|
* A list of fallback entries.
|
|
5
5
|
*/
|
|
@@ -14,13 +14,11 @@ export type HandlePrecachingOptions = {
|
|
|
14
14
|
* @default false
|
|
15
15
|
*/
|
|
16
16
|
cleanupOutdatedCaches?: boolean;
|
|
17
|
-
} & ({
|
|
18
17
|
/**
|
|
19
|
-
* An URL that should point to a HTML
|
|
20
|
-
*
|
|
21
|
-
* For more complex cases, consider `@serwist/sw.fallbacks`instead.
|
|
18
|
+
* An URL that should point to a HTML file with which navigation requests for URLs that aren't
|
|
19
|
+
* precached will be fulfilled.
|
|
22
20
|
*/
|
|
23
|
-
navigateFallback
|
|
21
|
+
navigateFallback?: string;
|
|
24
22
|
/**
|
|
25
23
|
* URLs that should be allowed to use the `navigateFallback` handler.
|
|
26
24
|
*/
|
|
@@ -30,11 +28,11 @@ export type HandlePrecachingOptions = {
|
|
|
30
28
|
* over `navigateFallbackAllowlist`.
|
|
31
29
|
*/
|
|
32
30
|
navigateFallbackDenylist?: RegExp[];
|
|
33
|
-
}
|
|
34
|
-
navigateFallback?: never;
|
|
35
|
-
});
|
|
31
|
+
}
|
|
36
32
|
/**
|
|
37
33
|
* Handles a list of precache entries and cleans up outdated caches.
|
|
34
|
+
*
|
|
35
|
+
* @see https://serwist.pages.dev/docs/sw/handle-precaching
|
|
38
36
|
* @param options
|
|
39
37
|
*/
|
|
40
38
|
export declare const handlePrecaching: ({ precacheEntries, precacheOptions, cleanupOutdatedCaches, ...options }: HandlePrecachingOptions) => void;
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"handlePrecaching.d.ts","sourceRoot":"","sources":["../../src/abstractions/handlePrecaching.ts"],"names":[],"mappings":"AAGA,OAAO,KAAK,EAAE,aAAa,EAAE,oBAAoB,EAAE,MAAM,wBAAwB,CAAC;AAIlF,MAAM,WAAW,uBAAuB;IACtC;;OAEG;IACH,eAAe,CAAC,EAAE,CAAC,aAAa,GAAG,MAAM,CAAC,EAAE,CAAC;IAC7C;;OAEG;IACH,eAAe,CAAC,EAAE,oBAAoB,CAAC;IACvC;;;;OAIG;IACH,qBAAqB,CAAC,EAAE,OAAO,CAAC;IAChC;;;OAGG;IACH,gBAAgB,CAAC,EAAE,MAAM,CAAC;IAC1B;;OAEG;IACH,yBAAyB,CAAC,EAAE,MAAM,EAAE,CAAC;IACrC;;;OAGG;IACH,wBAAwB,CAAC,EAAE,MAAM,EAAE,CAAC;CACrC;AAED;;;;;GAKG;AACH,eAAO,MAAM,gBAAgB,4EAAqF,uBAAuB,KAAG,IAoB3I,CAAC"}
|
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
import { type SerwistInstallOptions } from "./Serwist.js";
|
|
2
|
+
/**
|
|
3
|
+
* Options for `installSerwist`.
|
|
4
|
+
*
|
|
5
|
+
* @deprecated
|
|
6
|
+
*/
|
|
7
|
+
export type InstallSerwistOptions = SerwistInstallOptions;
|
|
8
|
+
/**
|
|
9
|
+
* Abstracts away the core APIs of Serwist.
|
|
10
|
+
*
|
|
11
|
+
* @param options - `installSerwist` options.
|
|
12
|
+
* @deprecated
|
|
13
|
+
*/
|
|
14
|
+
export declare const installSerwist: (options: InstallSerwistOptions) => void;
|
|
15
|
+
//# sourceMappingURL=installSerwist.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"installSerwist.d.ts","sourceRoot":"","sources":["../../src/abstractions/installSerwist.ts"],"names":[],"mappings":"AAGA,OAAO,EAAW,KAAK,qBAAqB,EAAE,MAAM,cAAc,CAAC;AAEnE;;;;GAIG;AACH,MAAM,MAAM,qBAAqB,GAAG,qBAAqB,CAAC;AAE1D;;;;;GAKG;AACH,eAAO,MAAM,cAAc,YAAa,qBAAqB,KAAG,IAS/D,CAAC"}
|
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Checks whether the current browser supports
|
|
3
|
+
* navigation preloading.
|
|
4
|
+
*
|
|
5
|
+
* @returns
|
|
6
|
+
*/
|
|
7
|
+
export declare const isNavigationPreloadSupported: () => boolean;
|
|
8
|
+
/**
|
|
9
|
+
* If the browser supports navigation preloading, then this will enable it.
|
|
10
|
+
*
|
|
11
|
+
* @param headerValue Optional. Allows developers to override the value of
|
|
12
|
+
* the `Service-Worker-Navigation-Preload` header which will be sent to the
|
|
13
|
+
* server when making the navigation request.
|
|
14
|
+
*/
|
|
15
|
+
export declare const enableNavigationPreload: (headerValue?: string) => void;
|
|
16
|
+
/**
|
|
17
|
+
* If the browser supports navigation preloading, then this will disable it.
|
|
18
|
+
*/
|
|
19
|
+
export declare const disableNavigationPreload: () => void;
|
|
20
|
+
//# sourceMappingURL=navigationPreload.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"navigationPreload.d.ts","sourceRoot":"","sources":["../../src/abstractions/navigationPreload.ts"],"names":[],"mappings":"AAIA;;;;;GAKG;AACH,eAAO,MAAM,4BAA4B,QAAO,OAE/C,CAAC;AAEF;;;;;;GAMG;AACH,eAAO,MAAM,uBAAuB,iBAAkB,MAAM,KAAG,IAqB9D,CAAC;AAEF;;GAEG;AACH,eAAO,MAAM,wBAAwB,QAAO,IAgB3C,CAAC"}
|
|
@@ -2,6 +2,8 @@ import type { RuntimeCaching } from "./types.js";
|
|
|
2
2
|
/**
|
|
3
3
|
* Registers caching strategies to a singleton Router instance. It is a simple
|
|
4
4
|
* syntatic sugar for `@serwist/routing.registerRoute`.
|
|
5
|
+
*
|
|
6
|
+
* @see https://serwist.pages.dev/docs/sw/register-runtime-caching
|
|
5
7
|
* @param runtimeCachingList
|
|
6
8
|
* @returns
|
|
7
9
|
*/
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"registerRuntimeCaching.d.ts","sourceRoot":"","sources":["../../src/abstractions/registerRuntimeCaching.ts"],"names":[],"mappings":"AAEA,OAAO,KAAK,EAAE,cAAc,EAAE,MAAM,YAAY,CAAC;AAEjD;;;;;;;GAOG;AACH,eAAO,MAAM,sBAAsB,0BAA2B,cAAc,EAAE,KAAG,IAIhF,CAAC"}
|
|
@@ -0,0 +1,29 @@
|
|
|
1
|
+
import type { RouteHandler, RouteMatchCallback } from "@serwist/core";
|
|
2
|
+
import type { HTTPMethod } from "../routing/utils/constants.js";
|
|
3
|
+
export interface RuntimeCaching {
|
|
4
|
+
/**
|
|
5
|
+
* The HTTP method to match against. The default value of `'GET'` is normally
|
|
6
|
+
* sufficient, unless you explicitly need to match `'POST'`, `'PUT'`, or
|
|
7
|
+
* another type of request.
|
|
8
|
+
* @default "GET"
|
|
9
|
+
*/
|
|
10
|
+
method?: HTTPMethod;
|
|
11
|
+
/**
|
|
12
|
+
* This match criteria determines whether the configured handler will
|
|
13
|
+
* generate a response for any requests that don't match one of the precached
|
|
14
|
+
* URLs. If multiple `RuntimeCaching` routes are defined, then the first one
|
|
15
|
+
* whose `matcher` matches will be the one that responds.
|
|
16
|
+
*
|
|
17
|
+
* This value directly maps to the first parameter passed to
|
|
18
|
+
* `@serwist/routing.registerRoute`. It's recommended to use a
|
|
19
|
+
* `@serwist/core.RouteMatchCallback` function for greatest flexibility.
|
|
20
|
+
*/
|
|
21
|
+
matcher: RegExp | string | RouteMatchCallback;
|
|
22
|
+
/**
|
|
23
|
+
* This determines how the runtime route will generate a response. It
|
|
24
|
+
* can be a `@serwist/core.RouteHandler` callback function with custom
|
|
25
|
+
* response logic.
|
|
26
|
+
*/
|
|
27
|
+
handler: RouteHandler;
|
|
28
|
+
}
|
|
29
|
+
//# sourceMappingURL=types.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"types.d.ts","sourceRoot":"","sources":["../../src/abstractions/types.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,YAAY,EAAE,kBAAkB,EAAE,MAAM,eAAe,CAAC;AACtE,OAAO,KAAK,EAAE,UAAU,EAAE,MAAM,+BAA+B,CAAC;AAEhE,MAAM,WAAW,cAAc;IAC7B;;;;;OAKG;IACH,MAAM,CAAC,EAAE,UAAU,CAAC;IACpB;;;;;;;;;OASG;IACH,OAAO,EAAE,MAAM,GAAG,MAAM,GAAG,kBAAkB,CAAC;IAC9C;;;;OAIG;IACH,OAAO,EAAE,YAAY,CAAC;CACvB"}
|
|
@@ -0,0 +1,54 @@
|
|
|
1
|
+
import { assert, logger } from '@serwist/core/internal';
|
|
2
|
+
import { R as Route } from './singletonRouter.js';
|
|
3
|
+
|
|
4
|
+
class NavigationRoute extends Route {
|
|
5
|
+
_allowlist;
|
|
6
|
+
_denylist;
|
|
7
|
+
constructor(handler, { allowlist = [
|
|
8
|
+
/./
|
|
9
|
+
], denylist = [] } = {}){
|
|
10
|
+
if (process.env.NODE_ENV !== "production") {
|
|
11
|
+
assert.isArrayOfClass(allowlist, RegExp, {
|
|
12
|
+
moduleName: "@serwist/routing",
|
|
13
|
+
className: "NavigationRoute",
|
|
14
|
+
funcName: "constructor",
|
|
15
|
+
paramName: "options.allowlist"
|
|
16
|
+
});
|
|
17
|
+
assert.isArrayOfClass(denylist, RegExp, {
|
|
18
|
+
moduleName: "@serwist/routing",
|
|
19
|
+
className: "NavigationRoute",
|
|
20
|
+
funcName: "constructor",
|
|
21
|
+
paramName: "options.denylist"
|
|
22
|
+
});
|
|
23
|
+
}
|
|
24
|
+
super((options)=>this._match(options), handler);
|
|
25
|
+
this._allowlist = allowlist;
|
|
26
|
+
this._denylist = denylist;
|
|
27
|
+
}
|
|
28
|
+
_match({ url, request }) {
|
|
29
|
+
if (request && request.mode !== "navigate") {
|
|
30
|
+
return false;
|
|
31
|
+
}
|
|
32
|
+
const pathnameAndSearch = url.pathname + url.search;
|
|
33
|
+
for (const regExp of this._denylist){
|
|
34
|
+
if (regExp.test(pathnameAndSearch)) {
|
|
35
|
+
if (process.env.NODE_ENV !== "production") {
|
|
36
|
+
logger.log(`The navigation route ${pathnameAndSearch} is not being used, since the URL matches this denylist pattern: ${regExp.toString()}`);
|
|
37
|
+
}
|
|
38
|
+
return false;
|
|
39
|
+
}
|
|
40
|
+
}
|
|
41
|
+
if (this._allowlist.some((regExp)=>regExp.test(pathnameAndSearch))) {
|
|
42
|
+
if (process.env.NODE_ENV !== "production") {
|
|
43
|
+
logger.debug(`The navigation route ${pathnameAndSearch} is being used.`);
|
|
44
|
+
}
|
|
45
|
+
return true;
|
|
46
|
+
}
|
|
47
|
+
if (process.env.NODE_ENV !== "production") {
|
|
48
|
+
logger.log(`The navigation route ${pathnameAndSearch} is not being used, since the URL being navigated to doesn't match the allowlist.`);
|
|
49
|
+
}
|
|
50
|
+
return false;
|
|
51
|
+
}
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
export { NavigationRoute as N };
|
|
@@ -0,0 +1,193 @@
|
|
|
1
|
+
import { getFriendlyURL, logger, assert, SerwistError, timeout } from '@serwist/core/internal';
|
|
2
|
+
import { S as Strategy } from './Strategy.js';
|
|
3
|
+
|
|
4
|
+
const cacheOkAndOpaquePlugin = {
|
|
5
|
+
cacheWillUpdate: async ({ response })=>{
|
|
6
|
+
if (response.status === 200 || response.status === 0) {
|
|
7
|
+
return response;
|
|
8
|
+
}
|
|
9
|
+
return null;
|
|
10
|
+
}
|
|
11
|
+
};
|
|
12
|
+
|
|
13
|
+
const messages = {
|
|
14
|
+
strategyStart: (strategyName, request)=>`Using ${strategyName} to respond to '${getFriendlyURL(request.url)}'`,
|
|
15
|
+
printFinalResponse: (response)=>{
|
|
16
|
+
if (response) {
|
|
17
|
+
logger.groupCollapsed("View the final response here.");
|
|
18
|
+
logger.log(response || "[No response returned]");
|
|
19
|
+
logger.groupEnd();
|
|
20
|
+
}
|
|
21
|
+
}
|
|
22
|
+
};
|
|
23
|
+
|
|
24
|
+
class NetworkFirst extends Strategy {
|
|
25
|
+
_networkTimeoutSeconds;
|
|
26
|
+
constructor(options = {}){
|
|
27
|
+
super(options);
|
|
28
|
+
if (!this.plugins.some((p)=>"cacheWillUpdate" in p)) {
|
|
29
|
+
this.plugins.unshift(cacheOkAndOpaquePlugin);
|
|
30
|
+
}
|
|
31
|
+
this._networkTimeoutSeconds = options.networkTimeoutSeconds || 0;
|
|
32
|
+
if (process.env.NODE_ENV !== "production") {
|
|
33
|
+
if (this._networkTimeoutSeconds) {
|
|
34
|
+
assert.isType(this._networkTimeoutSeconds, "number", {
|
|
35
|
+
moduleName: "@serwist/strategies",
|
|
36
|
+
className: this.constructor.name,
|
|
37
|
+
funcName: "constructor",
|
|
38
|
+
paramName: "networkTimeoutSeconds"
|
|
39
|
+
});
|
|
40
|
+
}
|
|
41
|
+
}
|
|
42
|
+
}
|
|
43
|
+
async _handle(request, handler) {
|
|
44
|
+
const logs = [];
|
|
45
|
+
if (process.env.NODE_ENV !== "production") {
|
|
46
|
+
assert.isInstance(request, Request, {
|
|
47
|
+
moduleName: "@serwist/strategies",
|
|
48
|
+
className: this.constructor.name,
|
|
49
|
+
funcName: "handle",
|
|
50
|
+
paramName: "makeRequest"
|
|
51
|
+
});
|
|
52
|
+
}
|
|
53
|
+
const promises = [];
|
|
54
|
+
let timeoutId;
|
|
55
|
+
if (this._networkTimeoutSeconds) {
|
|
56
|
+
const { id, promise } = this._getTimeoutPromise({
|
|
57
|
+
request,
|
|
58
|
+
logs,
|
|
59
|
+
handler
|
|
60
|
+
});
|
|
61
|
+
timeoutId = id;
|
|
62
|
+
promises.push(promise);
|
|
63
|
+
}
|
|
64
|
+
const networkPromise = this._getNetworkPromise({
|
|
65
|
+
timeoutId,
|
|
66
|
+
request,
|
|
67
|
+
logs,
|
|
68
|
+
handler
|
|
69
|
+
});
|
|
70
|
+
promises.push(networkPromise);
|
|
71
|
+
const response = await handler.waitUntil((async ()=>{
|
|
72
|
+
return await handler.waitUntil(Promise.race(promises)) || await networkPromise;
|
|
73
|
+
})());
|
|
74
|
+
if (process.env.NODE_ENV !== "production") {
|
|
75
|
+
logger.groupCollapsed(messages.strategyStart(this.constructor.name, request));
|
|
76
|
+
for (const log of logs){
|
|
77
|
+
logger.log(log);
|
|
78
|
+
}
|
|
79
|
+
messages.printFinalResponse(response);
|
|
80
|
+
logger.groupEnd();
|
|
81
|
+
}
|
|
82
|
+
if (!response) {
|
|
83
|
+
throw new SerwistError("no-response", {
|
|
84
|
+
url: request.url
|
|
85
|
+
});
|
|
86
|
+
}
|
|
87
|
+
return response;
|
|
88
|
+
}
|
|
89
|
+
_getTimeoutPromise({ request, logs, handler }) {
|
|
90
|
+
let timeoutId;
|
|
91
|
+
const timeoutPromise = new Promise((resolve)=>{
|
|
92
|
+
const onNetworkTimeout = async ()=>{
|
|
93
|
+
if (process.env.NODE_ENV !== "production") {
|
|
94
|
+
logs.push(`Timing out the network response at ${this._networkTimeoutSeconds} seconds.`);
|
|
95
|
+
}
|
|
96
|
+
resolve(await handler.cacheMatch(request));
|
|
97
|
+
};
|
|
98
|
+
timeoutId = setTimeout(onNetworkTimeout, this._networkTimeoutSeconds * 1000);
|
|
99
|
+
});
|
|
100
|
+
return {
|
|
101
|
+
promise: timeoutPromise,
|
|
102
|
+
id: timeoutId
|
|
103
|
+
};
|
|
104
|
+
}
|
|
105
|
+
async _getNetworkPromise({ timeoutId, request, logs, handler }) {
|
|
106
|
+
let error = undefined;
|
|
107
|
+
let response = undefined;
|
|
108
|
+
try {
|
|
109
|
+
response = await handler.fetchAndCachePut(request);
|
|
110
|
+
} catch (fetchError) {
|
|
111
|
+
if (fetchError instanceof Error) {
|
|
112
|
+
error = fetchError;
|
|
113
|
+
}
|
|
114
|
+
}
|
|
115
|
+
if (timeoutId) {
|
|
116
|
+
clearTimeout(timeoutId);
|
|
117
|
+
}
|
|
118
|
+
if (process.env.NODE_ENV !== "production") {
|
|
119
|
+
if (response) {
|
|
120
|
+
logs.push("Got response from network.");
|
|
121
|
+
} else {
|
|
122
|
+
logs.push("Unable to get a response from the network. Will respond " + "with a cached response.");
|
|
123
|
+
}
|
|
124
|
+
}
|
|
125
|
+
if (error || !response) {
|
|
126
|
+
response = await handler.cacheMatch(request);
|
|
127
|
+
if (process.env.NODE_ENV !== "production") {
|
|
128
|
+
if (response) {
|
|
129
|
+
logs.push(`Found a cached response in the '${this.cacheName}' cache.`);
|
|
130
|
+
} else {
|
|
131
|
+
logs.push(`No response found in the '${this.cacheName}' cache.`);
|
|
132
|
+
}
|
|
133
|
+
}
|
|
134
|
+
}
|
|
135
|
+
return response;
|
|
136
|
+
}
|
|
137
|
+
}
|
|
138
|
+
|
|
139
|
+
class NetworkOnly extends Strategy {
|
|
140
|
+
_networkTimeoutSeconds;
|
|
141
|
+
constructor(options = {}){
|
|
142
|
+
super(options);
|
|
143
|
+
this._networkTimeoutSeconds = options.networkTimeoutSeconds || 0;
|
|
144
|
+
}
|
|
145
|
+
async _handle(request, handler) {
|
|
146
|
+
if (process.env.NODE_ENV !== "production") {
|
|
147
|
+
assert.isInstance(request, Request, {
|
|
148
|
+
moduleName: "@serwist/strategies",
|
|
149
|
+
className: this.constructor.name,
|
|
150
|
+
funcName: "_handle",
|
|
151
|
+
paramName: "request"
|
|
152
|
+
});
|
|
153
|
+
}
|
|
154
|
+
let error = undefined;
|
|
155
|
+
let response;
|
|
156
|
+
try {
|
|
157
|
+
const promises = [
|
|
158
|
+
handler.fetch(request)
|
|
159
|
+
];
|
|
160
|
+
if (this._networkTimeoutSeconds) {
|
|
161
|
+
const timeoutPromise = timeout(this._networkTimeoutSeconds * 1000);
|
|
162
|
+
promises.push(timeoutPromise);
|
|
163
|
+
}
|
|
164
|
+
response = await Promise.race(promises);
|
|
165
|
+
if (!response) {
|
|
166
|
+
throw new Error(`Timed out the network response after ${this._networkTimeoutSeconds} seconds.`);
|
|
167
|
+
}
|
|
168
|
+
} catch (err) {
|
|
169
|
+
if (err instanceof Error) {
|
|
170
|
+
error = err;
|
|
171
|
+
}
|
|
172
|
+
}
|
|
173
|
+
if (process.env.NODE_ENV !== "production") {
|
|
174
|
+
logger.groupCollapsed(messages.strategyStart(this.constructor.name, request));
|
|
175
|
+
if (response) {
|
|
176
|
+
logger.log("Got response from network.");
|
|
177
|
+
} else {
|
|
178
|
+
logger.log("Unable to get a response from the network.");
|
|
179
|
+
}
|
|
180
|
+
messages.printFinalResponse(response);
|
|
181
|
+
logger.groupEnd();
|
|
182
|
+
}
|
|
183
|
+
if (!response) {
|
|
184
|
+
throw new SerwistError("no-response", {
|
|
185
|
+
url: request.url,
|
|
186
|
+
error
|
|
187
|
+
});
|
|
188
|
+
}
|
|
189
|
+
return response;
|
|
190
|
+
}
|
|
191
|
+
}
|
|
192
|
+
|
|
193
|
+
export { NetworkOnly as N, NetworkFirst as a, cacheOkAndOpaquePlugin as c, messages as m };
|