Use tanstack for caching of events
This commit is contained in:
5
.github/copilot-instructions.md
vendored
5
.github/copilot-instructions.md
vendored
@@ -1,11 +1,14 @@
|
||||
GitHub Copilot Instructions for This Repository
|
||||
|
||||
Basics:
|
||||
Basics: These you need to really follow!
|
||||
- If you have any questions, always ask me first!
|
||||
- Use Svelte 5 runes exclusively
|
||||
- Declare reactive state with $state(); derive values with $derived(); run side-effect logic with $effect() etc.
|
||||
- When doing client-side loading, always implement placeholders and loaders, so the UI remains responsive and layout shifts are minimized.
|
||||
- Don't use placeholders and loaders for static data like heading etc.
|
||||
- Never use supabse-js. I am using supabse-ssr and supabase client is located in:
|
||||
- client: $props.data.supabse
|
||||
- server: $locals.supabase
|
||||
|
||||
Do not fall back to the legacy $: label syntax or Svelte 3/4 stores! This is important!
|
||||
|
||||
|
||||
27
package-lock.json
generated
27
package-lock.json
generated
@@ -11,6 +11,7 @@
|
||||
"@supabase/ssr": "^0.6.1",
|
||||
"@supabase/supabase-js": "^2.50.0",
|
||||
"@sveltejs/adapter-node": "^5.2.12",
|
||||
"@tanstack/svelte-query": "^5.83.0",
|
||||
"googleapis": "^150.0.1",
|
||||
"papaparse": "^5.5.3",
|
||||
"qrcode": "^1.5.4",
|
||||
@@ -1382,6 +1383,32 @@
|
||||
"vite": "^5.2.0 || ^6"
|
||||
}
|
||||
},
|
||||
"node_modules/@tanstack/query-core": {
|
||||
"version": "5.83.0",
|
||||
"resolved": "https://registry.npmjs.org/@tanstack/query-core/-/query-core-5.83.0.tgz",
|
||||
"integrity": "sha512-0M8dA+amXUkyz5cVUm/B+zSk3xkQAcuXuz5/Q/LveT4ots2rBpPTZOzd7yJa2Utsf8D2Upl5KyjhHRY+9lB/XA==",
|
||||
"license": "MIT",
|
||||
"funding": {
|
||||
"type": "github",
|
||||
"url": "https://github.com/sponsors/tannerlinsley"
|
||||
}
|
||||
},
|
||||
"node_modules/@tanstack/svelte-query": {
|
||||
"version": "5.83.0",
|
||||
"resolved": "https://registry.npmjs.org/@tanstack/svelte-query/-/svelte-query-5.83.0.tgz",
|
||||
"integrity": "sha512-8tNXhuoizntZXnAzo4yqUWgZZnklQkXGUNpb3YreW68DyCBhhrGbErnrODQs3fVc2ABcMvAHIki5uErbdzXH1A==",
|
||||
"license": "MIT",
|
||||
"dependencies": {
|
||||
"@tanstack/query-core": "5.83.0"
|
||||
},
|
||||
"funding": {
|
||||
"type": "github",
|
||||
"url": "https://github.com/sponsors/tannerlinsley"
|
||||
},
|
||||
"peerDependencies": {
|
||||
"svelte": "^3.54.0 || ^4.0.0 || ^5.0.0"
|
||||
}
|
||||
},
|
||||
"node_modules/@types/cookie": {
|
||||
"version": "0.6.0",
|
||||
"resolved": "https://registry.npmjs.org/@types/cookie/-/cookie-0.6.0.tgz",
|
||||
|
||||
@@ -33,6 +33,7 @@
|
||||
"@supabase/ssr": "^0.6.1",
|
||||
"@supabase/supabase-js": "^2.50.0",
|
||||
"@sveltejs/adapter-node": "^5.2.12",
|
||||
"@tanstack/svelte-query": "^5.83.0",
|
||||
"googleapis": "^150.0.1",
|
||||
"papaparse": "^5.5.3",
|
||||
"qrcode": "^1.5.4",
|
||||
|
||||
9
src/lib/helpers.ts
Normal file
9
src/lib/helpers.ts
Normal file
@@ -0,0 +1,9 @@
|
||||
|
||||
|
||||
export const reactiveQueryArgs = <T>(cb: () => T) => {
|
||||
const store = writable<T>();
|
||||
$effect.pre(() => {
|
||||
store.set(cb());
|
||||
});
|
||||
return store;
|
||||
};
|
||||
@@ -1,21 +1,34 @@
|
||||
<script lang="ts">
|
||||
// Add any navbar logic here if needed
|
||||
import { browser } from '$app/environment';
|
||||
import { QueryClient, QueryClientProvider } from '@tanstack/svelte-query';
|
||||
|
||||
const queryClient = new QueryClient({
|
||||
defaultOptions: {
|
||||
queries: {
|
||||
enabled: browser,
|
||||
staleTime: 5 * 60_000, // 5 min cache
|
||||
refetchOnWindowFocus: false
|
||||
}
|
||||
}
|
||||
});
|
||||
</script>
|
||||
|
||||
<nav class="bg-gray-50 border-b border-gray-300 text-gray-900 p-2">
|
||||
<div class="container max-w-2xl mx-auto p-2">
|
||||
<div class="flex items-center justify-between">
|
||||
<div class="font-bold text-lg">ScanWave</div>
|
||||
|
||||
<ul class="flex space-x-4">
|
||||
<li><a href="/private/home" class="hover:underline">Home</a></li>
|
||||
<li><a href="/private/scanner" class="hover:underline">Scanner</a></li>
|
||||
<li><a href="/private/events" class="hover:underline">Events</a></li>
|
||||
</ul>
|
||||
</div>
|
||||
</div>
|
||||
<nav class="border-b border-gray-300 bg-gray-50 p-2 text-gray-900">
|
||||
<div class="container mx-auto max-w-2xl p-2">
|
||||
<div class="flex items-center justify-between">
|
||||
<div class="text-lg font-bold">ScanWave</div>
|
||||
|
||||
<ul class="flex space-x-4">
|
||||
<li><a href="/private/home">Home</a></li>
|
||||
<li><a href="/private/scanner">Scanner</a></li>
|
||||
<li><a href="/private/events">Events</a></li>
|
||||
</ul>
|
||||
</div>
|
||||
</div>
|
||||
</nav>
|
||||
|
||||
<div class="container max-w-2xl mx-auto p-2 bg-white">
|
||||
<slot />
|
||||
</div>
|
||||
<QueryClientProvider client={queryClient}>
|
||||
<div class="container mx-auto max-w-2xl bg-white p-2">
|
||||
<slot />
|
||||
</div>
|
||||
</QueryClientProvider>
|
||||
|
||||
@@ -1,150 +1,64 @@
|
||||
<script lang="ts">
|
||||
import { onMount } from 'svelte';
|
||||
import SingleEvent from './SingleEvent.svelte';
|
||||
import { createQuery } from '@tanstack/svelte-query';
|
||||
import { getEvents } from './queries';
|
||||
import { writable } from 'svelte/store';
|
||||
|
||||
// Get Supabase client from props
|
||||
let { data } = $props();
|
||||
|
||||
// Types
|
||||
interface Event {
|
||||
id: string;
|
||||
name: string;
|
||||
date: string;
|
||||
archived: boolean; // Whether the event is from events_archived table
|
||||
}
|
||||
// Reactive state for search input and debounced search term
|
||||
let searchInput = $state('');
|
||||
let debouncedSearch = writable('');
|
||||
let searchTimeout: ReturnType<typeof setTimeout>;
|
||||
|
||||
// State
|
||||
let allEvents = $state<Event[]>([]); // All events from both tables
|
||||
let displayEvents = $state<Event[]>([]); // Events to display (filtered by search)
|
||||
let loading = $state(true);
|
||||
let error = $state('');
|
||||
let searchTerm = $state('');
|
||||
let isSearching = $state(false);
|
||||
let searchTimeout: ReturnType<typeof setTimeout> | null = null;
|
||||
|
||||
onMount(async () => {
|
||||
await loadEvents();
|
||||
});
|
||||
|
||||
async function loadEvents() {
|
||||
loading = true;
|
||||
error = '';
|
||||
try {
|
||||
// Fetch regular events
|
||||
const { data: eventsData, error: eventsError } = await data.supabase
|
||||
.from('events')
|
||||
.select('id, name, date')
|
||||
.order('date', { ascending: false });
|
||||
|
||||
if (eventsError) throw eventsError;
|
||||
|
||||
// Fetch archived events (limited to 20)
|
||||
const { data: archivedEventsData, error: archivedError } = await data.supabase
|
||||
.from('events_archived')
|
||||
.select('id, name, date')
|
||||
.order('date', { ascending: false })
|
||||
.limit(20);
|
||||
|
||||
if (archivedError) throw archivedError;
|
||||
|
||||
// Merge both arrays, marking archived events
|
||||
const regularEvents = (eventsData || []).map(event => ({ ...event, archived: false }));
|
||||
const archivedEvents = (archivedEventsData || []).map(event => ({ ...event, archived: true }));
|
||||
|
||||
// Sort all events by date (newest first)
|
||||
const combined = [...regularEvents, ...archivedEvents];
|
||||
|
||||
allEvents = combined;
|
||||
displayEvents = allEvents;
|
||||
|
||||
} catch (err) {
|
||||
console.error('Error loading events:', err);
|
||||
error = 'Failed to load events';
|
||||
} finally {
|
||||
loading = false;
|
||||
}
|
||||
}
|
||||
|
||||
function formatDate(dateString: string) {
|
||||
return new Date(dateString).toLocaleDateString('en-GB', {
|
||||
day: '2-digit',
|
||||
month: '2-digit',
|
||||
year: 'numeric'
|
||||
});
|
||||
}
|
||||
|
||||
async function searchEvents(term: string) {
|
||||
if (!term.trim()) {
|
||||
displayEvents = allEvents;
|
||||
return;
|
||||
}
|
||||
|
||||
isSearching = true;
|
||||
error = '';
|
||||
|
||||
try {
|
||||
// Search regular events
|
||||
const { data: regularResults, error: regularError } = await data.supabase
|
||||
.from('events')
|
||||
.select('id, name, date')
|
||||
.ilike('name', `%${term}%`)
|
||||
.order('date', { ascending: false });
|
||||
|
||||
if (regularError) {
|
||||
console.error('Regular events search error:', regularError);
|
||||
throw regularError;
|
||||
}
|
||||
|
||||
// Search archived events
|
||||
const { data: archivedResults, error: archivedError } = await data.supabase
|
||||
.from('events_archived')
|
||||
.select('id, name, date')
|
||||
.ilike('name', `%${term}%`)
|
||||
.order('date', { ascending: false })
|
||||
.limit(50);
|
||||
|
||||
if (archivedError) {
|
||||
console.error('Archived events search error:', archivedError);
|
||||
throw archivedError;
|
||||
}
|
||||
|
||||
// Merge search results
|
||||
const regularEvents = (regularResults || []).map(event => ({ ...event, archived: false }));
|
||||
const archivedEvents = (archivedResults || []).map(event => ({ ...event, archived: true }));
|
||||
|
||||
// Sort merged results by date (newest first)
|
||||
const combined = [...regularEvents, ...archivedEvents];
|
||||
combined.sort((a, b) => new Date(b.date).getTime() - new Date(a.date).getTime());
|
||||
|
||||
displayEvents = combined;
|
||||
|
||||
} catch (err) {
|
||||
console.error('Error searching events:', err);
|
||||
} finally {
|
||||
isSearching = false;
|
||||
}
|
||||
}
|
||||
|
||||
// Handle search term changes
|
||||
// Debounce the search input
|
||||
function handleSearchInput() {
|
||||
if (searchTimeout) {
|
||||
clearTimeout(searchTimeout);
|
||||
}
|
||||
|
||||
searchTimeout = setTimeout(() => {
|
||||
searchEvents(searchTerm);
|
||||
debouncedSearch.set(searchInput);
|
||||
}, 300);
|
||||
}
|
||||
|
||||
const eventsQuery = $derived(
|
||||
createQuery({
|
||||
queryKey: ['refetch', $debouncedSearch],
|
||||
queryFn: async () => getEvents(data.supabase, $debouncedSearch)
|
||||
})
|
||||
);
|
||||
|
||||
// Derived values for UI state
|
||||
let isLoading = $derived($eventsQuery.isLoading || $eventsQuery.isFetching);
|
||||
let displayEvents = $derived($eventsQuery.data || []);
|
||||
let hasError = $derived(!!$eventsQuery.error);
|
||||
let errorMessage = $derived($eventsQuery.error?.message);
|
||||
|
||||
// Format date helper function
|
||||
function formatDate(dateString: string) {
|
||||
const date = new Date(dateString);
|
||||
return date.toLocaleDateString('en-GB', {
|
||||
day: '2-digit',
|
||||
month: '2-digit',
|
||||
year: 'numeric'
|
||||
});
|
||||
}
|
||||
|
||||
// Clear search function
|
||||
function clearSearch() {
|
||||
searchTerm = '';
|
||||
displayEvents = allEvents;
|
||||
searchInput = '';
|
||||
debouncedSearch.set('');
|
||||
}
|
||||
</script>
|
||||
|
||||
<h1 class="text-2xl font-bold mb-4 mt-2 text-center">All Events</h1>
|
||||
<h1 class="mt-2 mb-4 text-center text-2xl font-bold">
|
||||
{$debouncedSearch ? `Search Results: "${$debouncedSearch}"` : 'All Events'}
|
||||
</h1>
|
||||
|
||||
<div class="grid grid-cols-1 md:grid-cols-2 gap-4 max-w-2xl mx-auto mb-10">
|
||||
{#if loading}
|
||||
{#if isLoading}
|
||||
<!-- Loading placeholders -->
|
||||
{#each Array(4) as _}
|
||||
<div class="block border border-gray-300 rounded bg-white p-4 min-h-[72px]">
|
||||
@@ -154,15 +68,9 @@
|
||||
</div>
|
||||
</div>
|
||||
{/each}
|
||||
{:else if error}
|
||||
{:else if hasError}
|
||||
<div class="col-span-full text-center py-8">
|
||||
<p class="text-red-600">{error}</p>
|
||||
<button
|
||||
onclick={loadEvents}
|
||||
class="mt-2 px-4 py-2 bg-blue-600 text-white rounded hover:bg-blue-700"
|
||||
>
|
||||
Try Again
|
||||
</button>
|
||||
<p class="text-red-600">{errorMessage}</p>
|
||||
</div>
|
||||
{:else if displayEvents.length === 0}
|
||||
<div class="col-span-full text-center py-8">
|
||||
@@ -181,38 +89,70 @@
|
||||
</div>
|
||||
|
||||
<!-- Bottom actions - Mobile optimized -->
|
||||
<div class="fixed bottom-0 left-0 right-0 z-50 bg-white border-t border-gray-300 shadow-lg pb-safe">
|
||||
<div class="pb-safe fixed right-0 bottom-0 left-0 z-50 border-t border-gray-300 bg-white">
|
||||
<!-- Search bar and New Event button layout -->
|
||||
<div class="max-w-2xl mx-auto px-4 py-3 flex flex-col sm:flex-row gap-3 sm:items-center">
|
||||
<div class="mx-auto flex max-w-2xl flex-col gap-3 px-4 py-3 sm:flex-row sm:items-center">
|
||||
<!-- Search bar - Full width on mobile, adaptive on desktop -->
|
||||
<div class="relative flex-grow">
|
||||
<input
|
||||
type="text"
|
||||
bind:value={searchTerm}
|
||||
bind:value={searchInput}
|
||||
oninput={handleSearchInput}
|
||||
placeholder="Search events..."
|
||||
class="w-full pl-10 pr-10 py-2.5 rounded-lg border border-gray-300 text-sm focus:outline-none focus:ring-2 focus:ring-blue-500 focus:border-transparent"
|
||||
class="w-full rounded-lg border border-gray-300 py-2.5 pr-10 pl-10 text-sm focus:border-transparent focus:ring-2 focus:ring-blue-500 focus:outline-none"
|
||||
/>
|
||||
<div class="absolute left-3 top-1/2 -translate-y-1/2">
|
||||
{#if isSearching}
|
||||
<svg class="animate-spin h-4 w-4 text-gray-400" xmlns="http://www.w3.org/2000/svg" fill="none" viewBox="0 0 24 24">
|
||||
<circle class="opacity-25" cx="12" cy="12" r="10" stroke="currentColor" stroke-width="4"></circle>
|
||||
<path class="opacity-75" fill="currentColor" d="M4 12a8 8 0 018-8V0C5.373 0 0 5.373 0 12h4zm2 5.291A7.962 7.962 0 014 12H0c0 3.042 1.135 5.824 3 7.938l3-2.647z"></path>
|
||||
<div class="absolute top-1/2 left-3 -translate-y-1/2">
|
||||
{#if isLoading}
|
||||
<svg
|
||||
class="h-4 w-4 animate-spin text-gray-400"
|
||||
xmlns="http://www.w3.org/2000/svg"
|
||||
fill="none"
|
||||
viewBox="0 0 24 24"
|
||||
>
|
||||
<circle class="opacity-25" cx="12" cy="12" r="10" stroke="currentColor" stroke-width="4"
|
||||
></circle>
|
||||
<path
|
||||
class="opacity-75"
|
||||
fill="currentColor"
|
||||
d="M4 12a8 8 0 018-8V0C5.373 0 0 5.373 0 12h4zm2 5.291A7.962 7.962 0 014 12H0c0 3.042 1.135 5.824 3 7.938l3-2.647z"
|
||||
></path>
|
||||
</svg>
|
||||
{:else}
|
||||
<svg class="h-4 w-4 text-gray-400" xmlns="http://www.w3.org/2000/svg" fill="none" viewBox="0 0 24 24" stroke="currentColor">
|
||||
<path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M21 21l-6-6m2-5a7 7 0 11-14 0 7 7 0 0114 0z" />
|
||||
<svg
|
||||
class="h-4 w-4 text-gray-400"
|
||||
xmlns="http://www.w3.org/2000/svg"
|
||||
fill="none"
|
||||
viewBox="0 0 24 24"
|
||||
stroke="currentColor"
|
||||
>
|
||||
<path
|
||||
stroke-linecap="round"
|
||||
stroke-linejoin="round"
|
||||
stroke-width="2"
|
||||
d="M21 21l-6-6m2-5a7 7 0 11-14 0 7 7 0 0114 0z"
|
||||
/>
|
||||
</svg>
|
||||
{/if}
|
||||
</div>
|
||||
{#if searchTerm}
|
||||
{#if searchInput}
|
||||
<button
|
||||
onclick={clearSearch}
|
||||
class="absolute right-3 top-1/2 -translate-y-1/2 text-gray-400 hover:text-gray-600"
|
||||
class="absolute top-1/2 right-3 -translate-y-1/2 text-gray-400 hover:text-gray-600"
|
||||
aria-label="Clear search"
|
||||
>
|
||||
<svg class="h-4 w-4" xmlns="http://www.w3.org/2000/svg" fill="none" viewBox="0 0 24 24" stroke="currentColor">
|
||||
<path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M6 18L18 6M6 6l12 12" />
|
||||
<svg
|
||||
class="h-4 w-4"
|
||||
xmlns="http://www.w3.org/2000/svg"
|
||||
fill="none"
|
||||
viewBox="0 0 24 24"
|
||||
stroke="currentColor"
|
||||
>
|
||||
<path
|
||||
stroke-linecap="round"
|
||||
stroke-linejoin="round"
|
||||
stroke-width="2"
|
||||
d="M6 18L18 6M6 6l12 12"
|
||||
/>
|
||||
</svg>
|
||||
</button>
|
||||
{/if}
|
||||
@@ -221,11 +161,22 @@
|
||||
<!-- New Event button - Adaptive width -->
|
||||
<a
|
||||
href="/private/events/event/new"
|
||||
class="bg-blue-600 hover:bg-blue-700 text-white font-bold py-2.5 px-6 rounded-lg transition text-center whitespace-nowrap sm:flex-shrink-0"
|
||||
class="rounded-lg bg-blue-600 px-6 py-2.5 text-center font-bold whitespace-nowrap text-white transition hover:bg-blue-700 sm:flex-shrink-0"
|
||||
>
|
||||
<span class="flex items-center justify-center gap-2">
|
||||
<svg xmlns="http://www.w3.org/2000/svg" class="h-5 w-5" fill="none" viewBox="0 0 24 24" stroke="currentColor">
|
||||
<path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M12 4v16m8-8H4" />
|
||||
<svg
|
||||
xmlns="http://www.w3.org/2000/svg"
|
||||
class="h-5 w-5"
|
||||
fill="none"
|
||||
viewBox="0 0 24 24"
|
||||
stroke="currentColor"
|
||||
>
|
||||
<path
|
||||
stroke-linecap="round"
|
||||
stroke-linejoin="round"
|
||||
stroke-width="2"
|
||||
d="M12 4v16m8-8H4"
|
||||
/>
|
||||
</svg>
|
||||
New Event
|
||||
</span>
|
||||
@@ -234,4 +185,4 @@
|
||||
</div>
|
||||
|
||||
<!-- Add padding to bottom of content to prevent overlap with fixed bottom bar -->
|
||||
<div class="h-24"></div>
|
||||
<div class="h-24"></div>
|
||||
|
||||
73
src/routes/private/events/queries.ts
Normal file
73
src/routes/private/events/queries.ts
Normal file
@@ -0,0 +1,73 @@
|
||||
import type { SupabaseClient } from '@supabase/supabase-js';
|
||||
|
||||
export interface Event {
|
||||
id: string;
|
||||
name: string;
|
||||
date: string;
|
||||
archived: boolean;
|
||||
}
|
||||
|
||||
/**
|
||||
* Unified function to get events or search events based on a search term
|
||||
* @param supabase The Supabase client
|
||||
* @param searchTerm Optional search term - if provided, will filter by name
|
||||
* @returns Combined array of regular and archived events
|
||||
*/
|
||||
export async function getEvents(supabase: SupabaseClient, searchTerm: string = '') {
|
||||
try {
|
||||
const searchPattern = searchTerm.trim() ? `%${searchTerm}%` : null;
|
||||
|
||||
console.log('Actually fetching!');
|
||||
|
||||
// Build regular events query
|
||||
let regularQuery = supabase
|
||||
.from('events')
|
||||
.select('id, name, date')
|
||||
.order('date', { ascending: false });
|
||||
|
||||
// Apply search filter if needed
|
||||
if (searchPattern) {
|
||||
regularQuery = regularQuery.ilike('name', searchPattern);
|
||||
}
|
||||
|
||||
// Fetch regular events
|
||||
const { data: regularEvents, error: regularError } = await regularQuery;
|
||||
|
||||
if (regularError) throw regularError;
|
||||
|
||||
// Build archived events query
|
||||
let archivedQuery = supabase
|
||||
.from('events_archived')
|
||||
.select('id, name, date')
|
||||
.order('date', { ascending: false })
|
||||
.limit(searchPattern ? 50 : 20); // Fetch more when searching
|
||||
|
||||
// Apply search filter if needed
|
||||
if (searchPattern) {
|
||||
archivedQuery = archivedQuery.ilike('name', searchPattern);
|
||||
}
|
||||
|
||||
// Fetch archived events
|
||||
const { data: archivedEvents, error: archivedError } = await archivedQuery;
|
||||
|
||||
if (archivedError) throw archivedError;
|
||||
|
||||
// Merge both arrays, marking archived events
|
||||
const regularMapped = (regularEvents || []).map((event) => ({ ...event, archived: false }));
|
||||
const archivedMapped = (archivedEvents || []).map((event) => ({
|
||||
...event,
|
||||
archived: true
|
||||
}));
|
||||
|
||||
// Sort all events by date (newest first)
|
||||
const combined = [...regularMapped, ...archivedMapped];
|
||||
combined.sort((a, b) => new Date(b.date).getTime() - new Date(a.date).getTime());
|
||||
|
||||
return combined;
|
||||
} catch (error) {
|
||||
console.error('Error fetching events:', error);
|
||||
throw new Error(searchTerm.trim()
|
||||
? `Failed to search events for "${searchTerm}"`
|
||||
: 'Failed to load events');
|
||||
}
|
||||
}
|
||||
@@ -1,62 +1,18 @@
|
||||
/// <reference types="@sveltejs/kit" />
|
||||
import { build, files, version } from '$service-worker';
|
||||
|
||||
// Create unique cache names for this deployment
|
||||
const STATIC_CACHE = `static-cache-${version}`;
|
||||
const SUPABASE_CACHE = `supabase-cache-${version}`;
|
||||
const DYNAMIC_CACHE = `dynamic-cache-${version}`;
|
||||
// Create a unique cache name for this deployment
|
||||
const CACHE = `cache-${version}`;
|
||||
|
||||
const ASSETS = [
|
||||
...build, // the app itself
|
||||
...files // everything in `static`
|
||||
];
|
||||
|
||||
// Configure which Supabase endpoints to cache and for how long
|
||||
const SUPABASE_CACHE_CONFIG = [
|
||||
// Format: { urlPattern: RegExp, maxAgeSeconds: number, strategy: 'cache-first' | 'network-first' | 'stale-while-revalidate' }
|
||||
{ urlPattern: /events/, maxAgeSeconds: 3600, strategy: 'stale-while-revalidate' },
|
||||
{ urlPattern: /participants/, maxAgeSeconds: 3600, strategy: 'network-first' },
|
||||
{ urlPattern: /sections/, maxAgeSeconds: 3600, strategy: 'cache-first' },
|
||||
{ urlPattern: /profiles/, maxAgeSeconds: 3600, strategy: 'cache-first' },
|
||||
];
|
||||
|
||||
// Helper to determine if a request is for Supabase
|
||||
function isSupabaseRequest(url) {
|
||||
return url.hostname.includes('supabase.co');
|
||||
}
|
||||
|
||||
// Helper to find matching cache config for a Supabase URL
|
||||
function getSupabaseCacheConfig(url) {
|
||||
const urlString = url.toString();
|
||||
return SUPABASE_CACHE_CONFIG.find(config => config.urlPattern.test(urlString));
|
||||
}
|
||||
|
||||
// Helper to check if cached response is expired
|
||||
function isCacheExpired(cachedResponse, maxAgeSeconds) {
|
||||
if (!cachedResponse) return true;
|
||||
|
||||
const cachedAt = new Date(cachedResponse.headers.get('sw-cache-timestamp') || 0);
|
||||
const now = new Date();
|
||||
return (now.getTime() - cachedAt.getTime()) > (maxAgeSeconds * 1000);
|
||||
}
|
||||
|
||||
// Helper to add timestamp to cached responses
|
||||
function addTimestampToResponse(response) {
|
||||
const clonedResponse = response.clone();
|
||||
const headers = new Headers(clonedResponse.headers);
|
||||
headers.set('sw-cache-timestamp', new Date().toISOString());
|
||||
|
||||
return new Response(clonedResponse.body, {
|
||||
status: clonedResponse.status,
|
||||
statusText: clonedResponse.statusText,
|
||||
headers
|
||||
});
|
||||
}
|
||||
|
||||
self.addEventListener('install', (event) => {
|
||||
// Create a new cache and add all files to it
|
||||
async function addFilesToCache() {
|
||||
const cache = await caches.open(STATIC_CACHE);
|
||||
const cache = await caches.open(CACHE);
|
||||
await cache.addAll(ASSETS);
|
||||
}
|
||||
|
||||
@@ -67,9 +23,7 @@ self.addEventListener('activate', (event) => {
|
||||
// Remove previous cached data from disk
|
||||
async function deleteOldCaches() {
|
||||
for (const key of await caches.keys()) {
|
||||
if (![STATIC_CACHE, SUPABASE_CACHE, DYNAMIC_CACHE].includes(key)) {
|
||||
await caches.delete(key);
|
||||
}
|
||||
if (key !== CACHE) await caches.delete(key);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -77,121 +31,53 @@ self.addEventListener('activate', (event) => {
|
||||
});
|
||||
|
||||
self.addEventListener('fetch', (event) => {
|
||||
// ignore POST requests and other mutations
|
||||
// ignore POST requests etc
|
||||
if (event.request.method !== 'GET') return;
|
||||
|
||||
async function respond() {
|
||||
const url = new URL(event.request.url);
|
||||
|
||||
// Skip caching for auth routes and auth requests
|
||||
if (url.pathname.startsWith('/auth/') || url.pathname.includes('auth/v1')) {
|
||||
// Skip caching for auth routes
|
||||
if (url.pathname.startsWith('/auth/')) {
|
||||
return fetch(event.request);
|
||||
}
|
||||
|
||||
// Handle static assets
|
||||
const cache = await caches.open(CACHE);
|
||||
|
||||
// `build`/`files` can always be served from the cache
|
||||
if (ASSETS.includes(url.pathname)) {
|
||||
const staticCache = await caches.open(STATIC_CACHE);
|
||||
const cachedResponse = await staticCache.match(url.pathname);
|
||||
|
||||
if (cachedResponse) {
|
||||
return cachedResponse;
|
||||
const response = await cache.match(url.pathname);
|
||||
|
||||
if (response) {
|
||||
return response;
|
||||
}
|
||||
}
|
||||
|
||||
// Handle Supabase requests with specific caching strategies
|
||||
if (isSupabaseRequest(url)) {
|
||||
const cacheConfig = getSupabaseCacheConfig(url);
|
||||
|
||||
if (!cacheConfig) {
|
||||
// No specific config, use network-only for unconfigured Supabase endpoints
|
||||
return fetch(event.request);
|
||||
}
|
||||
|
||||
const supabaseCache = await caches.open(SUPABASE_CACHE);
|
||||
const cachedResponse = await supabaseCache.match(event.request);
|
||||
|
||||
// Apply the appropriate caching strategy based on config
|
||||
switch (cacheConfig.strategy) {
|
||||
case 'cache-first': {
|
||||
// If we have a valid cache that's not expired, use it
|
||||
if (cachedResponse && !isCacheExpired(cachedResponse, cacheConfig.maxAgeSeconds)) {
|
||||
return cachedResponse;
|
||||
}
|
||||
|
||||
// Otherwise fetch and update cache
|
||||
try {
|
||||
const response = await fetch(event.request);
|
||||
if (response.ok) {
|
||||
const timestampedResponse = addTimestampToResponse(response);
|
||||
supabaseCache.put(event.request, timestampedResponse.clone());
|
||||
return timestampedResponse;
|
||||
}
|
||||
return response;
|
||||
} catch (err) {
|
||||
// If offline and we have any cached version, return it even if expired
|
||||
if (cachedResponse) return cachedResponse;
|
||||
throw err;
|
||||
}
|
||||
}
|
||||
|
||||
case 'stale-while-revalidate': {
|
||||
// Start network fetch
|
||||
const fetchPromise = fetch(event.request)
|
||||
.then(response => {
|
||||
if (response.ok) {
|
||||
const timestampedResponse = addTimestampToResponse(response);
|
||||
supabaseCache.put(event.request, timestampedResponse.clone());
|
||||
return timestampedResponse;
|
||||
}
|
||||
return response;
|
||||
})
|
||||
.catch(() => {
|
||||
// If network fails, we'll use cache if available
|
||||
if (cachedResponse) return cachedResponse;
|
||||
throw new Error('Network request failed and no cache available');
|
||||
});
|
||||
|
||||
// Return cached response immediately if available, otherwise wait for network
|
||||
return cachedResponse || fetchPromise;
|
||||
}
|
||||
|
||||
case 'network-first':
|
||||
default: {
|
||||
try {
|
||||
const response = await fetch(event.request);
|
||||
if (response.ok) {
|
||||
const timestampedResponse = addTimestampToResponse(response);
|
||||
supabaseCache.put(event.request, timestampedResponse.clone());
|
||||
return timestampedResponse;
|
||||
}
|
||||
return response;
|
||||
} catch (err) {
|
||||
// If offline, try to return cached response
|
||||
if (cachedResponse) return cachedResponse;
|
||||
throw err;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// For everything else, use dynamic cache with network-first strategy
|
||||
const dynamicCache = await caches.open(DYNAMIC_CACHE);
|
||||
|
||||
|
||||
// for everything else, try the network first, but
|
||||
// fall back to the cache if we're offline
|
||||
try {
|
||||
const response = await fetch(event.request);
|
||||
|
||||
if (response.status === 200) {
|
||||
dynamicCache.put(event.request, response.clone());
|
||||
|
||||
// if we're offline, fetch can return a value that is not a Response
|
||||
// instead of throwing - and we can't pass this non-Response to respondWith
|
||||
if (!(response instanceof Response)) {
|
||||
throw new Error('invalid response from fetch');
|
||||
}
|
||||
|
||||
|
||||
if (response.status === 200) {
|
||||
cache.put(event.request, response.clone());
|
||||
}
|
||||
|
||||
return response;
|
||||
} catch (err) {
|
||||
const cachedResponse = await dynamicCache.match(event.request);
|
||||
|
||||
if (cachedResponse) {
|
||||
return cachedResponse;
|
||||
const response = await cache.match(event.request);
|
||||
|
||||
if (response) {
|
||||
return response;
|
||||
}
|
||||
|
||||
|
||||
// if there's no cache, then just error out
|
||||
// as there is nothing we can do to respond to this request
|
||||
throw err;
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user