nuxt-skew-protection 1.6.0 → 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
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
|
-
|
|
1245
|
-
|
|
1246
|
-
|
|
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({
|
|
@@ -1283,7 +1287,9 @@ export {}
|
|
|
1283
1287
|
const daysSince = Math.floor((Date.now() - oldestTimestamp) / (1e3 * 60 * 60 * 24));
|
|
1284
1288
|
const timeInfo = daysSince > 0 ? `${daysSince} day${daysSince > 1 ? "s" : ""} ago` : "today";
|
|
1285
1289
|
const storageInfo = options.storage.base ? `${colors.green(options.storage.driver)} ${colors.gray(`(${options.storage.base})`)}` : colors.green(options.storage.driver);
|
|
1286
|
-
if (totalReleases === 1) {
|
|
1290
|
+
if (totalReleases === 1 && options.storage.driver === "fs") {
|
|
1291
|
+
logger.info("Initialized local storage with its first release.");
|
|
1292
|
+
} else if (totalReleases === 1) {
|
|
1287
1293
|
logger.warn(`No previous versions found in storage. This is either the first deployment or storage is misconfigured. https://nuxtseo.com/docs/skew-protection/storage-configuration`);
|
|
1288
1294
|
} else {
|
|
1289
1295
|
logger.log(`Storing ${colors.yellow(assets.length.toString())} assets for ${colors.cyan(buildId.slice(0, 8))} (${totalReleases} releases, oldest from ${timeInfo}) [${storageInfo}]`);
|
|
@@ -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((
|
|
52
|
+
swRegistration.then((registration2) => {
|
|
41
53
|
logger.debug("[SW] Service worker registered successfully");
|
|
42
|
-
const sw =
|
|
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
|
|
58
|
-
const sw =
|
|
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 [];
|