nuxt-skew-protection 0.2.0 → 0.2.1
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.cjs +40 -20
- package/dist/module.json +1 -1
- package/dist/module.mjs +40 -20
- package/dist/runtime/app/components/SkewNotification.d.vue.ts +3 -2
- package/dist/runtime/app/components/SkewNotification.vue +16 -11
- package/dist/runtime/app/components/SkewNotification.vue.d.ts +3 -2
- package/dist/runtime/app/composables/useRuntimeConfigSkewProtection.js +4 -1
- package/dist/runtime/app/composables/useSkewProtection.d.ts +1 -1
- package/dist/runtime/app/composables/useSkewProtection.js +12 -5
- package/dist/runtime/app/plugins/check-updates-sse.client.js +3 -4
- package/dist/runtime/app/plugins/check-updates-websocket.client.js +4 -18
- package/dist/runtime/app/plugins/sw-track-user-modules.client.d.ts +1 -9
- package/dist/runtime/app/plugins/sw-track-user-modules.client.js +47 -24
- package/dist/runtime/server/routes/_skew/ws.js +3 -3
- package/dist/runtime/types.d.ts +1 -0
- package/package.json +1 -1
package/dist/module.cjs
CHANGED
|
@@ -207,7 +207,10 @@ async function resolveBuildTimeDriver(storage) {
|
|
|
207
207
|
if (!namespaceId) {
|
|
208
208
|
namespaceId = await detectCloudflareKVNamespace();
|
|
209
209
|
}
|
|
210
|
-
return cloudflareKVWranglerDriver(
|
|
210
|
+
return cloudflareKVWranglerDriver({
|
|
211
|
+
namespaceId,
|
|
212
|
+
...driverOptions
|
|
213
|
+
});
|
|
211
214
|
}
|
|
212
215
|
case "vercel-kv": {
|
|
213
216
|
return vercelKVCLIDriver(driverOptions);
|
|
@@ -551,7 +554,8 @@ function createAssetManager(options) {
|
|
|
551
554
|
storeAssetsInStorage,
|
|
552
555
|
listExistingVersions,
|
|
553
556
|
restoreOldAssetsToPublic,
|
|
554
|
-
augmentBuildMetadata
|
|
557
|
+
augmentBuildMetadata,
|
|
558
|
+
getManifest: () => getVersionManifest(storage)
|
|
555
559
|
};
|
|
556
560
|
}
|
|
557
561
|
|
|
@@ -673,29 +677,45 @@ export {}
|
|
|
673
677
|
nuxt.options.nitro.publicAssets = nuxt.options.nitro.publicAssets || [];
|
|
674
678
|
nuxt.options.nitro.publicAssets.push({
|
|
675
679
|
dir: swPath,
|
|
676
|
-
maxAge: 0
|
|
680
|
+
maxAge: 0
|
|
677
681
|
// Service workers should not be cached
|
|
678
|
-
baseURL: "/_skew"
|
|
679
682
|
});
|
|
680
683
|
if (options.storage && options.bundlePreviousDeploymentChunks) {
|
|
681
|
-
nuxt.hook("nitro:
|
|
684
|
+
nuxt.hook("nitro:init", (nitro) => {
|
|
682
685
|
const buildId = nuxt.options.runtimeConfig.app.buildId ||= nuxt.options.buildId;
|
|
683
|
-
|
|
684
|
-
|
|
685
|
-
|
|
686
|
-
|
|
686
|
+
let assetManager;
|
|
687
|
+
nitro.hooks.hook("compiled", async () => {
|
|
688
|
+
const outputDir = nitro.options.output.dir;
|
|
689
|
+
assetManager = createAssetManager({
|
|
690
|
+
...options,
|
|
691
|
+
driver: await resolveBuildTimeDriver(options.storage)
|
|
692
|
+
});
|
|
693
|
+
const assets = await assetManager.getAssetsFromBuild(outputDir);
|
|
694
|
+
const { isExistingVersion } = await assetManager.updateVersionsManifest(buildId, assets);
|
|
695
|
+
logger.log(`Storing ${assets.length} assets in storage...`);
|
|
696
|
+
await assetManager.storeAssetsInStorage(buildId, outputDir, assets);
|
|
697
|
+
const existingVersions = await assetManager.listExistingVersions();
|
|
698
|
+
const versionCount = existingVersions.filter((v) => v.id !== buildId).length;
|
|
699
|
+
if (versionCount > 0) {
|
|
700
|
+
const manifest = await assetManager.getManifest();
|
|
701
|
+
let totalAssets = 0;
|
|
702
|
+
const versionSizes = [];
|
|
703
|
+
for (const [vId, vData] of Object.entries(manifest.versions)) {
|
|
704
|
+
if (vId !== buildId) {
|
|
705
|
+
totalAssets += vData.assets.length;
|
|
706
|
+
versionSizes.push(`${vId.slice(0, 8)}:${vData.assets.length}`);
|
|
707
|
+
}
|
|
708
|
+
}
|
|
709
|
+
logger.log(`Restoring build files from ${versionCount} release${versionCount > 1 ? "s" : ""} (${totalAssets} assets) [${versionSizes.join(", ")}]...`);
|
|
710
|
+
}
|
|
711
|
+
await assetManager.restoreOldAssetsToPublic(buildId, outputDir, assets, isExistingVersion);
|
|
712
|
+
await assetManager.augmentBuildMetadata(buildId, outputDir);
|
|
713
|
+
});
|
|
714
|
+
nitro.hooks.hook("close", async () => {
|
|
715
|
+
if (assetManager) {
|
|
716
|
+
await assetManager.cleanupExpiredVersions();
|
|
717
|
+
}
|
|
687
718
|
});
|
|
688
|
-
const assets = await assetManager.getAssetsFromBuild(outputDir);
|
|
689
|
-
const { isExistingVersion } = await assetManager.updateVersionsManifest(buildId, assets);
|
|
690
|
-
await assetManager.storeAssetsInStorage(buildId, outputDir, assets);
|
|
691
|
-
const existingVersions = await assetManager.listExistingVersions();
|
|
692
|
-
const versionCount = existingVersions.filter((v) => v.id !== buildId).length;
|
|
693
|
-
if (versionCount > 0) {
|
|
694
|
-
logger.log(`Restoring build files from the last ${versionCount} release${versionCount > 1 ? "s" : ""}...`);
|
|
695
|
-
}
|
|
696
|
-
await assetManager.restoreOldAssetsToPublic(buildId, outputDir, assets, isExistingVersion);
|
|
697
|
-
await assetManager.augmentBuildMetadata(buildId, outputDir);
|
|
698
|
-
await assetManager.cleanupExpiredVersions();
|
|
699
719
|
});
|
|
700
720
|
}
|
|
701
721
|
if (options.checkForUpdateStrategy === "ws") {
|
package/dist/module.json
CHANGED
package/dist/module.mjs
CHANGED
|
@@ -204,7 +204,10 @@ async function resolveBuildTimeDriver(storage) {
|
|
|
204
204
|
if (!namespaceId) {
|
|
205
205
|
namespaceId = await detectCloudflareKVNamespace();
|
|
206
206
|
}
|
|
207
|
-
return cloudflareKVWranglerDriver(
|
|
207
|
+
return cloudflareKVWranglerDriver({
|
|
208
|
+
namespaceId,
|
|
209
|
+
...driverOptions
|
|
210
|
+
});
|
|
208
211
|
}
|
|
209
212
|
case "vercel-kv": {
|
|
210
213
|
return vercelKVCLIDriver(driverOptions);
|
|
@@ -548,7 +551,8 @@ function createAssetManager(options) {
|
|
|
548
551
|
storeAssetsInStorage,
|
|
549
552
|
listExistingVersions,
|
|
550
553
|
restoreOldAssetsToPublic,
|
|
551
|
-
augmentBuildMetadata
|
|
554
|
+
augmentBuildMetadata,
|
|
555
|
+
getManifest: () => getVersionManifest(storage)
|
|
552
556
|
};
|
|
553
557
|
}
|
|
554
558
|
|
|
@@ -670,29 +674,45 @@ export {}
|
|
|
670
674
|
nuxt.options.nitro.publicAssets = nuxt.options.nitro.publicAssets || [];
|
|
671
675
|
nuxt.options.nitro.publicAssets.push({
|
|
672
676
|
dir: swPath,
|
|
673
|
-
maxAge: 0
|
|
677
|
+
maxAge: 0
|
|
674
678
|
// Service workers should not be cached
|
|
675
|
-
baseURL: "/_skew"
|
|
676
679
|
});
|
|
677
680
|
if (options.storage && options.bundlePreviousDeploymentChunks) {
|
|
678
|
-
nuxt.hook("nitro:
|
|
681
|
+
nuxt.hook("nitro:init", (nitro) => {
|
|
679
682
|
const buildId = nuxt.options.runtimeConfig.app.buildId ||= nuxt.options.buildId;
|
|
680
|
-
|
|
681
|
-
|
|
682
|
-
|
|
683
|
-
|
|
683
|
+
let assetManager;
|
|
684
|
+
nitro.hooks.hook("compiled", async () => {
|
|
685
|
+
const outputDir = nitro.options.output.dir;
|
|
686
|
+
assetManager = createAssetManager({
|
|
687
|
+
...options,
|
|
688
|
+
driver: await resolveBuildTimeDriver(options.storage)
|
|
689
|
+
});
|
|
690
|
+
const assets = await assetManager.getAssetsFromBuild(outputDir);
|
|
691
|
+
const { isExistingVersion } = await assetManager.updateVersionsManifest(buildId, assets);
|
|
692
|
+
logger.log(`Storing ${assets.length} assets in storage...`);
|
|
693
|
+
await assetManager.storeAssetsInStorage(buildId, outputDir, assets);
|
|
694
|
+
const existingVersions = await assetManager.listExistingVersions();
|
|
695
|
+
const versionCount = existingVersions.filter((v) => v.id !== buildId).length;
|
|
696
|
+
if (versionCount > 0) {
|
|
697
|
+
const manifest = await assetManager.getManifest();
|
|
698
|
+
let totalAssets = 0;
|
|
699
|
+
const versionSizes = [];
|
|
700
|
+
for (const [vId, vData] of Object.entries(manifest.versions)) {
|
|
701
|
+
if (vId !== buildId) {
|
|
702
|
+
totalAssets += vData.assets.length;
|
|
703
|
+
versionSizes.push(`${vId.slice(0, 8)}:${vData.assets.length}`);
|
|
704
|
+
}
|
|
705
|
+
}
|
|
706
|
+
logger.log(`Restoring build files from ${versionCount} release${versionCount > 1 ? "s" : ""} (${totalAssets} assets) [${versionSizes.join(", ")}]...`);
|
|
707
|
+
}
|
|
708
|
+
await assetManager.restoreOldAssetsToPublic(buildId, outputDir, assets, isExistingVersion);
|
|
709
|
+
await assetManager.augmentBuildMetadata(buildId, outputDir);
|
|
710
|
+
});
|
|
711
|
+
nitro.hooks.hook("close", async () => {
|
|
712
|
+
if (assetManager) {
|
|
713
|
+
await assetManager.cleanupExpiredVersions();
|
|
714
|
+
}
|
|
684
715
|
});
|
|
685
|
-
const assets = await assetManager.getAssetsFromBuild(outputDir);
|
|
686
|
-
const { isExistingVersion } = await assetManager.updateVersionsManifest(buildId, assets);
|
|
687
|
-
await assetManager.storeAssetsInStorage(buildId, outputDir, assets);
|
|
688
|
-
const existingVersions = await assetManager.listExistingVersions();
|
|
689
|
-
const versionCount = existingVersions.filter((v) => v.id !== buildId).length;
|
|
690
|
-
if (versionCount > 0) {
|
|
691
|
-
logger.log(`Restoring build files from the last ${versionCount} release${versionCount > 1 ? "s" : ""}...`);
|
|
692
|
-
}
|
|
693
|
-
await assetManager.restoreOldAssetsToPublic(buildId, outputDir, assets, isExistingVersion);
|
|
694
|
-
await assetManager.augmentBuildMetadata(buildId, outputDir);
|
|
695
|
-
await assetManager.cleanupExpiredVersions();
|
|
696
716
|
});
|
|
697
717
|
}
|
|
698
718
|
if (options.checkForUpdateStrategy === "ws") {
|
|
@@ -5,7 +5,8 @@ interface Props {
|
|
|
5
5
|
*/
|
|
6
6
|
forceOpen?: boolean;
|
|
7
7
|
}
|
|
8
|
-
declare var
|
|
8
|
+
declare var __VLS_7: {
|
|
9
|
+
version: any;
|
|
9
10
|
isCurrentChunksOutdated: any;
|
|
10
11
|
dismiss: any;
|
|
11
12
|
reload: any;
|
|
@@ -15,7 +16,7 @@ declare var __VLS_1: {
|
|
|
15
16
|
isAppOutdated: any;
|
|
16
17
|
};
|
|
17
18
|
type __VLS_Slots = {} & {
|
|
18
|
-
default?: (props: typeof
|
|
19
|
+
default?: (props: typeof __VLS_7) => any;
|
|
19
20
|
};
|
|
20
21
|
declare const __VLS_base: import("vue").DefineComponent<Props, {}, {}, {}, {}, import("vue").ComponentOptionsMixin, import("vue").ComponentOptionsMixin, {
|
|
21
22
|
dismiss: () => any;
|
|
@@ -3,11 +3,15 @@ import { useTimeAgo } from "@vueuse/core";
|
|
|
3
3
|
import { reloadNuxtApp } from "nuxt/app";
|
|
4
4
|
import { computed, ref } from "vue";
|
|
5
5
|
import { useSkewProtection } from "../composables/useSkewProtection";
|
|
6
|
+
defineOptions({
|
|
7
|
+
inheritAttrs: false
|
|
8
|
+
});
|
|
6
9
|
const props = defineProps({
|
|
7
10
|
forceOpen: { type: Boolean, required: false }
|
|
8
11
|
});
|
|
9
12
|
const emit = defineEmits(["dismiss", "reload", "update:open", "chunksOutdated"]);
|
|
10
13
|
const skewProtection = useSkewProtection();
|
|
14
|
+
const version = skewProtection.clientVersion;
|
|
11
15
|
const chunksOutdated = ref(false);
|
|
12
16
|
const appOutdated = ref(false);
|
|
13
17
|
const outdatedPayload = ref(null);
|
|
@@ -54,18 +58,19 @@ async function handleReload() {
|
|
|
54
58
|
persistState: true
|
|
55
59
|
});
|
|
56
60
|
}
|
|
57
|
-
const shouldRender = import.meta.client && !import.meta.prerender;
|
|
58
61
|
</script>
|
|
59
62
|
|
|
60
63
|
<template>
|
|
61
|
-
<
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
64
|
+
<ClientOnly>
|
|
65
|
+
<slot
|
|
66
|
+
:version="version"
|
|
67
|
+
:is-current-chunks-outdated="isCurrentChunksOutdated"
|
|
68
|
+
:dismiss="handleDismiss"
|
|
69
|
+
:reload="handleReload"
|
|
70
|
+
:time-ago="timeAgo"
|
|
71
|
+
:release-date="releaseDate"
|
|
72
|
+
:payload="outdatedPayload"
|
|
73
|
+
:is-app-outdated="isAppOutdated"
|
|
74
|
+
/>
|
|
75
|
+
</ClientOnly>
|
|
71
76
|
</template>
|
|
@@ -5,7 +5,8 @@ interface Props {
|
|
|
5
5
|
*/
|
|
6
6
|
forceOpen?: boolean;
|
|
7
7
|
}
|
|
8
|
-
declare var
|
|
8
|
+
declare var __VLS_7: {
|
|
9
|
+
version: any;
|
|
9
10
|
isCurrentChunksOutdated: any;
|
|
10
11
|
dismiss: any;
|
|
11
12
|
reload: any;
|
|
@@ -15,7 +16,7 @@ declare var __VLS_1: {
|
|
|
15
16
|
isAppOutdated: any;
|
|
16
17
|
};
|
|
17
18
|
type __VLS_Slots = {} & {
|
|
18
|
-
default?: (props: typeof
|
|
19
|
+
default?: (props: typeof __VLS_7) => any;
|
|
19
20
|
};
|
|
20
21
|
declare const __VLS_base: import("vue").DefineComponent<Props, {}, {}, {}, {}, import("vue").ComponentOptionsMixin, import("vue").ComponentOptionsMixin, {
|
|
21
22
|
dismiss: () => any;
|
|
@@ -3,7 +3,7 @@ import type { ChunksOutdatedPayload } from '../../types.js';
|
|
|
3
3
|
export declare function checkForUpdates(): Promise<void>;
|
|
4
4
|
export declare function useSkewProtection(): {
|
|
5
5
|
manifest: import("vue").Ref<NuxtAppManifestMeta | undefined, NuxtAppManifestMeta | undefined>;
|
|
6
|
-
clientVersion:
|
|
6
|
+
clientVersion: string;
|
|
7
7
|
isOutdated: import("vue").ComputedRef<boolean | undefined>;
|
|
8
8
|
onCurrentChunksOutdated: (callback: (payload: ChunksOutdatedPayload) => void | Promise<void>) => () => void;
|
|
9
9
|
onAppOutdated: (callback: (manifest?: NuxtAppManifestMeta) => void | Promise<void>) => () => void;
|
|
@@ -1,10 +1,12 @@
|
|
|
1
1
|
import { buildAssetsURL } from "#internal/nuxt/paths";
|
|
2
|
-
import {
|
|
2
|
+
import { useCookie, useNuxtApp } from "nuxt/app";
|
|
3
3
|
import { computed, onUnmounted, ref } from "vue";
|
|
4
|
+
import { useRuntimeConfigSkewProtection } from "./useRuntimeConfigSkewProtection.js";
|
|
4
5
|
export async function checkForUpdates() {
|
|
5
6
|
const nuxtApp = useNuxtApp();
|
|
6
|
-
const runtimeConfig =
|
|
7
|
-
const
|
|
7
|
+
const runtimeConfig = useRuntimeConfigSkewProtection();
|
|
8
|
+
const cookie = useCookie(runtimeConfig.cookie.name);
|
|
9
|
+
const clientVersion = import.meta.client ? cookie.value : runtimeConfig.buildId;
|
|
8
10
|
const meta = await $fetch(`${buildAssetsURL("builds/latest.json")}?${Date.now()}`).catch(() => {
|
|
9
11
|
return null;
|
|
10
12
|
});
|
|
@@ -14,8 +16,13 @@ export async function checkForUpdates() {
|
|
|
14
16
|
}
|
|
15
17
|
export function useSkewProtection() {
|
|
16
18
|
const nuxtApp = useNuxtApp();
|
|
17
|
-
const runtimeConfig =
|
|
18
|
-
const
|
|
19
|
+
const runtimeConfig = useRuntimeConfigSkewProtection();
|
|
20
|
+
const cookie = useCookie(runtimeConfig.cookie.name, {
|
|
21
|
+
default() {
|
|
22
|
+
return runtimeConfig.buildId;
|
|
23
|
+
}
|
|
24
|
+
});
|
|
25
|
+
const clientVersion = import.meta.client ? cookie.value : runtimeConfig.buildId;
|
|
19
26
|
const manifest = ref();
|
|
20
27
|
function onCurrentChunksOutdated(callback) {
|
|
21
28
|
const hook = nuxtApp.hooks.hook("skew-protection:chunks-outdated", callback);
|
|
@@ -1,13 +1,12 @@
|
|
|
1
|
-
import { defineNuxtPlugin, useRuntimeConfig } from "#app";
|
|
2
1
|
import { useEventSource } from "@vueuse/core";
|
|
2
|
+
import { defineNuxtPlugin } from "nuxt/app";
|
|
3
3
|
import { watch } from "vue";
|
|
4
4
|
import { logger } from "../../shared/logger.js";
|
|
5
|
-
import { checkForUpdates } from "../composables/useSkewProtection.js";
|
|
5
|
+
import { checkForUpdates, useSkewProtection } from "../composables/useSkewProtection.js";
|
|
6
6
|
export default defineNuxtPlugin({
|
|
7
7
|
name: "skew-protection:sse-updates",
|
|
8
8
|
setup(nuxtApp) {
|
|
9
|
-
const
|
|
10
|
-
const clientVersion = runtimeConfig.app.buildId;
|
|
9
|
+
const { clientVersion } = useSkewProtection();
|
|
11
10
|
const { data } = useEventSource(`/_skew/sse`, [], {
|
|
12
11
|
autoReconnect: {
|
|
13
12
|
retries: 10,
|
|
@@ -1,15 +1,12 @@
|
|
|
1
|
-
import { defineNuxtPlugin, useRuntimeConfig } from "#app";
|
|
2
1
|
import { useWebSocket } from "@vueuse/core";
|
|
2
|
+
import { defineNuxtPlugin } from "nuxt/app";
|
|
3
3
|
import { watch } from "vue";
|
|
4
4
|
import { logger } from "../../shared/logger.js";
|
|
5
|
-
import {
|
|
6
|
-
import { checkForUpdates } from "../composables/useSkewProtection.js";
|
|
5
|
+
import { checkForUpdates, useSkewProtection } from "../composables/useSkewProtection.js";
|
|
7
6
|
export default defineNuxtPlugin({
|
|
8
7
|
name: "skew-protection:websocket-updates",
|
|
9
8
|
setup(nuxtApp) {
|
|
10
|
-
const
|
|
11
|
-
const runtimeConfig = useRuntimeConfig();
|
|
12
|
-
const clientVersion = runtimeConfig.app.buildId;
|
|
9
|
+
const { clientVersion } = useSkewProtection();
|
|
13
10
|
const protocol = typeof window !== "undefined" && window.location.protocol === "https:" ? "wss:" : "ws:";
|
|
14
11
|
const url = typeof window !== "undefined" ? `${protocol}//${window.location.host}/_skew/ws` : "";
|
|
15
12
|
const {
|
|
@@ -41,21 +38,10 @@ export default defineNuxtPlugin({
|
|
|
41
38
|
return;
|
|
42
39
|
try {
|
|
43
40
|
const parsed = JSON.parse(message);
|
|
44
|
-
if (parsed.
|
|
41
|
+
if (parsed.version) {
|
|
45
42
|
const newVersion = parsed.version;
|
|
46
43
|
if (newVersion !== clientVersion) {
|
|
47
44
|
nuxtApp.runWithContext(checkForUpdates);
|
|
48
|
-
if (skewConfig.debug) {
|
|
49
|
-
logger.info(`Version update received via WebSocket: ${newVersion}`);
|
|
50
|
-
}
|
|
51
|
-
}
|
|
52
|
-
} else if (parsed.type === "connected") {
|
|
53
|
-
if (skewConfig.debug) {
|
|
54
|
-
logger.info("WebSocket connection acknowledged");
|
|
55
|
-
}
|
|
56
|
-
} else if (parsed.type === "pong") {
|
|
57
|
-
if (skewConfig.debug) {
|
|
58
|
-
logger.debug("Received pong from WebSocket");
|
|
59
45
|
}
|
|
60
46
|
}
|
|
61
47
|
} catch (error) {
|
|
@@ -1,10 +1,2 @@
|
|
|
1
|
-
declare const _default: import("#app").Plugin<
|
|
2
|
-
skewServiceWorker: {
|
|
3
|
-
getLoadedModules: () => Promise<string[]>;
|
|
4
|
-
};
|
|
5
|
-
}> & import("#app").ObjectPlugin<{
|
|
6
|
-
skewServiceWorker: {
|
|
7
|
-
getLoadedModules: () => Promise<string[]>;
|
|
8
|
-
};
|
|
9
|
-
}>;
|
|
1
|
+
declare const _default: import("#app").Plugin<Record<string, unknown>> & import("#app").ObjectPlugin<Record<string, unknown>>;
|
|
10
2
|
export default _default;
|
|
@@ -1,4 +1,5 @@
|
|
|
1
|
-
import { defineNuxtPlugin
|
|
1
|
+
import { defineNuxtPlugin } from "nuxt/app";
|
|
2
|
+
import { useSkewProtection } from "../composables/useSkewProtection.js";
|
|
2
3
|
function normalizePath(pathOrUrl) {
|
|
3
4
|
try {
|
|
4
5
|
const url = new URL(pathOrUrl);
|
|
@@ -13,10 +14,23 @@ export default defineNuxtPlugin({
|
|
|
13
14
|
if (!("serviceWorker" in navigator)) {
|
|
14
15
|
return;
|
|
15
16
|
}
|
|
16
|
-
const
|
|
17
|
-
const
|
|
18
|
-
|
|
19
|
-
|
|
17
|
+
const { clientVersion, onAppOutdated } = useSkewProtection();
|
|
18
|
+
const swRegistration = navigator.serviceWorker.register("/_nuxt-skew-sw.js");
|
|
19
|
+
swRegistration.then((registration) => {
|
|
20
|
+
const sw = registration.active || registration.installing || registration.waiting;
|
|
21
|
+
if (!sw)
|
|
22
|
+
return;
|
|
23
|
+
const alreadyLoadedModules = performance.getEntriesByType("resource").filter((r) => r.name.includes("/_nuxt/") && r.name.endsWith(".js")).map((r) => r.name);
|
|
24
|
+
alreadyLoadedModules.forEach((url) => {
|
|
25
|
+
sw.postMessage({ type: "ADD_MODULE", url });
|
|
26
|
+
});
|
|
27
|
+
});
|
|
28
|
+
async function getLoadedModules() {
|
|
29
|
+
const registration = await swRegistration;
|
|
30
|
+
const sw = registration.active;
|
|
31
|
+
if (!sw) {
|
|
32
|
+
return [];
|
|
33
|
+
}
|
|
20
34
|
return new Promise((resolve) => {
|
|
21
35
|
let timeoutId;
|
|
22
36
|
const messageHandler = (event) => {
|
|
@@ -27,7 +41,7 @@ export default defineNuxtPlugin({
|
|
|
27
41
|
}
|
|
28
42
|
};
|
|
29
43
|
navigator.serviceWorker.addEventListener("message", messageHandler);
|
|
30
|
-
|
|
44
|
+
sw.postMessage({ type: "GET_MODULES" });
|
|
31
45
|
timeoutId = setTimeout(() => {
|
|
32
46
|
navigator.serviceWorker.removeEventListener("message", messageHandler);
|
|
33
47
|
resolve([]);
|
|
@@ -55,44 +69,53 @@ export default defineNuxtPlugin({
|
|
|
55
69
|
});
|
|
56
70
|
}
|
|
57
71
|
}
|
|
58
|
-
|
|
72
|
+
onAppOutdated(async (_manifest) => {
|
|
59
73
|
const versions = _manifest?.skewProtection?.versions;
|
|
60
74
|
if (!versions) {
|
|
61
75
|
return;
|
|
62
76
|
}
|
|
63
77
|
const newVersionId = _manifest.id;
|
|
64
|
-
if (!clientVersion || clientVersion === newVersionId) {
|
|
65
|
-
return;
|
|
66
|
-
}
|
|
67
|
-
navigator.serviceWorker.controller?.postMessage({ type: "RESET_MODULES" });
|
|
68
78
|
const sortedVersions = Object.entries(versions).map(([id, data]) => ({ id, timestamp: new Date(data.timestamp).getTime() })).sort((a, b) => a.timestamp - b.timestamp);
|
|
69
79
|
const currentIdx = sortedVersions.findIndex((v) => v.id === clientVersion);
|
|
70
80
|
const newIdx = sortedVersions.findIndex((v) => v.id === newVersionId);
|
|
71
|
-
if (currentIdx === -1 || newIdx === -1) {
|
|
72
|
-
return;
|
|
73
|
-
}
|
|
74
81
|
const allDeletedChunks = [];
|
|
75
82
|
const passedReleases = [];
|
|
76
|
-
|
|
77
|
-
const version
|
|
78
|
-
if (version) {
|
|
83
|
+
if (currentIdx === -1) {
|
|
84
|
+
for (const version of sortedVersions) {
|
|
79
85
|
passedReleases.push(version.id);
|
|
80
86
|
const versionData = versions[version.id];
|
|
81
87
|
if (versionData?.deletedChunks) {
|
|
82
88
|
allDeletedChunks.push(...versionData.deletedChunks);
|
|
83
89
|
}
|
|
84
90
|
}
|
|
91
|
+
} else if (newIdx === -1) {
|
|
92
|
+
for (let i = currentIdx + 1; i < sortedVersions.length; i++) {
|
|
93
|
+
const version = sortedVersions[i];
|
|
94
|
+
if (version) {
|
|
95
|
+
passedReleases.push(version.id);
|
|
96
|
+
const versionData = versions[version.id];
|
|
97
|
+
if (versionData?.deletedChunks) {
|
|
98
|
+
allDeletedChunks.push(...versionData.deletedChunks);
|
|
99
|
+
}
|
|
100
|
+
}
|
|
101
|
+
}
|
|
102
|
+
} else {
|
|
103
|
+
for (let i = currentIdx + 1; i <= newIdx; i++) {
|
|
104
|
+
const version = sortedVersions[i];
|
|
105
|
+
if (version) {
|
|
106
|
+
passedReleases.push(version.id);
|
|
107
|
+
const versionData = versions[version.id];
|
|
108
|
+
if (versionData?.deletedChunks) {
|
|
109
|
+
allDeletedChunks.push(...versionData.deletedChunks);
|
|
110
|
+
}
|
|
111
|
+
}
|
|
112
|
+
}
|
|
85
113
|
}
|
|
86
114
|
if (allDeletedChunks.length > 0) {
|
|
115
|
+
await new Promise((resolve) => setTimeout(resolve, 100));
|
|
87
116
|
await checkDeletedChunks(allDeletedChunks, passedReleases);
|
|
88
117
|
}
|
|
118
|
+
swRegistration.then((reg) => reg.active?.postMessage({ type: "RESET_MODULES" }));
|
|
89
119
|
});
|
|
90
|
-
return {
|
|
91
|
-
provide: {
|
|
92
|
-
skewServiceWorker: {
|
|
93
|
-
getLoadedModules
|
|
94
|
-
}
|
|
95
|
-
}
|
|
96
|
-
};
|
|
97
120
|
}
|
|
98
121
|
});
|
|
@@ -3,15 +3,15 @@ import { useRuntimeConfig } from "nitropack/runtime";
|
|
|
3
3
|
export default defineWebSocketHandler({
|
|
4
4
|
open(peer) {
|
|
5
5
|
const runtimeConfig = useRuntimeConfig();
|
|
6
|
-
peer.send({
|
|
6
|
+
peer.send(JSON.stringify({
|
|
7
7
|
type: "connected",
|
|
8
8
|
version: runtimeConfig.app?.buildId,
|
|
9
9
|
timestamp: Date.now()
|
|
10
|
-
});
|
|
10
|
+
}));
|
|
11
11
|
},
|
|
12
12
|
message(peer, message) {
|
|
13
13
|
if (message.text().includes("ping")) {
|
|
14
|
-
peer.send("pong");
|
|
14
|
+
peer.send(JSON.stringify({ type: "pong" }));
|
|
15
15
|
}
|
|
16
16
|
}
|
|
17
17
|
});
|
package/dist/runtime/types.d.ts
CHANGED