@eclipse-docks/extension-pwa 0.7.97 → 0.7.99

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.
Files changed (2) hide show
  1. package/package.json +1 -1
  2. package/src/sw.ts +58 -19
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@eclipse-docks/extension-pwa",
3
- "version": "0.7.97",
3
+ "version": "0.7.99",
4
4
  "type": "module",
5
5
  "main": "./dist/index.js",
6
6
  "exports": {
package/src/sw.ts CHANGED
@@ -21,8 +21,8 @@ type SwUpdateProgressMessage = {
21
21
  const PRECACHE_MANIFEST = self.__WB_MANIFEST;
22
22
  const PRECACHE_TOTAL = PRECACHE_MANIFEST.length;
23
23
  let updateProgressStarted = false;
24
- const updateProgressSeen = new Set<string>();
25
- const updateProgressFetched = new Set<string>();
24
+ /** URLs finished during this waiting-SW install (cache hit or network fetch). */
25
+ const precacheInstallDoneUrls = new Set<string>();
26
26
 
27
27
  async function broadcastUpdateProgress(
28
28
  completed: number,
@@ -38,6 +38,38 @@ async function broadcastUpdateProgress(
38
38
  clients.forEach((client) => client.postMessage(message));
39
39
  }
40
40
 
41
+ function pathnameForProgress(url: string): string {
42
+ try {
43
+ return new URL(url).pathname;
44
+ } catch {
45
+ return url;
46
+ }
47
+ }
48
+
49
+ function shouldReportPrecacheInstallProgress(event: ExtendableEvent | undefined): boolean {
50
+ return Boolean(event?.type === 'install' && self.registration.active && PRECACHE_TOTAL > 0);
51
+ }
52
+
53
+ function ensureUpdateProgressStarted(): void {
54
+ if (updateProgressStarted || !self.registration.active || PRECACHE_TOTAL <= 0) {
55
+ return;
56
+ }
57
+ updateProgressStarted = true;
58
+ void broadcastUpdateProgress(0, PRECACHE_TOTAL);
59
+ }
60
+
61
+ function markPrecacheUrlDone(requestUrl: string): void {
62
+ if (precacheInstallDoneUrls.has(requestUrl)) {
63
+ return;
64
+ }
65
+ precacheInstallDoneUrls.add(requestUrl);
66
+ void broadcastUpdateProgress(
67
+ precacheInstallDoneUrls.size,
68
+ PRECACHE_TOTAL,
69
+ pathnameForProgress(requestUrl),
70
+ );
71
+ }
72
+
41
73
  function withCoopCoep(response: Response): Response {
42
74
  if (response.status === 0 || response.type === 'opaque' || response.type === 'opaqueredirect') {
43
75
  return response;
@@ -56,25 +88,32 @@ cleanupOutdatedCaches();
56
88
 
57
89
  addPlugins([
58
90
  {
59
- cachedResponseWillBeUsed: async ({ cachedResponse }) =>
60
- cachedResponse ? withCoopCoep(cachedResponse) : cachedResponse,
61
- requestWillFetch: async ({ request }) => {
62
- if (!updateProgressStarted && self.registration.active && PRECACHE_TOTAL > 0) {
63
- updateProgressStarted = true;
64
- void broadcastUpdateProgress(0, PRECACHE_TOTAL);
91
+ handlerWillStart: async ({ event, request }) => {
92
+ if (!shouldReportPrecacheInstallProgress(event)) {
93
+ return;
94
+ }
95
+ ensureUpdateProgressStarted();
96
+ void broadcastUpdateProgress(
97
+ precacheInstallDoneUrls.size,
98
+ PRECACHE_TOTAL,
99
+ pathnameForProgress(request.url),
100
+ );
101
+ },
102
+ cachedResponseWillBeUsed: async ({ cachedResponse, request, event }) => {
103
+ if (cachedResponse) {
104
+ const response = withCoopCoep(cachedResponse);
105
+ if (shouldReportPrecacheInstallProgress(event)) {
106
+ ensureUpdateProgressStarted();
107
+ markPrecacheUrlDone(request.url);
108
+ }
109
+ return response;
65
110
  }
66
- updateProgressSeen.add(request.url);
67
- return request;
111
+ return cachedResponse;
68
112
  },
69
- fetchDidSucceed: async ({ request, response }) => {
70
- if (
71
- updateProgressStarted &&
72
- updateProgressSeen.has(request.url) &&
73
- !updateProgressFetched.has(request.url)
74
- ) {
75
- updateProgressFetched.add(request.url);
76
- const currentFile = new URL(request.url).pathname;
77
- void broadcastUpdateProgress(updateProgressFetched.size, PRECACHE_TOTAL, currentFile);
113
+ fetchDidSucceed: async ({ request, response, event }) => {
114
+ if (shouldReportPrecacheInstallProgress(event)) {
115
+ ensureUpdateProgressStarted();
116
+ markPrecacheUrlDone(request.url);
78
117
  }
79
118
  return withCoopCoep(response);
80
119
  },