Просмотр исходного кода

better handling of frontend updates, following svelte docs

Jan Kessler 1 неделя назад
Родитель
Сommit
dfd4037132
3 измененных файлов с 20 добавлено и 1 удалено
  1. 3 0
      Dockerfile
  2. 10 0
      src/routes/+layout.svelte
  3. 7 1
      svelte.config.js

+ 3 - 0
Dockerfile

@@ -26,6 +26,9 @@ ARG BUILD_HASH
 
 
 WORKDIR /app
 WORKDIR /app
 
 
+# to store git revision in build
+RUN apk add --no-cache git
+
 COPY package.json package-lock.json ./
 COPY package.json package-lock.json ./
 RUN npm ci
 RUN npm ci
 
 

+ 10 - 0
src/routes/+layout.svelte

@@ -49,6 +49,16 @@
 	import AppSidebar from '$lib/components/app/AppSidebar.svelte';
 	import AppSidebar from '$lib/components/app/AppSidebar.svelte';
 	import { chatCompletion } from '$lib/apis/openai';
 	import { chatCompletion } from '$lib/apis/openai';
 
 
+	import { beforeNavigate } from '$app/navigation';
+	import { updated } from '$app/state';
+
+	// handle frontend updates (https://svelte.dev/docs/kit/configuration#version)
+	beforeNavigate(({ willUnload, to }) => {
+		if (updated.current && !willUnload && to?.url) {
+			location.href = to.url.href;
+		}
+	});
+
 	setContext('i18n', i18n);
 	setContext('i18n', i18n);
 
 
 	const bc = new BroadcastChannel('active-tab-channel');
 	const bc = new BroadcastChannel('active-tab-channel');

+ 7 - 1
svelte.config.js

@@ -1,4 +1,5 @@
 import adapter from '@sveltejs/adapter-static';
 import adapter from '@sveltejs/adapter-static';
+import * as child_process from 'node:child_process';
 import { vitePreprocess } from '@sveltejs/vite-plugin-svelte';
 import { vitePreprocess } from '@sveltejs/vite-plugin-svelte';
 
 
 /** @type {import('@sveltejs/kit').Config} */
 /** @type {import('@sveltejs/kit').Config} */
@@ -14,7 +15,12 @@ const config = {
 			pages: 'build',
 			pages: 'build',
 			assets: 'build',
 			assets: 'build',
 			fallback: 'index.html'
 			fallback: 'index.html'
-		})
+		}),
+		// poll for new version name every 60 seconds (to trigger reload mechanic in +layout.svelte)
+		version: {
+			name: child_process.execSync('git rev-parse HEAD').toString().trim(),
+			pollInterval: 60000
+		}
 	},
 	},
 	vitePlugin: {
 	vitePlugin: {
 		// inspector: {
 		// inspector: {