nuxt-skew-protection 0.1.7 → 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 +46 -27
- package/dist/module.d.cts +1 -1
- package/dist/module.d.mts +1 -1
- package/dist/module.d.ts +1 -1
- package/dist/module.json +1 -1
- package/dist/module.mjs +47 -28
- package/dist/runtime/app/components/SkewNotification.d.vue.ts +3 -2
- package/dist/runtime/app/components/SkewNotification.vue +17 -10
- 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 +6 -8
- package/dist/runtime/app/composables/useSkewProtection.js +32 -16
- package/dist/runtime/app/plugins/check-updates-sse.client.js +5 -9
- package/dist/runtime/app/plugins/check-updates-websocket.client.js +11 -38
- package/dist/runtime/app/plugins/sw-track-user-modules.client.d.ts +1 -12
- package/dist/runtime/app/plugins/sw-track-user-modules.client.js +50 -34
- package/dist/runtime/server/routes/_skew/ws.js +3 -3
- package/dist/runtime/types.d.ts +1 -0
- package/package.json +2 -1
- package/dist/runtime/app/plugins/0.skew-protection.d.ts +0 -29
- package/dist/runtime/app/plugins/0.skew-protection.js +0 -41
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
|
|
|
@@ -633,9 +637,6 @@ export {}
|
|
|
633
637
|
name: "SkewNotification",
|
|
634
638
|
filePath: resolver.resolve(`./runtime/app/components/SkewNotification.vue`)
|
|
635
639
|
});
|
|
636
|
-
kit.addPlugin({
|
|
637
|
-
src: resolver.resolve("./runtime/app/plugins/0.skew-protection")
|
|
638
|
-
});
|
|
639
640
|
kit.addImports({
|
|
640
641
|
name: "useSkewProtection",
|
|
641
642
|
from: resolver.resolve("./runtime/app/composables/useSkewProtection")
|
|
@@ -658,10 +659,12 @@ export {}
|
|
|
658
659
|
middleware: true
|
|
659
660
|
});
|
|
660
661
|
}
|
|
661
|
-
|
|
662
|
-
|
|
663
|
-
|
|
664
|
-
|
|
662
|
+
if (options.cookie !== false) {
|
|
663
|
+
kit.addServerHandler({
|
|
664
|
+
handler: resolver.resolve("./runtime/server/middleware/set-skew-protection-cookie"),
|
|
665
|
+
middleware: true
|
|
666
|
+
});
|
|
667
|
+
}
|
|
665
668
|
kit.addPlugin({
|
|
666
669
|
src: resolver.resolve("./runtime/app/plugins/sw-track-user-modules.client"),
|
|
667
670
|
mode: "client"
|
|
@@ -674,29 +677,45 @@ export {}
|
|
|
674
677
|
nuxt.options.nitro.publicAssets = nuxt.options.nitro.publicAssets || [];
|
|
675
678
|
nuxt.options.nitro.publicAssets.push({
|
|
676
679
|
dir: swPath,
|
|
677
|
-
maxAge: 0
|
|
680
|
+
maxAge: 0
|
|
678
681
|
// Service workers should not be cached
|
|
679
|
-
baseURL: "/_skew"
|
|
680
682
|
});
|
|
681
683
|
if (options.storage && options.bundlePreviousDeploymentChunks) {
|
|
682
|
-
nuxt.hook("nitro:
|
|
684
|
+
nuxt.hook("nitro:init", (nitro) => {
|
|
683
685
|
const buildId = nuxt.options.runtimeConfig.app.buildId ||= nuxt.options.buildId;
|
|
684
|
-
|
|
685
|
-
|
|
686
|
-
|
|
687
|
-
|
|
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
|
+
}
|
|
688
718
|
});
|
|
689
|
-
const assets = await assetManager.getAssetsFromBuild(outputDir);
|
|
690
|
-
const { isExistingVersion } = await assetManager.updateVersionsManifest(buildId, assets);
|
|
691
|
-
await assetManager.storeAssetsInStorage(buildId, outputDir, assets);
|
|
692
|
-
const existingVersions = await assetManager.listExistingVersions();
|
|
693
|
-
const versionCount = existingVersions.filter((v) => v.id !== buildId).length;
|
|
694
|
-
if (versionCount > 0) {
|
|
695
|
-
logger.log(`Restoring build files from the last ${versionCount} release${versionCount > 1 ? "s" : ""}...`);
|
|
696
|
-
}
|
|
697
|
-
await assetManager.restoreOldAssetsToPublic(buildId, outputDir, assets, isExistingVersion);
|
|
698
|
-
await assetManager.augmentBuildMetadata(buildId, outputDir);
|
|
699
|
-
await assetManager.cleanupExpiredVersions();
|
|
700
719
|
});
|
|
701
720
|
}
|
|
702
721
|
if (options.checkForUpdateStrategy === "ws") {
|
package/dist/module.d.cts
CHANGED
|
@@ -30,7 +30,7 @@ interface ModuleOptions {
|
|
|
30
30
|
/**
|
|
31
31
|
* Cookie configuration for storing deployment version
|
|
32
32
|
*/
|
|
33
|
-
cookie?: Omit<CookieSerializeOptions, 'encode'> & {
|
|
33
|
+
cookie?: false | Omit<CookieSerializeOptions, 'encode'> & {
|
|
34
34
|
/**
|
|
35
35
|
* Cookie name for storing deployment version
|
|
36
36
|
* @default '__nkpv'
|
package/dist/module.d.mts
CHANGED
|
@@ -30,7 +30,7 @@ interface ModuleOptions {
|
|
|
30
30
|
/**
|
|
31
31
|
* Cookie configuration for storing deployment version
|
|
32
32
|
*/
|
|
33
|
-
cookie?: Omit<CookieSerializeOptions, 'encode'> & {
|
|
33
|
+
cookie?: false | Omit<CookieSerializeOptions, 'encode'> & {
|
|
34
34
|
/**
|
|
35
35
|
* Cookie name for storing deployment version
|
|
36
36
|
* @default '__nkpv'
|
package/dist/module.d.ts
CHANGED
|
@@ -30,7 +30,7 @@ interface ModuleOptions {
|
|
|
30
30
|
/**
|
|
31
31
|
* Cookie configuration for storing deployment version
|
|
32
32
|
*/
|
|
33
|
-
cookie?: Omit<CookieSerializeOptions, 'encode'> & {
|
|
33
|
+
cookie?: false | Omit<CookieSerializeOptions, 'encode'> & {
|
|
34
34
|
/**
|
|
35
35
|
* Cookie name for storing deployment version
|
|
36
36
|
* @default '__nkpv'
|
package/dist/module.json
CHANGED
package/dist/module.mjs
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import { writeFileSync, unlinkSync, existsSync, readFileSync, promises } from 'node:fs';
|
|
2
|
-
import { useNuxt, tryUseNuxt, useLogger, defineNuxtModule, createResolver, addTypeTemplate, addComponent,
|
|
2
|
+
import { useNuxt, tryUseNuxt, useLogger, defineNuxtModule, createResolver, addTypeTemplate, addComponent, addImports, addServerHandler, addPlugin } from '@nuxt/kit';
|
|
3
3
|
import { provider, env } from 'std-env';
|
|
4
4
|
import { join } from 'node:path';
|
|
5
5
|
import { tmpdir } from 'node:os';
|
|
@@ -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
|
|
|
@@ -630,9 +634,6 @@ export {}
|
|
|
630
634
|
name: "SkewNotification",
|
|
631
635
|
filePath: resolver.resolve(`./runtime/app/components/SkewNotification.vue`)
|
|
632
636
|
});
|
|
633
|
-
addPlugin({
|
|
634
|
-
src: resolver.resolve("./runtime/app/plugins/0.skew-protection")
|
|
635
|
-
});
|
|
636
637
|
addImports({
|
|
637
638
|
name: "useSkewProtection",
|
|
638
639
|
from: resolver.resolve("./runtime/app/composables/useSkewProtection")
|
|
@@ -655,10 +656,12 @@ export {}
|
|
|
655
656
|
middleware: true
|
|
656
657
|
});
|
|
657
658
|
}
|
|
658
|
-
|
|
659
|
-
|
|
660
|
-
|
|
661
|
-
|
|
659
|
+
if (options.cookie !== false) {
|
|
660
|
+
addServerHandler({
|
|
661
|
+
handler: resolver.resolve("./runtime/server/middleware/set-skew-protection-cookie"),
|
|
662
|
+
middleware: true
|
|
663
|
+
});
|
|
664
|
+
}
|
|
662
665
|
addPlugin({
|
|
663
666
|
src: resolver.resolve("./runtime/app/plugins/sw-track-user-modules.client"),
|
|
664
667
|
mode: "client"
|
|
@@ -671,29 +674,45 @@ export {}
|
|
|
671
674
|
nuxt.options.nitro.publicAssets = nuxt.options.nitro.publicAssets || [];
|
|
672
675
|
nuxt.options.nitro.publicAssets.push({
|
|
673
676
|
dir: swPath,
|
|
674
|
-
maxAge: 0
|
|
677
|
+
maxAge: 0
|
|
675
678
|
// Service workers should not be cached
|
|
676
|
-
baseURL: "/_skew"
|
|
677
679
|
});
|
|
678
680
|
if (options.storage && options.bundlePreviousDeploymentChunks) {
|
|
679
|
-
nuxt.hook("nitro:
|
|
681
|
+
nuxt.hook("nitro:init", (nitro) => {
|
|
680
682
|
const buildId = nuxt.options.runtimeConfig.app.buildId ||= nuxt.options.buildId;
|
|
681
|
-
|
|
682
|
-
|
|
683
|
-
|
|
684
|
-
|
|
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
|
+
}
|
|
685
715
|
});
|
|
686
|
-
const assets = await assetManager.getAssetsFromBuild(outputDir);
|
|
687
|
-
const { isExistingVersion } = await assetManager.updateVersionsManifest(buildId, assets);
|
|
688
|
-
await assetManager.storeAssetsInStorage(buildId, outputDir, assets);
|
|
689
|
-
const existingVersions = await assetManager.listExistingVersions();
|
|
690
|
-
const versionCount = existingVersions.filter((v) => v.id !== buildId).length;
|
|
691
|
-
if (versionCount > 0) {
|
|
692
|
-
logger.log(`Restoring build files from the last ${versionCount} release${versionCount > 1 ? "s" : ""}...`);
|
|
693
|
-
}
|
|
694
|
-
await assetManager.restoreOldAssetsToPublic(buildId, outputDir, assets, isExistingVersion);
|
|
695
|
-
await assetManager.augmentBuildMetadata(buildId, outputDir);
|
|
696
|
-
await assetManager.cleanupExpiredVersions();
|
|
697
716
|
});
|
|
698
717
|
}
|
|
699
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;
|
|
@@ -1,13 +1,17 @@
|
|
|
1
1
|
<script setup>
|
|
2
|
-
import { reloadNuxtApp } from "#imports";
|
|
3
2
|
import { useTimeAgo } from "@vueuse/core";
|
|
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);
|
|
@@ -57,13 +61,16 @@ async function handleReload() {
|
|
|
57
61
|
</script>
|
|
58
62
|
|
|
59
63
|
<template>
|
|
60
|
-
<
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
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>
|
|
69
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;
|
|
@@ -1,14 +1,12 @@
|
|
|
1
1
|
import type { NuxtAppManifestMeta } from 'nuxt/app';
|
|
2
|
-
import type { Ref } from 'vue';
|
|
3
2
|
import type { ChunksOutdatedPayload } from '../../types.js';
|
|
3
|
+
export declare function checkForUpdates(): Promise<void>;
|
|
4
4
|
export declare function useSkewProtection(): {
|
|
5
|
-
manifest: Ref<NuxtAppManifestMeta |
|
|
6
|
-
|
|
7
|
-
isOutdated:
|
|
8
|
-
cookie: Ref<string | undefined>;
|
|
9
|
-
} & {
|
|
5
|
+
manifest: import("vue").Ref<NuxtAppManifestMeta | undefined, NuxtAppManifestMeta | undefined>;
|
|
6
|
+
clientVersion: string;
|
|
7
|
+
isOutdated: import("vue").ComputedRef<boolean | undefined>;
|
|
10
8
|
onCurrentChunksOutdated: (callback: (payload: ChunksOutdatedPayload) => void | Promise<void>) => () => void;
|
|
11
|
-
onAppOutdated: (callback: (manifest?: NuxtAppManifestMeta) => void | Promise<void>) => void;
|
|
12
|
-
checkForUpdates:
|
|
9
|
+
onAppOutdated: (callback: (manifest?: NuxtAppManifestMeta) => void | Promise<void>) => () => void;
|
|
10
|
+
checkForUpdates: typeof checkForUpdates;
|
|
13
11
|
simulateUpdate(): Promise<void>;
|
|
14
12
|
};
|
|
@@ -1,20 +1,29 @@
|
|
|
1
1
|
import { buildAssetsURL } from "#internal/nuxt/paths";
|
|
2
|
-
import { useNuxtApp } from "nuxt/app";
|
|
3
|
-
import { onUnmounted } from "vue";
|
|
4
|
-
|
|
2
|
+
import { useCookie, useNuxtApp } from "nuxt/app";
|
|
3
|
+
import { computed, onUnmounted, ref } from "vue";
|
|
4
|
+
import { useRuntimeConfigSkewProtection } from "./useRuntimeConfigSkewProtection.js";
|
|
5
|
+
export async function checkForUpdates() {
|
|
5
6
|
const nuxtApp = useNuxtApp();
|
|
6
|
-
const
|
|
7
|
-
|
|
8
|
-
|
|
7
|
+
const runtimeConfig = useRuntimeConfigSkewProtection();
|
|
8
|
+
const cookie = useCookie(runtimeConfig.cookie.name);
|
|
9
|
+
const clientVersion = import.meta.client ? cookie.value : runtimeConfig.buildId;
|
|
10
|
+
const meta = await $fetch(`${buildAssetsURL("builds/latest.json")}?${Date.now()}`).catch(() => {
|
|
11
|
+
return null;
|
|
12
|
+
});
|
|
13
|
+
if (meta && meta?.id !== clientVersion) {
|
|
14
|
+
await nuxtApp.hooks.callHook("app:manifest:update", meta);
|
|
9
15
|
}
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
+
}
|
|
17
|
+
export function useSkewProtection() {
|
|
18
|
+
const nuxtApp = useNuxtApp();
|
|
19
|
+
const runtimeConfig = useRuntimeConfigSkewProtection();
|
|
20
|
+
const cookie = useCookie(runtimeConfig.cookie.name, {
|
|
21
|
+
default() {
|
|
22
|
+
return runtimeConfig.buildId;
|
|
16
23
|
}
|
|
17
|
-
}
|
|
24
|
+
});
|
|
25
|
+
const clientVersion = import.meta.client ? cookie.value : runtimeConfig.buildId;
|
|
26
|
+
const manifest = ref();
|
|
18
27
|
function onCurrentChunksOutdated(callback) {
|
|
19
28
|
const hook = nuxtApp.hooks.hook("skew-protection:chunks-outdated", callback);
|
|
20
29
|
onUnmounted(() => {
|
|
@@ -25,14 +34,21 @@ export function useSkewProtection() {
|
|
|
25
34
|
return hook;
|
|
26
35
|
}
|
|
27
36
|
function onAppOutdated(callback) {
|
|
28
|
-
const hook = nuxtApp.hooks.hook("app:manifest:update",
|
|
37
|
+
const hook = nuxtApp.hooks.hook("app:manifest:update", (_manifest) => {
|
|
38
|
+
manifest.value = _manifest;
|
|
39
|
+
callback(_manifest);
|
|
40
|
+
});
|
|
29
41
|
onUnmounted(() => {
|
|
30
42
|
if (typeof hook === "function") {
|
|
31
43
|
hook();
|
|
32
44
|
}
|
|
33
45
|
});
|
|
46
|
+
return hook;
|
|
34
47
|
}
|
|
35
|
-
return
|
|
48
|
+
return {
|
|
49
|
+
manifest,
|
|
50
|
+
clientVersion,
|
|
51
|
+
isOutdated: computed(() => manifest.value && clientVersion !== manifest.value.id),
|
|
36
52
|
onCurrentChunksOutdated,
|
|
37
53
|
onAppOutdated,
|
|
38
54
|
checkForUpdates,
|
|
@@ -46,5 +62,5 @@ export function useSkewProtection() {
|
|
|
46
62
|
passedReleases: []
|
|
47
63
|
});
|
|
48
64
|
}
|
|
49
|
-
}
|
|
65
|
+
};
|
|
50
66
|
}
|
|
@@ -1,16 +1,12 @@
|
|
|
1
|
-
import { defineNuxtPlugin } 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 { useSkewProtection } 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
|
-
|
|
9
|
-
|
|
10
|
-
],
|
|
11
|
-
setup() {
|
|
12
|
-
const skew = useSkewProtection();
|
|
13
|
-
const clientVersion = skew.cookie.value;
|
|
8
|
+
setup(nuxtApp) {
|
|
9
|
+
const { clientVersion } = useSkewProtection();
|
|
14
10
|
const { data } = useEventSource(`/_skew/sse`, [], {
|
|
15
11
|
autoReconnect: {
|
|
16
12
|
retries: 10,
|
|
@@ -31,7 +27,7 @@ export default defineNuxtPlugin({
|
|
|
31
27
|
if (message.type === "connected" && message.version) {
|
|
32
28
|
const newVersion = message.version;
|
|
33
29
|
if (newVersion !== clientVersion) {
|
|
34
|
-
|
|
30
|
+
nuxtApp.runWithContext(checkForUpdates);
|
|
35
31
|
}
|
|
36
32
|
}
|
|
37
33
|
});
|
|
@@ -1,22 +1,15 @@
|
|
|
1
|
-
import { defineNuxtPlugin } 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 { useSkewProtection } 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
|
-
dependsOn: [
|
|
10
|
-
"skew-protection:root"
|
|
11
|
-
],
|
|
12
8
|
setup(nuxtApp) {
|
|
13
|
-
const
|
|
14
|
-
const skewProtection = useSkewProtection();
|
|
15
|
-
const versionCookie = skewProtection.cookie;
|
|
9
|
+
const { clientVersion } = useSkewProtection();
|
|
16
10
|
const protocol = typeof window !== "undefined" && window.location.protocol === "https:" ? "wss:" : "ws:";
|
|
17
11
|
const url = typeof window !== "undefined" ? `${protocol}//${window.location.host}/_skew/ws` : "";
|
|
18
12
|
const {
|
|
19
|
-
status,
|
|
20
13
|
data,
|
|
21
14
|
open,
|
|
22
15
|
close
|
|
@@ -45,46 +38,26 @@ export default defineNuxtPlugin({
|
|
|
45
38
|
return;
|
|
46
39
|
try {
|
|
47
40
|
const parsed = JSON.parse(message);
|
|
48
|
-
if (parsed.
|
|
41
|
+
if (parsed.version) {
|
|
49
42
|
const newVersion = parsed.version;
|
|
50
|
-
if (newVersion !==
|
|
51
|
-
nuxtApp.
|
|
52
|
-
id: newVersion,
|
|
53
|
-
timestamp: parsed.timestamp || Date.now()
|
|
54
|
-
});
|
|
55
|
-
if (skewConfig.debug) {
|
|
56
|
-
logger.info(`Version update received via WebSocket: ${newVersion}`);
|
|
57
|
-
}
|
|
58
|
-
}
|
|
59
|
-
} else if (parsed.type === "connected") {
|
|
60
|
-
if (skewConfig.debug) {
|
|
61
|
-
logger.info("WebSocket connection acknowledged");
|
|
62
|
-
}
|
|
63
|
-
} else if (parsed.type === "pong") {
|
|
64
|
-
if (skewConfig.debug) {
|
|
65
|
-
logger.debug("Received pong from WebSocket");
|
|
43
|
+
if (newVersion !== clientVersion) {
|
|
44
|
+
nuxtApp.runWithContext(checkForUpdates);
|
|
66
45
|
}
|
|
67
46
|
}
|
|
68
47
|
} catch (error) {
|
|
69
48
|
logger.error("Failed to parse WebSocket message:", error);
|
|
70
49
|
}
|
|
71
50
|
});
|
|
72
|
-
watch(status, (newStatus) => {
|
|
73
|
-
if (skewConfig.debug) {
|
|
74
|
-
if (newStatus === "OPEN") {
|
|
75
|
-
logger.info("WebSocket connection established");
|
|
76
|
-
} else if (newStatus === "CONNECTING") {
|
|
77
|
-
logger.info("WebSocket connecting...");
|
|
78
|
-
} else if (newStatus === "CLOSED") {
|
|
79
|
-
logger.info("WebSocket connection closed");
|
|
80
|
-
}
|
|
81
|
-
}
|
|
82
|
-
});
|
|
83
51
|
nuxtApp.hook("app:mounted", () => {
|
|
84
52
|
open();
|
|
85
53
|
});
|
|
86
54
|
nuxtApp.hook("app:error", () => {
|
|
87
55
|
close();
|
|
88
56
|
});
|
|
57
|
+
if (import.meta.client) {
|
|
58
|
+
window.addEventListener("beforeunload", () => {
|
|
59
|
+
close();
|
|
60
|
+
});
|
|
61
|
+
}
|
|
89
62
|
}
|
|
90
63
|
});
|
|
@@ -1,13 +1,2 @@
|
|
|
1
|
-
|
|
2
|
-
declare const _default: import("#app").Plugin<{
|
|
3
|
-
skewServiceWorker: {
|
|
4
|
-
manifest: import("vue").Ref<NuxtAppManifestMeta | undefined, NuxtAppManifestMeta | undefined>;
|
|
5
|
-
getLoadedModules: () => Promise<string[]>;
|
|
6
|
-
};
|
|
7
|
-
}> & import("#app").ObjectPlugin<{
|
|
8
|
-
skewServiceWorker: {
|
|
9
|
-
manifest: import("vue").Ref<NuxtAppManifestMeta | undefined, NuxtAppManifestMeta | undefined>;
|
|
10
|
-
getLoadedModules: () => Promise<string[]>;
|
|
11
|
-
};
|
|
12
|
-
}>;
|
|
1
|
+
declare const _default: import("#app").Plugin<Record<string, unknown>> & import("#app").ObjectPlugin<Record<string, unknown>>;
|
|
13
2
|
export default _default;
|
|
@@ -1,5 +1,4 @@
|
|
|
1
|
-
import { defineNuxtPlugin } from "
|
|
2
|
-
import { ref } from "vue";
|
|
1
|
+
import { defineNuxtPlugin } from "nuxt/app";
|
|
3
2
|
import { useSkewProtection } from "../composables/useSkewProtection.js";
|
|
4
3
|
function normalizePath(pathOrUrl) {
|
|
5
4
|
try {
|
|
@@ -11,28 +10,39 @@ function normalizePath(pathOrUrl) {
|
|
|
11
10
|
}
|
|
12
11
|
export default defineNuxtPlugin({
|
|
13
12
|
name: "skew-protection:service-worker",
|
|
14
|
-
dependsOn: [
|
|
15
|
-
"skew-protection:root"
|
|
16
|
-
],
|
|
17
13
|
setup(nuxtApp) {
|
|
18
14
|
if (!("serviceWorker" in navigator)) {
|
|
19
15
|
return;
|
|
20
16
|
}
|
|
21
|
-
const
|
|
22
|
-
const
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
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
|
+
}
|
|
26
34
|
return new Promise((resolve) => {
|
|
35
|
+
let timeoutId;
|
|
27
36
|
const messageHandler = (event) => {
|
|
28
37
|
if (event.data.type === "MODULES_LIST") {
|
|
38
|
+
clearTimeout(timeoutId);
|
|
29
39
|
navigator.serviceWorker.removeEventListener("message", messageHandler);
|
|
30
40
|
resolve(event.data.modules);
|
|
31
41
|
}
|
|
32
42
|
};
|
|
33
43
|
navigator.serviceWorker.addEventListener("message", messageHandler);
|
|
34
|
-
|
|
35
|
-
setTimeout(() => {
|
|
44
|
+
sw.postMessage({ type: "GET_MODULES" });
|
|
45
|
+
timeoutId = setTimeout(() => {
|
|
36
46
|
navigator.serviceWorker.removeEventListener("message", messageHandler);
|
|
37
47
|
resolve([]);
|
|
38
48
|
}, 5e3);
|
|
@@ -59,47 +69,53 @@ export default defineNuxtPlugin({
|
|
|
59
69
|
});
|
|
60
70
|
}
|
|
61
71
|
}
|
|
62
|
-
|
|
63
|
-
manifest.value = _manifest;
|
|
72
|
+
onAppOutdated(async (_manifest) => {
|
|
64
73
|
const versions = _manifest?.skewProtection?.versions;
|
|
65
74
|
if (!versions) {
|
|
66
75
|
return;
|
|
67
76
|
}
|
|
68
77
|
const newVersionId = _manifest.id;
|
|
69
|
-
const currentVersionId = versionCookie.value;
|
|
70
|
-
if (!currentVersionId || currentVersionId === newVersionId) {
|
|
71
|
-
return;
|
|
72
|
-
}
|
|
73
|
-
navigator.serviceWorker.controller?.postMessage({ type: "RESET_MODULES" });
|
|
74
78
|
const sortedVersions = Object.entries(versions).map(([id, data]) => ({ id, timestamp: new Date(data.timestamp).getTime() })).sort((a, b) => a.timestamp - b.timestamp);
|
|
75
|
-
const currentIdx = sortedVersions.findIndex((v) => v.id ===
|
|
79
|
+
const currentIdx = sortedVersions.findIndex((v) => v.id === clientVersion);
|
|
76
80
|
const newIdx = sortedVersions.findIndex((v) => v.id === newVersionId);
|
|
77
|
-
if (currentIdx === -1 || newIdx === -1) {
|
|
78
|
-
return;
|
|
79
|
-
}
|
|
80
81
|
const allDeletedChunks = [];
|
|
81
82
|
const passedReleases = [];
|
|
82
|
-
|
|
83
|
-
const version
|
|
84
|
-
if (version) {
|
|
83
|
+
if (currentIdx === -1) {
|
|
84
|
+
for (const version of sortedVersions) {
|
|
85
85
|
passedReleases.push(version.id);
|
|
86
86
|
const versionData = versions[version.id];
|
|
87
87
|
if (versionData?.deletedChunks) {
|
|
88
88
|
allDeletedChunks.push(...versionData.deletedChunks);
|
|
89
89
|
}
|
|
90
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
|
+
}
|
|
91
113
|
}
|
|
92
114
|
if (allDeletedChunks.length > 0) {
|
|
115
|
+
await new Promise((resolve) => setTimeout(resolve, 100));
|
|
93
116
|
await checkDeletedChunks(allDeletedChunks, passedReleases);
|
|
94
117
|
}
|
|
118
|
+
swRegistration.then((reg) => reg.active?.postMessage({ type: "RESET_MODULES" }));
|
|
95
119
|
});
|
|
96
|
-
return {
|
|
97
|
-
provide: {
|
|
98
|
-
skewServiceWorker: {
|
|
99
|
-
manifest,
|
|
100
|
-
getLoadedModules
|
|
101
|
-
}
|
|
102
|
-
}
|
|
103
|
-
};
|
|
104
120
|
}
|
|
105
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
package/package.json
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "nuxt-skew-protection",
|
|
3
3
|
"type": "module",
|
|
4
|
-
"version": "0.1
|
|
4
|
+
"version": "0.2.1",
|
|
5
5
|
"description": "Instantly adopted Nuxt releases with long-lived build assets.",
|
|
6
6
|
"author": {
|
|
7
7
|
"name": "Harlan Wilton",
|
|
@@ -31,6 +31,7 @@
|
|
|
31
31
|
"dependencies": {
|
|
32
32
|
"@nuxt/kit": "^4.1.3",
|
|
33
33
|
"@nuxtjs/robots": "^5.5.5",
|
|
34
|
+
"@vueuse/core": "^13.9.0",
|
|
34
35
|
"consola": "^3.4.2",
|
|
35
36
|
"cookie-es": "^2.0.0",
|
|
36
37
|
"defu": "^6.1.4",
|
|
@@ -1,29 +0,0 @@
|
|
|
1
|
-
import type { NuxtAppManifestMeta } from 'nuxt/app';
|
|
2
|
-
declare const _default: import("#app").Plugin<{
|
|
3
|
-
skewProtection: {
|
|
4
|
-
manifest: import("vue").Ref<{
|
|
5
|
-
id: string;
|
|
6
|
-
timestamp: number;
|
|
7
|
-
} | null, NuxtAppManifestMeta | {
|
|
8
|
-
id: string;
|
|
9
|
-
timestamp: number;
|
|
10
|
-
} | null>;
|
|
11
|
-
currentVersion: any;
|
|
12
|
-
isOutdated: import("vue").ComputedRef<boolean>;
|
|
13
|
-
cookie: import("#app").CookieRef<any>;
|
|
14
|
-
};
|
|
15
|
-
}> & import("#app").ObjectPlugin<{
|
|
16
|
-
skewProtection: {
|
|
17
|
-
manifest: import("vue").Ref<{
|
|
18
|
-
id: string;
|
|
19
|
-
timestamp: number;
|
|
20
|
-
} | null, NuxtAppManifestMeta | {
|
|
21
|
-
id: string;
|
|
22
|
-
timestamp: number;
|
|
23
|
-
} | null>;
|
|
24
|
-
currentVersion: any;
|
|
25
|
-
isOutdated: import("vue").ComputedRef<boolean>;
|
|
26
|
-
cookie: import("#app").CookieRef<any>;
|
|
27
|
-
};
|
|
28
|
-
}>;
|
|
29
|
-
export default _default;
|
|
@@ -1,41 +0,0 @@
|
|
|
1
|
-
import { defineNuxtPlugin, useCookie, useRuntimeConfig } from "nuxt/app";
|
|
2
|
-
import { computed, ref } from "vue";
|
|
3
|
-
import { useRuntimeConfigSkewProtection } from "../composables/useRuntimeConfigSkewProtection.js";
|
|
4
|
-
export default defineNuxtPlugin({
|
|
5
|
-
name: "skew-protection:root",
|
|
6
|
-
enforce: "pre",
|
|
7
|
-
setup(nuxtApp) {
|
|
8
|
-
const runtimeConfig = useRuntimeConfig();
|
|
9
|
-
const { cookie: cookieConfig } = useRuntimeConfigSkewProtection();
|
|
10
|
-
const { name: cookieName, ...cookieOptions } = cookieConfig;
|
|
11
|
-
const cookie = useCookie(cookieName, {
|
|
12
|
-
default: () => runtimeConfig.app.buildId,
|
|
13
|
-
...cookieOptions
|
|
14
|
-
});
|
|
15
|
-
const currentVersion = runtimeConfig.app.buildId || cookie.value;
|
|
16
|
-
const latestVersion = ref(currentVersion);
|
|
17
|
-
const manifest = ref(null);
|
|
18
|
-
const isOutdated = computed(() => currentVersion !== latestVersion.value);
|
|
19
|
-
if (import.meta.client) {
|
|
20
|
-
nuxtApp.hooks.hook("app:manifest:update", (_manifest) => {
|
|
21
|
-
if (_manifest) {
|
|
22
|
-
manifest.value = _manifest;
|
|
23
|
-
const newVersion = _manifest.id;
|
|
24
|
-
if (newVersion && newVersion !== latestVersion.value) {
|
|
25
|
-
latestVersion.value = newVersion;
|
|
26
|
-
}
|
|
27
|
-
}
|
|
28
|
-
});
|
|
29
|
-
}
|
|
30
|
-
return {
|
|
31
|
-
provide: {
|
|
32
|
-
skewProtection: {
|
|
33
|
-
manifest,
|
|
34
|
-
currentVersion,
|
|
35
|
-
isOutdated,
|
|
36
|
-
cookie
|
|
37
|
-
}
|
|
38
|
-
}
|
|
39
|
-
};
|
|
40
|
-
}
|
|
41
|
-
});
|