diff --git a/waiter_pwa/src/context/LiveContext.jsx b/waiter_pwa/src/context/LiveContext.jsx index 5829c19..e61f565 100644 --- a/waiter_pwa/src/context/LiveContext.jsx +++ b/waiter_pwa/src/context/LiveContext.jsx @@ -1,3 +1,4 @@ +import { warmPhotos } from '../services/photoCache' import { createContext, useContext, useCallback, useEffect, useRef } from 'react' import { useQueryClient } from '@tanstack/react-query' import useAuthStore from '../store/authStore' @@ -67,6 +68,7 @@ export function LiveProvider({ children }) { ]) await db.products.bulkPut(prodRes.data) await db.categories.bulkPut(catRes.data) + warmPhotos(prodRes.data, catRes.data) // the menu's photos kept for offline (5.2) // Also update React Query cache so AddItemsPage gets it immediately queryClient.setQueryData(['products'], { products: prodRes.data, categories: catRes.data }) } catch { /* offline — snapshot stays as-is */ } diff --git a/waiter_pwa/src/hooks/useProductCache.js b/waiter_pwa/src/hooks/useProductCache.js index 85b0510..e3d5834 100644 --- a/waiter_pwa/src/hooks/useProductCache.js +++ b/waiter_pwa/src/hooks/useProductCache.js @@ -2,6 +2,7 @@ import { useQuery, useQueryClient } from '@tanstack/react-query' import { useEffect, useState } from 'react' import client from '../api/client' import db from '../db/posdb' +import { warmPhotos } from '../services/photoCache' export const PRODUCTS_KEY = ['products'] @@ -15,6 +16,7 @@ async function fetchAndCacheProducts() { db.products.bulkPut(products).catch(() => {}) db.categories.bulkPut(categories).catch(() => {}) + warmPhotos(products, categories) // kept for offline (5.2) return { products, categories } } diff --git a/waiter_pwa/src/services/photoCache.js b/waiter_pwa/src/services/photoCache.js new file mode 100644 index 0000000..f0f95f7 --- /dev/null +++ b/waiter_pwa/src/services/photoCache.js @@ -0,0 +1,30 @@ +// The menu's photos kept for offline (plan step 5.2). Photos come from the media library at +// /api/media/file//card: an address that never changes meaning, served with a year-long +// `immutable` cache header. So once a photo is downloaded the phone keeps it (the browser's +// cache; in the installed PWA also the service worker's `xenia-media` cache) and shows it +// without a connection. warmPhotos() downloads the menu's photos ahead, a few at a time, right +// after the menu is read, so a product never shown before still has its photo offline. +import { assetUrl } from '../config/server' + +const warmed = new Set() +const AT_ONCE = 4 + +function load(url) { + return new Promise(resolve => { + const img = new Image() + img.onload = img.onerror = () => resolve() + img.decoding = 'async' + img.src = url + }) +} + +/** Download the photos of these products (and categories) that this app run hasn't fetched yet. */ +export async function warmPhotos(...lists) { + if (typeof Image === 'undefined' || (typeof navigator !== 'undefined' && navigator.onLine === false)) return + const urls = [...new Set(lists.flat().map(x => x?.image_url || x?.image?.card).filter(Boolean).map(u => assetUrl(u)))] + .filter(u => !warmed.has(u)) + urls.forEach(u => warmed.add(u)) + const queue = [...urls] + const worker = async () => { while (queue.length) await load(queue.shift()) } + await Promise.all(Array.from({ length: Math.min(AT_ONCE, queue.length) }, worker)) +} diff --git a/waiter_pwa/vite.config.js b/waiter_pwa/vite.config.js index 020117a..b387eed 100644 --- a/waiter_pwa/vite.config.js +++ b/waiter_pwa/vite.config.js @@ -64,7 +64,17 @@ export default defineConfig(({ mode }) => { }, workbox: { globPatterns: ['**/*.{js,css,html,ico,png,svg}'], - runtimeCaching: [], + // The media library's files (plan step 5.2): an address never changes meaning + // (/api/media/file//), so the cached copy is always right: cache first. + runtimeCaching: [{ + urlPattern: ({ url }) => url.pathname.startsWith('/api/media/file/'), + handler: 'CacheFirst', + options: { + cacheName: 'xenia-media', + expiration: { maxEntries: 800, maxAgeSeconds: 365 * 24 * 3600 }, + cacheableResponse: { statuses: [0, 200] }, + }, + }], }, devOptions: { enabled: false,