nuxt-skew-protection 1.6.1 → 1.6.2

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/module.json CHANGED
@@ -4,7 +4,7 @@
4
4
  "nuxt": ">=4.0.0"
5
5
  },
6
6
  "configKey": "skewProtection",
7
- "version": "1.6.1",
7
+ "version": "1.6.2",
8
8
  "builder": {
9
9
  "@nuxt/module-builder": "1.0.3",
10
10
  "unbuild": "3.6.1"
package/dist/module.mjs CHANGED
@@ -1241,9 +1241,13 @@ export {}
1241
1241
  handler: resolver.resolve("./runtime/server/routes/__skew/admin/stats.get")
1242
1242
  });
1243
1243
  }
1244
- addPlugin({
1245
- src: resolver.resolve("./runtime/app/plugins/sw-track-user-modules.client"),
1246
- mode: "client"
1244
+ nuxt.hook("modules:done", () => {
1245
+ if (hasNuxtModule("@vite-pwa/nuxt") || hasNuxtModule("@nuxtjs/pwa"))
1246
+ return;
1247
+ addPlugin({
1248
+ src: resolver.resolve("./runtime/app/plugins/sw-track-user-modules.client"),
1249
+ mode: "client"
1250
+ });
1247
1251
  });
1248
1252
  if (options.multiTab !== false || options.reloadStrategy && options.reloadStrategy !== "prompt") {
1249
1253
  addPlugin({
@@ -1,4 +1,4 @@
1
- import { defineNuxtPlugin } from "nuxt/app";
1
+ import { defineNuxtPlugin, useRuntimeConfig } from "nuxt/app";
2
2
  import { buildAssetsURL } from "#internal/nuxt/paths";
3
3
  import { logger } from "../../shared/logger.js";
4
4
  import { useRuntimeConfigSkewProtection } from "../composables/useRuntimeConfigSkewProtection.js";
@@ -21,25 +21,37 @@ function parseSkewFile(value) {
21
21
  }
22
22
  export default defineNuxtPlugin({
23
23
  name: "skew-protection:service-worker",
24
- setup(nuxtApp) {
24
+ async setup(nuxtApp) {
25
25
  if (import.meta.prerender)
26
26
  return;
27
27
  if (!("serviceWorker" in navigator)) {
28
28
  logger.debug("[SW] Service Worker not supported in this browser");
29
29
  return;
30
30
  }
31
+ const appUrl = new URL(useRuntimeConfig().app.baseURL, window.location.origin);
32
+ const serviceWorkerUrl = new URL("_nuxt-skew-sw.js", appUrl);
33
+ const inspection = await navigator.serviceWorker.getRegistration(appUrl.href).then((registration2) => ({ _tag: "Ok", registration: registration2 })).catch((error) => ({ _tag: "Err", error }));
34
+ if (inspection._tag === "Err") {
35
+ logger.warn("[SW] Could not inspect service workers. Chunk tracking is disabled:", inspection.error);
36
+ return;
37
+ }
38
+ const registration = inspection.registration;
39
+ const existingWorker = registration?.active || registration?.installing || registration?.waiting;
40
+ if (existingWorker && new URL(existingWorker.scriptURL).pathname !== serviceWorkerUrl.pathname) {
41
+ logger.debug("[SW] Keeping the existing service worker. Chunk tracking is disabled.");
42
+ return;
43
+ }
31
44
  const { assetRecovery } = useRuntimeConfigSkewProtection();
32
45
  const { clientVersion, onAppOutdated } = useSkewProtection();
33
46
  logger.debug("[SW] Initializing service worker tracking");
34
- const serviceWorkerUrl = new URL("/_nuxt-skew-sw.js", window.location.origin);
35
47
  if (assetRecovery._tag === "cloudflare") {
36
48
  serviceWorkerUrl.searchParams.set("buildAssetsPath", assetRecovery.buildAssetsPath);
37
49
  serviceWorkerUrl.searchParams.set("recoveryPath", assetRecovery.recoveryPath);
38
50
  }
39
51
  const swRegistration = navigator.serviceWorker.register(serviceWorkerUrl.href);
40
- swRegistration.then((registration) => {
52
+ swRegistration.then((registration2) => {
41
53
  logger.debug("[SW] Service worker registered successfully");
42
- const sw = registration.active || registration.installing || registration.waiting;
54
+ const sw = registration2.active || registration2.installing || registration2.waiting;
43
55
  if (!sw) {
44
56
  logger.debug("[SW] No active/installing/waiting service worker found");
45
57
  return;
@@ -54,8 +66,8 @@ export default defineNuxtPlugin({
54
66
  logger.debug("[SW] Service worker registration failed:", error);
55
67
  });
56
68
  async function getLoadedModules() {
57
- const registration = await swRegistration;
58
- const sw = registration.active;
69
+ const registration2 = await swRegistration;
70
+ const sw = registration2.active;
59
71
  if (!sw) {
60
72
  logger.debug("[SW] No active service worker to get modules from");
61
73
  return [];
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "nuxt-skew-protection",
3
3
  "type": "module",
4
- "version": "1.6.1",
4
+ "version": "1.6.2",
5
5
  "description": "Solve Nuxt version skews with persistent assets and instant updates.",
6
6
  "author": {
7
7
  "name": "Harlan Wilton",