fix: shop catalog always reflects latest data after admin changes
- Add force-dynamic to /api/catalog so Next.js never serves a stale cached route response to the shop - Add invalidateCatalogCache() to catalog-cache lib to drop the 30s in-process memory cache on demand - Call invalidateCatalogCache() after every admin PATCH/DELETE on an item so override saves are reflected on the very next shop request (no 30s delay) Refresh from Square already updated the shared disk + memory cache; force-dynamic ensures the shop route handler actually runs each time. Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
This commit is contained in:
parent
27093bcd54
commit
7bc84cea75
@ -1,5 +1,6 @@
|
||||
import { NextResponse } from 'next/server'
|
||||
import { setOverride, clearOverride } from '@/lib/overrides'
|
||||
import { invalidateCatalogCache } from '@/lib/catalog-cache'
|
||||
import type { ItemOverride } from '@/lib/overrides'
|
||||
|
||||
export async function PATCH(
|
||||
@ -9,6 +10,7 @@ export async function PATCH(
|
||||
try {
|
||||
const patch: Partial<ItemOverride> = await request.json()
|
||||
setOverride(params.id, patch)
|
||||
invalidateCatalogCache() // shop picks up override changes on next request
|
||||
return NextResponse.json({ ok: true })
|
||||
} catch (err) {
|
||||
console.error('[admin/items/patch] error:', err)
|
||||
@ -23,6 +25,7 @@ export async function DELETE(
|
||||
) {
|
||||
try {
|
||||
clearOverride(params.id)
|
||||
invalidateCatalogCache() // shop picks up reset on next request
|
||||
return NextResponse.json({ ok: true })
|
||||
} catch (err) {
|
||||
console.error('[admin/items/delete] error:', err)
|
||||
|
||||
@ -3,6 +3,8 @@ import { getCatalog } from '@/lib/catalog-cache'
|
||||
import { readOverrides } from '@/lib/overrides'
|
||||
import type { CatalogItem } from '@/data/mock-catalog'
|
||||
|
||||
export const dynamic = 'force-dynamic'
|
||||
|
||||
function applyOverrides(items: CatalogItem[]): CatalogItem[] {
|
||||
const overrides = readOverrides()
|
||||
|
||||
|
||||
@ -53,6 +53,12 @@ export async function refreshCatalog(): Promise<{ items: CatalogItem[]; fetchedA
|
||||
return { items, fetchedAt: new Date() }
|
||||
}
|
||||
|
||||
/** Drop the in-process memory cache so the next request re-reads from disk.
|
||||
* Call this after writing overrides so the shop picks up changes immediately. */
|
||||
export function invalidateCatalogCache(): void {
|
||||
memCache = null
|
||||
}
|
||||
|
||||
// ── Get (from cache or fetch) ─────────────────────────────────────────────────
|
||||
|
||||
export async function getCatalog(): Promise<{ items: CatalogItem[]; fetchedAt: Date | null }> {
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user