Skip to content

Commit

Permalink
feat: app polling (#969)
Browse files Browse the repository at this point in the history
  • Loading branch information
quentinderoubaix authored Oct 24, 2024
1 parent eb703f3 commit dc2be70
Show file tree
Hide file tree
Showing 2 changed files with 2 additions and 17 deletions.
18 changes: 1 addition & 17 deletions demo/src/routes/+layout.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@
import './styles.scss';
import {afterNavigate, beforeNavigate, onNavigate} from '$app/navigation';
import {page, updated} from '$app/stores';
import {onMount} from 'svelte';
import MobileSubMenu from './menu/MobileSubMenu.svelte';
import MobileMenu from './menu/MobileMenu.svelte';
import MainSection from '$lib/layout/MainSection.svelte';
Expand All @@ -19,26 +18,11 @@
import type {Snippet} from 'svelte';
import type {LayoutData} from './$types';
let appUpdated = $state(false);
const onServiceWorkerUpdate = () => {
void updated.check();
};
let isMainPage = $derived($routeLevel$ === 0);
let isApi = $derived($page.route.id?.startsWith('/api/'));
onMount(() => {
const unsuscribe = updated.subscribe((val) => (appUpdated = val));
navigator.serviceWorker?.addEventListener('controllerchange', onServiceWorkerUpdate);
return () => {
unsuscribe();
navigator.serviceWorker?.removeEventListener('controllerchange', onServiceWorkerUpdate);
};
});
beforeNavigate(({willUnload, to}) => {
if (appUpdated && !willUnload && to?.url) {
if ($updated && !willUnload && to?.url) {
// force reload of the page on navigation when a new version of the site has been detected
location.href = to.url.href;
}
Expand Down
1 change: 1 addition & 0 deletions demo/svelte.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@ const config = {

version: {
name: child_process.execSync('git rev-parse HEAD').toString().trim(),
pollInterval: 30_000,
},
},
};
Expand Down

0 comments on commit dc2be70

Please sign in to comment.