feat(waiter): keep the menu's photos for offline (platform-v2 step 5.2)
Right after the menu is read the phone downloads its photos (4 at a time, services/photoCache.js). The library's year-long immutable headers keep them in the browser cache; the installed PWA also caches /api/media/file/ cache- first (workbox `xenia-media`). With no connection the menu still shows them. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
@@ -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 */ }
|
||||
|
||||
@@ -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 }
|
||||
}
|
||||
|
||||
@@ -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/<hash>/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))
|
||||
}
|
||||
@@ -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/<hash>/<size>), 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,
|
||||
|
||||
Reference in New Issue
Block a user