Skip to content

Beautiful Open-Source Form Builder

Fix SSR configuration cache to avoid redundant API calls (#1305)

Julien Nahumfd31605e402e
diff --git a/.github/workflows/ci-cd.yml b/.github/workflows/ci-cd.yml
index 2c164a8f6..c3978c140 100644
--- a/.github/workflows/ci-cd.yml
+++ b/.github/workflows/ci-cd.yml
@@ -11,6 +11,7 @@ on:
- "docker/**"
- "docker-compose*.yml"
- "scripts/docker-setup.sh"
+ - "scripts/e2e-*.sh"
- "scripts/setup-env.sh"
- "scripts/tests/setup-env-test.sh"
- ".github/workflows/**"
@@ -21,6 +22,7 @@ on:
- "docker/**"
- "docker-compose*.yml"
- "scripts/docker-setup.sh"
+ - "scripts/e2e-*.sh"
- "scripts/setup-env.sh"
- "scripts/tests/setup-env-test.sh"
- ".github/workflows/**"
@@ -101,6 +103,9 @@ jobs:
- name: Test integration email event rendering
run: npm run test:email-events
+ - name: Test SSR configuration cache
+ run: npm run test:ssr-cache
+
- name: Run front-end tests
run: npm run test -- --run
@@ -112,6 +117,9 @@ jobs:
env:
SENTRY_AUTH_TOKEN: ${{ secrets.SENTRY_AUTH_TOKEN }}
+ - name: Test built SSR configuration cache
+ run: npm run test:ssr-cache:built
+
- name: Upload Nuxt Output Artifact
if: ${{ !cancelled() }}
uses: actions/upload-artifact@v4
diff --git a/client/package.json b/client/package.json
index 26fbf4499..25c1a0ca6 100644
--- a/client/package.json
+++ b/client/package.json
@@ -10,6 +10,8 @@
"generate": "export NODE_TLS_REJECT_UNAUTHORIZED=0; nuxt generate",
"preview": "nuxt preview",
"postinstall": "nuxt prepare",
+ "test:ssr-cache:built": "node --test tests/ssr-config-cache-built.test.mjs",
+ "test:ssr-cache": "node --test tests/ssr-config-cache.test.mjs",
"test:email-events": "node --test tests/integration-email-status.test.mjs",
"lint": "eslint --ext .js,.vue .",
"lint:fix": "eslint . --fix --ext .js,.vue",
diff --git a/client/plugins/feature-flags.server.js b/client/plugins/feature-flags.server.js
index 6a1cb0027..8e737032f 100644
--- a/client/plugins/feature-flags.server.js
+++ b/client/plugins/feature-flags.server.js
@@ -1,11 +1,11 @@
import { contentApi } from '~/api/content'
export default defineNuxtPlugin(async (nuxtApp) => {
- // Load feature flags during SSR using cached server route
+ // Reuse the Nitro cache across SSR requests, without forwarding user headers.
const featureFlagsState = useState('featureFlags', () => ({}))
try {
- const flags = await contentApi.featureFlags.list({ server: true })
+ const flags = await $fetch('/api/feature-flags', { retry: 0 })
featureFlagsState.value = flags
} catch (error) {
console.error('Failed to load feature flags on server:', error)
diff --git a/client/plugins/plan-catalog.server.js b/client/plugins/plan-catalog.server.js
index 9bf9547e1..2f4dba09b 100644
--- a/client/plugins/plan-catalog.server.js
+++ b/client/plugins/plan-catalog.server.js
@@ -4,7 +4,7 @@ export default defineNuxtPlugin(async (nuxtApp) => {
const planCatalogState = useState('planCatalog', () => ({ tiers: {} }))
try {
- const plans = await contentApi.plans.list({ server: true })
+ const plans = await $fetch('/api/plan-catalog', { retry: 0 })
if (plans?.tiers) {
planCatalogState.value = plans
}
diff --git a/client/server/api/feature-flags.get.js b/client/server/api/feature-flags.get.js
index 1f108f3f2..42e6cd901 100644
--- a/client/server/api/feature-flags.get.js
+++ b/client/server/api/feature-flags.get.js
@@ -1,33 +1,24 @@
-// Cached server API route for feature flags
-export default defineCachedEventHandler(async () => {
+// Global installation configuration: never forward cookies, tokens or custom domains.
+export default defineCachedEventHandler(() => {
const config = useRuntimeConfig()
-
- // Call Laravel API directly with proper headers
- const apiBase = config.privateApiBase || config.public.apiBase
-
- try {
- const response = await $fetch('/content/feature-flags', {
- baseURL: apiBase,
- headers: {
- 'accept': 'application/json',
- ...(config.apiSecret && { 'x-api-secret': config.apiSecret })
- }
- })
-
- return response
- } catch (error) {
- console.error('Failed to fetch feature flags from Laravel API:', error)
- // Return empty object as fallback to prevent breaking the app
- return {}
- }
+
+ return $fetch('/content/feature-flags', {
+ baseURL: config.privateApiBase || config.public.apiBase,
+ retry: 0,
+ headers: {
+ accept: 'application/json',
+ ...(config.apiSecret && { 'x-api-secret': config.apiSecret })
+ }
+ }).then((flags) => {
+ if (!flags || typeof flags.self_hosted !== 'boolean') {
+ throw createError({ statusCode: 502, statusMessage: 'Invalid feature flags response' })
+ }
+ return flags
+ })
}, {
- maxAge: 10 * 60, // 10 minutes cache
+ maxAge: 10 * 60,
name: 'feature-flags',
- getKey: (event) => {
- // Include timestamp in cache key for cache-busting when refreshing
- const url = new URL(event.node.req.url || '', 'http://localhost')
- const timestamp = url.searchParams.get('t')
- return timestamp ? `global:${timestamp}` : 'global'
- },
- swr: true // Serve stale while revalidating in background
-})
\ No newline at end of file
+ // Arbitrary public query parameters must not create a new upstream fetch.
+ getKey: () => 'global',
+ swr: true
+})
diff --git a/client/server/api/plan-catalog.get.js b/client/server/api/plan-catalog.get.js
new file mode 100644
index 000000000..c98be5686
--- /dev/null
+++ b/client/server/api/plan-catalog.get.js
@@ -0,0 +1,23 @@
+// The plan catalog is public and shared by every request to this installation.
+export default defineCachedEventHandler(() => {
+ const config = useRuntimeConfig()
+
+ return $fetch('/content/plans', {
+ baseURL: config.privateApiBase || config.public.apiBase,
+ retry: 0,
+ headers: {
+ accept: 'application/json',
+ ...(config.apiSecret && { 'x-api-secret': config.apiSecret })
+ }
+ }).then((plans) => {
+ if (!plans?.tiers || typeof plans.tiers !== 'object' || Array.isArray(plans.tiers)) {
+ throw createError({ statusCode: 502, statusMessage: 'Invalid plan catalog response' })
+ }
+ return plans
+ })
+}, {
+ maxAge: 10 * 60,
+ name: 'plan-catalog',
+ getKey: () => 'global',
+ swr: true
+})
diff --git a/client/tests/ssr-config-cache-built.test.mjs b/client/tests/ssr-config-cache-built.test.mjs
new file mode 100644
index 000000000..3a12ba05a
--- /dev/null
+++ b/client/tests/ssr-config-cache-built.test.mjs
@@ -0,0 +1,64 @@
+import { test } from 'node:test'
+import assert from 'node:assert/strict'
+import { createServer } from 'node:http'
+import { spawn } from 'node:child_process'
+import { access } from 'node:fs/promises'
+
+const client = new URL('../', import.meta.url)
+const listen = (server) => new Promise((resolve) => server.listen(0, '127.0.0.1', () => resolve(server.address().port)))
+const close = (server) => new Promise((resolve) => server.close(resolve))
+
+test('built Nuxt pages and 404s reuse configuration across real SSR requests', { timeout: 30000 }, async () => {
+ await access(new URL('.output/server/index.mjs', client))
+ const calls = new Map()
+ const backend = createServer((req, res) => {
+ calls.set(req.url, (calls.get(req.url) || 0) + 1)
+ res.setHeader('content-type', 'application/json')
+ res.end(JSON.stringify(req.url === '/content/feature-flags'
+ ? { self_hosted: false, setup_required: false, services: {}, billing: { enabled: false }, license: null }
+ : req.url === '/content/plans' ? { tiers: { free: { name: 'Free' } } } : { data: [] }))
+ })
+ let child
+ let output = ''
+ try {
+ const backendPort = await listen(backend)
+ const reservation = createServer()
+ const port = await listen(reservation)
+ await close(reservation)
+ child = spawn(process.execPath, ['.output/server/index.mjs'], {
+ cwd: client,
+ env: {
+ PATH: process.env.PATH,
+ NODE_ENV: 'production', NITRO_HOST: '127.0.0.1', NITRO_PORT: String(port),
+ NUXT_PUBLIC_API_BASE: `http://127.0.0.1:${backendPort}`,
+ NUXT_PRIVATE_API_BASE: `http://127.0.0.1:${backendPort}`,
+ NUXT_PUBLIC_APP_URL: `http://127.0.0.1:${port}`,
+ NUXT_PUBLIC_ENV: 'testing', NUXT_API_SECRET: 'local-test-only'
+ }
+ })
+ child.stdout.on('data', (data) => { output += data })
+ child.stderr.on('data', (data) => { output += data })
+ let started = false
+ for (let i = 0; i < 100; i++) {
+ if (output.includes('Listening on')) { started = true; break }
+ if (child.exitCode !== null) assert.fail(`Nuxt exited with ${child.exitCode}: ${output}`)
+ await new Promise((resolve) => setTimeout(resolve, 100))
+ }
+ assert.ok(started, `Nuxt did not start: ${output}`)
+ for (const path of ['/', '/', '/cache-test-missing-page', '/cache-test-missing-page', '/']) {
+ const response = await fetch(`http://127.0.0.1:${port}${path}`, { signal: AbortSignal.timeout(5000) })
+ const html = await response.text()
+ assert.equal(response.status, path === '/' ? 200 : 404)
+ assert.ok(html.includes('featureFlags') && html.includes('planCatalog'), 'SSR includes hydrated configuration')
+ }
+ assert.equal(calls.get('/content/feature-flags'), 1)
+ assert.equal(calls.get('/content/plans'), 1)
+ } finally {
+ if (child && child.exitCode === null) {
+ const exited = new Promise((resolve) => child.once('exit', resolve))
+ child.kill('SIGTERM')
+ await exited
+ }
+ await close(backend)
+ }
+})
diff --git a/client/tests/ssr-config-cache.test.mjs b/client/tests/ssr-config-cache.test.mjs
new file mode 100644
index 000000000..1138bc5b0
--- /dev/null
+++ b/client/tests/ssr-config-cache.test.mjs
@@ -0,0 +1,172 @@
+import { test } from 'node:test'
+import assert from 'node:assert/strict'
+import { createServer } from 'node:http'
+import { mkdtemp, rm, readFile, writeFile } from 'node:fs/promises'
+import { fileURLToPath, pathToFileURL } from 'node:url'
+import { join } from 'node:path'
+import { createNitro, build, prepare } from 'nitropack'
+import { ofetch } from 'ofetch'
+
+const client = fileURLToPath(new URL('../', import.meta.url))
+const listen = (server) => new Promise((resolve) => server.listen(0, '127.0.0.1', () => resolve(`http://127.0.0.1:${server.address().port}`)))
+const close = (server) => new Promise((resolve) => server.close(resolve))
+const endpoints = [
+ { name: 'feature-flags', path: '/content/feature-flags', state: 'featureFlags', refresh: 'refreshFeatureFlags', data: { self_hosted: false, services: {}, version: 'first' } },
+ { name: 'plan-catalog', path: '/content/plans', state: 'planCatalog', refresh: 'refreshPlanCatalog', data: { tiers: { pro: { name: 'Pro' } }, version: 'first' } }
+]
+
+async function eventually(check) {
+ for (let attempt = 0; attempt < 100; attempt++) {
+ if (await check()) return
+ await new Promise((resolve) => setTimeout(resolve, 10))
+ }
+ assert.fail('Background cache refresh did not complete')
+}
+
+test('SSR configuration uses the real Nitro cache across requests', { timeout: 60000 }, async (t) => {
+ const directory = await mkdtemp(join(client, '.ssr-cache-test-'))
+ const calls = []
+ const upstream = new Map()
+ const states = new Map()
+ const plugins = new Map()
+ const originalNow = Date.now
+ const globalKeys = ['defineNuxtPlugin', 'useState', '$fetch', '__testContentApi']
+ const originalGlobals = new Map(globalKeys.map((key) => [key, Object.getOwnPropertyDescriptor(globalThis, key)]))
+ const backend = createServer((req, res) => {
+ calls.push({ path: req.url, headers: req.headers })
+ const result = upstream.get(req.url)
+ res.writeHead(result?.status || 404, { 'content-type': 'application/json' })
+ res.end(JSON.stringify(result?.data || {}))
+ })
+ let server
+ let nitro
+ try {
+ const apiBase = await listen(backend)
+ const resetHandler = join(directory, 'reset.js')
+ await writeFile(resetHandler, "export default defineEventHandler(async () => { await useStorage('cache').clear(); return {} })")
+ nitro = await createNitro({
+ rootDir: directory, preset: 'node-listener', logLevel: 0, compatibilityDate: '2024-10-30',
+ runtimeConfig: { privateApiBase: apiBase, public: { apiBase: 'http://unused.invalid' }, apiSecret: 'test-ssr-secret' },
+ handlers: [
+ ...endpoints.map(({ name }) => ({ route: `/api/${name}`, handler: join(client, 'server/api', `${name}.get.js`) })),
+ { route: '/__test/reset', handler: resetHandler }
+ ],
+ storage: { cache: { driver: 'memory' } },
+ externals: { inline: [/./] }
+ })
+ await prepare(nitro)
+ await build(nitro)
+ const { listener } = await import(pathToFileURL(join(directory, '.output/server/index.mjs')).href)
+ server = createServer(listener)
+ const origin = await listen(server)
+ const read = (name, options = {}) => ofetch(`/api/${name}`, { baseURL: origin, retry: 0, ...options })
+ globalThis.defineNuxtPlugin = (plugin) => plugin
+ globalThis.useState = (key, init) => {
+ if (!states.has(key)) states.set(key, { value: init() })
+ return states.get(key)
+ }
+ globalThis.$fetch = (path, options) => ofetch(path, { baseURL: origin, ...options })
+ // Bootstrap must use Nitro; explicitly requested refreshes use this direct API.
+ globalThis.__testContentApi = {
+ featureFlags: { list: (options) => ofetch('/content/feature-flags', { baseURL: apiBase, ...options }) },
+ plans: { list: (options) => ofetch('/content/plans', { baseURL: apiBase, ...options }) }
+ }
+ for (const { name } of endpoints) {
+ for (const side of ['server', 'client']) {
+ const source = await readFile(join(client, 'plugins', `${name}.${side}.js`), 'utf8')
+ const filename = join(directory, `${name}.${side}.mjs`)
+ await writeFile(filename, source.replace("import { contentApi } from '~/api/content'", 'const contentApi = globalThis.__testContentApi'))
+ plugins.set(`${name}.${side}`, (await import(pathToFileURL(filename).href)).default)
+ }
+ }
+ t.beforeEach(async () => {
+ Date.now = originalNow
+ await fetch(`${origin}/__test/reset`)
+ calls.length = 0
+ states.clear()
+ for (const { path, data } of endpoints) upstream.set(path, { status: 200, data })
+ })
+
+ for (const endpoint of endpoints) {
+ const { name, path, data, state, refresh } = endpoint
+ await t.test(`${name}: concurrent requests and changing query/visitor headers share one fetch`, async () => {
+ const results = await Promise.all(Array.from({ length: 40 }, (_, i) => read(name, {
+ query: { t: i }, headers: { authorization: 'Bearer visitor-token', cookie: `visitor=${i}`, 'x-custom-domain': 'visitor.example', 'x-api-secret': 'visitor-secret' }
+ })))
+ for (const result of results) assert.deepEqual(result, data)
+ assert.equal(calls.length, 1)
+ assert.equal(calls[0].path, path)
+ assert.equal(calls[0].headers.authorization, undefined)
+ assert.equal(calls[0].headers.cookie, undefined)
+ assert.equal(calls[0].headers['x-custom-domain'], undefined)
+ assert.equal(calls[0].headers['x-api-secret'], 'test-ssr-secret')
+ })
+
+ await t.test(`${name}: cold 429 is not retried by either SSR or upstream fetch`, async () => {
+ upstream.set(path, { status: 429, data: {} })
+ await plugins.get(`${name}.server`)({ provide() {} })
+ assert.equal(calls.length, 1)
+ assert.deepEqual(states.get(state).value, name === 'feature-flags' ? {} : { tiers: {} })
+ upstream.set(path, { status: 200, data })
+ assert.deepEqual(await read(name), data)
+ assert.equal(calls.length, 2)
+ })
+
+ await t.test(`${name}: invalid successful responses do not poison the cache`, async () => {
+ upstream.set(path, { status: 200, data: {} })
+ await assert.rejects(read(name), (error) => error.statusCode === 502)
+ upstream.set(path, { status: 200, data })
+ assert.deepEqual(await read(name), data)
+ assert.equal(calls.length, 2)
+ })
+
+ await t.test(`${name}: repeated SSR, hydration and explicit refresh keep their semantics`, async () => {
+ const provided = {}
+ const app = { provide: (key, value) => { provided[key] = value } }
+ for (let i = 0; i < 10; i++) {
+ states.clear()
+ await plugins.get(`${name}.server`)(app)
+ assert.deepEqual(states.get(state).value, data)
+ }
+ assert.equal(calls.length, 1)
+ await plugins.get(`${name}.client`)(app)
+ assert.equal(calls.length, 1, 'client hydration must not refetch')
+ // Preserve the explicit cache-busting query sent by the client refresh.
+ const fresh = { ...data, version: 'changed' }
+ const now = originalNow()
+ Date.now = () => now
+ upstream.set(`${path}?t=${now}`, { status: 200, data: fresh })
+ await provided[refresh]()
+ assert.deepEqual(states.get(state).value, fresh)
+ assert.equal(calls.length, 2)
+ assert.ok(calls[1].path.startsWith(`${path}?t=`))
+ assert.deepEqual(await read(name), data, 'manual refresh must not contaminate the shared SSR cache')
+ })
+
+ await t.test(`${name}: failed expired refresh keeps good data, then a recovery updates it`, async () => {
+ assert.deepEqual(await read(name), data)
+ Date.now = () => originalNow() + 601000
+ upstream.set(path, { status: 503, data: {} })
+ assert.deepEqual(await read(name), data)
+ await eventually(() => calls.length === 2)
+ const fresh = { ...data, version: 'recovered' }
+ upstream.set(path, { status: 200, data: fresh })
+ await eventually(async () => (await read(name)).version === 'recovered')
+ assert.equal(calls.length, 3)
+ for (let i = 0; i < 5; i++) assert.deepEqual(await read(name), fresh)
+ assert.equal(calls.length, 3)
+ })
+ }
+ } finally {
+ Date.now = originalNow
+ for (const key of globalKeys) {
+ const descriptor = originalGlobals.get(key)
+ if (descriptor) Object.defineProperty(globalThis, key, descriptor)
+ else delete globalThis[key]
+ }
+ if (server) await close(server)
+ await close(backend)
+ await nitro?.close()
+ await rm(directory, { recursive: true, force: true })
+ }
+})
diff --git a/scripts/e2e-reset.sh b/scripts/e2e-reset.sh
index 193d565ba..c16676650 100755
--- a/scripts/e2e-reset.sh
+++ b/scripts/e2e-reset.sh
@@ -6,9 +6,11 @@ ROOT_DIR=$(CDPATH= cd -- "$(dirname "$0")/.." && pwd)
COMPOSE_FILES="-f $ROOT_DIR/docker-compose.e2e.yml"
docker compose $COMPOSE_FILES exec -T api sh -lc 'rm -rf storage/framework/cache/data/* storage/framework/sessions/*'
-docker compose $COMPOSE_FILES exec -T api php artisan optimize:clear
-docker compose $COMPOSE_FILES exec -T api php artisan cache:clear
-docker compose $COMPOSE_FILES exec -T api sh -lc 'mkdir -p storage/framework/cache/data storage/framework/sessions && chmod -R a+rwX storage/framework/cache storage/framework/sessions'
-docker compose $COMPOSE_FILES exec -T api php artisan migrate:fresh --seed --seeder=Database\\Seeders\\E2ETestSeeder --force
+# The entrypoint and Composer can create files as root. Restore ownership, then
+# run Artisan as the PHP-FPM user so seeded cache directories stay writable by API requests.
+docker compose $COMPOSE_FILES exec -T api sh -lc 'mkdir -p storage/framework/cache/data storage/framework/sessions bootstrap/cache && chown -R www-data:www-data storage bootstrap/cache && chmod -R u+rwX storage bootstrap/cache'
+docker compose $COMPOSE_FILES exec -T --user www-data api php artisan optimize:clear
+docker compose $COMPOSE_FILES exec -T --user www-data api php artisan cache:clear
+docker compose $COMPOSE_FILES exec -T --user www-data api php artisan migrate:fresh --seed --seeder=Database\\Seeders\\E2ETestSeeder --force
echo "E2E database reset complete."