Files
Andrew Beveridge ☄️andClaude Opus 5.5 ccf6da020f fix: tenant portal edge config stampede spawned cold API instances (v0.268.6) (#1141)
## Summary
This follows up #1140. While investigating the stall-alert flapping, I
found that 18 of the 28 requests that were the first on a new autoscaled
instance (each ≥10s) over 4 days came from the tenant portal's Pages
Function. Each one was `/api/tenant/config?tenant=…` with no user agent.

At 03:55 UTC, something loaded 17 `singa.nomadkaraoke.com` pages at
once. All 17 missed the 5-minute edge cache in the same second and
called the API. Cloud Run spawned 5 cold instances, and those HTML
responses waited 15–17s.

## Changes
New `frontend/lib/edge/tenantConfigCache.ts`, used by
`functions/[[path]].ts`:
- **Stale-while-revalidate:**
  - Under 5 minutes old, the cached config is served as is.
- Older than that, the stale copy is still served and refreshed in the
background via `waitUntil`. Copies are stored for 7 days.
- Entries cached before this deploy have no timestamp, so they are
treated as stale.
- **Bounded cold miss:** the backend fetch has a 3s
`AbortSignal.timeout`. On timeout or error the HTML is served without
injection, and `TenantProvider` falls back to fetching the config
client-side, as it already did.
- **Single flight:** concurrent misses in one isolate share one fetch.
- **Payload shape check:** a malformed 200 response isn't cached for a
week. This came from a CodeRabbit finding.

Other changes:
- `jest.setup.js` guards the `window.matchMedia` mock so
`@jest-environment node` tests can run. It's a no-op for jsdom tests.
- `docs/ARCHITECTURE.md` has a new section on the edge config cache.

**Trade-off:** a branding change now reaches each colo on the first page
view after 5 minutes, and that view still shows the old branding.
Before, it showed up on the first view after expiry.

## Testing
- 10 new Jest tests (node environment):
  - cold miss;
  - fresh hit;
  - stale hit with background refresh;
  - a 17-request burst makes one fetch;
  - 503 and timeout return null;
  - a failed refresh keeps the stale copy;
  - an entry without a timestamp is treated as stale;
  - a malformed payload isn't cached.
- `wrangler pages functions build` compiles the bundle.
- `make test`: all passed.
- Local CodeRabbit review: 1 finding, fixed.

@coderabbitai ignore

🤖 Generated with [Claude Code](https://claude.com/claude-code)

https://claude.ai/code/session_01DTfrTaaZQfXUUduAhZpLeq

Co-authored-by: Claude Opus 5.5 <noreply@anthropic.com>
2026-10-13 18:29:45 -04:00

103 lines
2.9 KiB
JavaScript

// Learn more: https://github.com/testing-library/jest-dom
import '@testing-library/jest-dom'
// Mock next-intl — loads actual English strings so tests see real text
const enMessages = require('./messages/en.json')
function getNestedValue(obj, dottedKey) {
return dottedKey.split('.').reduce((o, k) => (o && typeof o === 'object' ? o[k] : undefined), obj)
}
jest.mock('next-intl', () => ({
useTranslations: (namespace) => {
const messages = namespace
? getNestedValue(enMessages, namespace) || {}
: enMessages
const t = (key, params) => {
const value = getNestedValue(messages, key)
if (typeof value !== 'string') return key
if (params) {
return value.replace(/\{(\w+)(?:,\s*plural,\s*one\s*\{([^}]*)\}\s*other\s*\{([^}]*)\})?\}/g,
(match, name, one, other) => {
if (one !== undefined && other !== undefined) {
return params[name] === 1 ? one : other
}
return params[name] !== undefined ? String(params[name]) : match
})
}
return value
}
t.rich = (key, params) => t(key, params)
t.raw = (key) => getNestedValue(messages, key) || key
return t
},
useLocale: () => 'en',
useMessages: () => enMessages,
NextIntlClientProvider: ({ children }) => children,
}))
// Mock @/i18n/routing — locale-aware navigation primitives
jest.mock('@/i18n/routing', () => {
const React = require('react')
return {
routing: {
locales: [
'en', 'es', 'de', 'pt', 'fr', 'ja', 'ko', 'zh', 'it', 'nl',
'pl', 'tr', 'ru', 'th', 'id', 'vi', 'tl', 'hi', 'ar', 'sv',
'nb', 'da', 'fi', 'cs', 'ro', 'hu', 'el', 'he', 'ms', 'uk',
'hr', 'sk', 'ca',
],
defaultLocale: 'en',
},
Link: ({ children, href, ...props }) =>
React.createElement('a', { href, ...props }, children),
redirect: jest.fn(),
usePathname: () => '/',
useRouter: () => ({
push: jest.fn(),
replace: jest.fn(),
prefetch: jest.fn(),
back: jest.fn(),
forward: jest.fn(),
refresh: jest.fn(),
}),
}
})
// Mock window.matchMedia (skipped in `@jest-environment node` tests, e.g. edge code)
if (typeof window !== 'undefined') {
Object.defineProperty(window, 'matchMedia', {
writable: true,
value: jest.fn().mockImplementation(query => ({
matches: false,
media: query,
onchange: null,
addListener: jest.fn(),
removeListener: jest.fn(),
addEventListener: jest.fn(),
removeEventListener: jest.fn(),
dispatchEvent: jest.fn(),
})),
})
}
// Mock IntersectionObserver
global.IntersectionObserver = class IntersectionObserver {
constructor() {}
disconnect() {}
observe() {}
takeRecords() {
return []
}
unobserve() {}
}
// Mock ResizeObserver
global.ResizeObserver = class ResizeObserver {
constructor() {}
disconnect() {}
observe() {}
unobserve() {}
}