Skip to content

Privacy-first password manager with built-in email aliasing. Fully encrypted and self-hostable.

Update WASM prefetch and deregister legacy service worker (#2413)

Leendert de Borst64c63079dfc8
diff --git a/apps/web/index.html b/apps/web/index.html
index 29715b922..5371619ba 100644
--- a/apps/web/index.html
+++ b/apps/web/index.html
@@ -20,7 +20,6 @@ https://github.com/aliasvault/aliasvault | AGPLv3
<link rel="apple-touch-icon" sizes="500x500" href="/img/icon-500.png" />
<link rel="apple-touch-icon" sizes="192x192" href="/img/icon-192.png" />
<link rel="stylesheet" href="/src/style.css" />
-<link rel="prefetch" href="/wasm/aliasvault_core_bg.wasm" as="fetch" type="application/wasm" crossorigin="anonymous" />
<script>
(function () {
var theme = localStorage.getItem('color-theme');
diff --git a/apps/web/package-lock.json b/apps/web/package-lock.json
index 295890730..c53d06ebc 100644
--- a/apps/web/package-lock.json
+++ b/apps/web/package-lock.json
@@ -40,8 +40,7 @@
"postcss": "^8.5.24",
"tailwindcss": "^3.4.17",
"typescript": "^5.6.3",
- "vite": "^8.0.0",
- "vite-plugin-static-copy": "^3.4.0"
+ "vite": "^8.0.0"
}
},
"../../core/client": {
@@ -5048,19 +5047,6 @@
"url": "https://github.com/sponsors/sindresorhus"
}
},
- "node_modules/p-map": {
- "version": "7.0.7",
- "resolved": "https://registry.npmjs.org/p-map/-/p-map-7.0.7.tgz",
- "integrity": "sha512-VaWRu2i4FJNRtiRWCuuQRgfQ1B7a6+gMSrO+3j0EQi/k0ULfS9kosRxGoiqwzIjZTDI02tGfk5mXXltLg6QtfQ==",
- "dev": true,
- "license": "MIT",
- "engines": {
- "node": ">=18"
- },
- "funding": {
- "url": "https://github.com/sponsors/sindresorhus"
- }
- },
"node_modules/p-try": {
"version": "2.2.0",
"resolved": "https://registry.npmjs.org/p-try/-/p-try-2.2.0.tgz",
@@ -6723,29 +6709,6 @@
}
}
},
- "node_modules/vite-plugin-static-copy": {
- "version": "3.4.0",
- "resolved": "https://registry.npmjs.org/vite-plugin-static-copy/-/vite-plugin-static-copy-3.4.0.tgz",
- "integrity": "sha512-ekryzCw0ouAOE8tw4RvVL/dfqguXzumsV3FBKoKso4MQ1MUUrUXtl5RI4KpJQUNGqFEsg9kxl4EvDl02YtA9VQ==",
- "dev": true,
- "license": "MIT",
- "dependencies": {
- "chokidar": "^3.6.0",
- "p-map": "^7.0.4",
- "picocolors": "^1.1.1",
- "tinyglobby": "^0.2.15"
- },
- "engines": {
- "node": "^18.0.0 || >=20.0.0"
- },
- "funding": {
- "type": "github",
- "url": "https://github.com/sponsors/sapphi-red"
- },
- "peerDependencies": {
- "vite": "^5.0.0 || ^6.0.0 || ^7.0.0 || ^8.0.0"
- }
- },
"node_modules/vite/node_modules/picomatch": {
"version": "4.0.7",
"resolved": "https://registry.npmjs.org/picomatch/-/picomatch-4.0.7.tgz",
diff --git a/apps/web/package.json b/apps/web/package.json
index 4135ebc36..1569627c8 100644
--- a/apps/web/package.json
+++ b/apps/web/package.json
@@ -18,7 +18,6 @@
"test:e2e:h": "node tests/run-e2e.mjs --headed --workers=1",
"test:e2e:pause": "node tests/run-e2e.mjs --pause",
"test:e2e:p": "node tests/run-e2e.mjs --pause"
-
},
"dependencies": {
"@aliasvault/client": "file:../../core/client",
@@ -52,8 +51,7 @@
"postcss": "^8.5.24",
"tailwindcss": "^3.4.17",
"typescript": "^5.6.3",
- "vite": "^8.0.0",
- "vite-plugin-static-copy": "^3.4.0"
+ "vite": "^8.0.0"
},
"overrides": {
"shell-quote": "1.8.4",
diff --git a/apps/web/public/service-worker.js b/apps/web/public/service-worker.js
new file mode 100644
index 000000000..7c3876405
--- /dev/null
+++ b/apps/web/public/service-worker.js
@@ -0,0 +1,20 @@
+/*
+ * NOTE: the previous Blazor WASM client registered a service worker at this path. The current React client does not
+ * support this anymore, however we keep this file here to properly deregister the old service worker to avoid update
+ * conflicts.
+ *
+ * TODO: remove this service worker declaration a while after version 0.31.0 is released.
+ */
+self.addEventListener('install', () => {
+ self.skipWaiting();
+});
+
+self.addEventListener('activate', (event) => {
+ event.waitUntil((async () => {
+ const cacheKeys = await caches.keys();
+ await Promise.all(cacheKeys.filter((key) => key.startsWith('offline-cache-')).map((key) => caches.delete(key)));
+ await self.registration.unregister();
+ const windows = await self.clients.matchAll({ type: 'window' });
+ await Promise.all(windows.map((client) => client.navigate(client.url)));
+ })());
+});
diff --git a/apps/web/src/main.tsx b/apps/web/src/main.tsx
index 097d9b36a..0374365ed 100644
--- a/apps/web/src/main.tsx
+++ b/apps/web/src/main.tsx
@@ -1,5 +1,6 @@
import '@/platform/ClientServices';
+import { getPlatform } from '@aliasvault/client/platform';
import ReactDOM from 'react-dom/client';
import App from '@/App';
@@ -41,12 +42,14 @@ const renderApp = (): void => {
};
/**
- * Load the runtime config and the translations before the first render; the loading screen in index.html stays
- * up until the app has painted.
+ * Load the Rust core wasm, the runtime config and the translations before the first render. The loading screen in
+ * index.html stays up until the app has painted, and shows its error state when any of these fail.
*/
const bootstrap = async (): Promise<void> => {
- await loadAppConfig();
- await initI18n();
+ await Promise.all([
+ getPlatform().rustCore.init(),
+ loadAppConfig().then(initI18n),
+ ]);
renderApp();
};
diff --git a/apps/web/src/platform/WebPlatform.ts b/apps/web/src/platform/WebPlatform.ts
index 960f2fa8c..0357ccd05 100644
--- a/apps/web/src/platform/WebPlatform.ts
+++ b/apps/web/src/platform/WebPlatform.ts
@@ -5,6 +5,7 @@
import { createRustSqliteEngine } from '@aliasvault/client/database/RustSqliteEngine';
import { type IClientPlatform, TranslatableMessage } from '@aliasvault/client/platform';
import { createWasmRustCore } from '@aliasvault/client/rust/WasmRustCore';
+import wasmUrl from '@aliasvault/client/wasm/aliasvault_core_bg.wasm?url';
import i18n from '@/i18n/i18n';
import { WebKeyValueStore } from '@/platform/WebKeyValueStore';
@@ -19,17 +20,10 @@ const TRANSLATION_KEYS: Record<TranslatableMessage, string> = {
};
/**
- * Where the wasm binary is served from (copied there by vite.config.ts).
- * @param file - the file name
+ * The Rust core, streamed into WebAssembly.instantiateStreaming; it also hosts the SQLite engine. The wasm URL is
+ * content hashed by Vite, so a new build never loads a cached binary from an older release.
*/
-function wasmUrl(file: string): string {
- return `${import.meta.env.BASE_URL}wasm/${file}`;
-}
-
-/**
- * The Rust core, streamed into WebAssembly.instantiateStreaming; it also hosts the SQLite engine.
- */
-const rustCore = createWasmRustCore(async (): Promise<Response> => fetch(wasmUrl('aliasvault_core_bg.wasm')));
+const rustCore = createWasmRustCore(async (): Promise<Response> => fetch(wasmUrl));
/**
* The platform the web app registers with the client core.
diff --git a/apps/web/vite.config.ts b/apps/web/vite.config.ts
index 796e482fd..9772c6af3 100644
--- a/apps/web/vite.config.ts
+++ b/apps/web/vite.config.ts
@@ -3,7 +3,6 @@ import path from 'node:path';
import react from '@vitejs/plugin-react';
import { defineConfig, type Plugin } from 'vite';
-import { viteStaticCopy } from 'vite-plugin-static-copy';
const CORE_DIR = path.resolve(import.meta.dirname, '../../core');
const LOCALES_DIR = path.resolve(import.meta.dirname, 'src/i18n/locales');
@@ -32,6 +31,23 @@ function loadingScreenLocales(): Plugin {
};
}
+/**
+ * Preload the content hashed Rust core wasm from index.html, so its download runs in parallel with the app bundle.
+ */
+function preloadCoreWasm(): Plugin {
+ return {
+ name: 'preload-core-wasm',
+ apply: 'build',
+ transformIndexHtml(_html, context) {
+ const wasm = Object.values(context.bundle ?? {}).find((chunk) => chunk.type === 'asset' && /aliasvault_core_bg-.*\.wasm$/.test(chunk.fileName));
+ if (!wasm) {
+ throw new Error('The Rust core wasm asset is missing from the bundle.');
+ }
+ return [{ tag: 'link', attrs: { rel: 'preload', href: `/${wasm.fileName}`, as: 'fetch', type: 'application/wasm', crossorigin: 'anonymous' }, injectTo: 'head' }];
+ },
+ };
+}
+
// See https://vite.dev/config/
export default defineConfig({
define: {
@@ -58,10 +74,6 @@ export default defineConfig({
plugins: [
react(),
loadingScreenLocales(),
- viteStaticCopy({
- targets: [
- { src: path.resolve(CORE_DIR, 'client/wasm/aliasvault_core_bg.wasm'), dest: 'wasm' },
- ],
- }),
+ preloadCoreWasm(),
],
});