Skip to content

Beautiful Open-Source Form Builder

Avoid caching setup-required feature flags in Nuxt (#1360)

Julien Nahum9765ce524eb9
diff --git a/client/server/api/feature-flags.get.js b/client/server/api/feature-flags.get.js
index 42e6cd901..643f055a4 100644
--- a/client/server/api/feature-flags.get.js
+++ b/client/server/api/feature-flags.get.js
@@ -1,5 +1,5 @@
// Global installation configuration: never forward cookies, tokens or custom domains.
-export default defineCachedEventHandler(() => {
+const getFeatureFlags = defineCachedFunction(() => {
const config = useRuntimeConfig()
return $fetch('/content/feature-flags', {
@@ -20,5 +20,14 @@ export default defineCachedEventHandler(() => {
name: 'feature-flags',
// Arbitrary public query parameters must not create a new upstream fetch.
getKey: () => 'global',
+ // Registration clears Laravel's cache, but cannot invalidate this Nitro cache.
+ // Keep checking the API until setup finishes, then cache normally.
+ validate: (entry) => entry.value !== undefined && entry.value.setup_required !== true,
swr: true
})
+
+export default defineEventHandler((event) => {
+ // Browsers and reverse proxies must not retain the pre-registration flag.
+ setHeader(event, 'cache-control', 'no-store')
+ return getFeatureFlags()
+})
diff --git a/client/tests/ssr-config-cache-built.test.mjs b/client/tests/ssr-config-cache-built.test.mjs
index 3a12ba05a..eaac863fe 100644
--- a/client/tests/ssr-config-cache-built.test.mjs
+++ b/client/tests/ssr-config-cache-built.test.mjs
@@ -8,14 +8,15 @@ 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 () => {
+test('built Nuxt exits setup immediately, then reuses configuration across SSR pages and 404s', { timeout: 30000 }, async () => {
await access(new URL('.output/server/index.mjs', client))
const calls = new Map()
+ let setupRequired = true
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 }
+ ? { self_hosted: true, setup_required: setupRequired, services: {}, billing: { enabled: false }, license: null }
: req.url === '/content/plans' ? { tiers: { free: { name: 'Free' } } } : { data: [] }))
})
let child
@@ -45,13 +46,24 @@ test('built Nuxt pages and 404s reuse configuration across real SSR requests', {
await new Promise((resolve) => setTimeout(resolve, 100))
}
assert.ok(started, `Nuxt did not start: ${output}`)
+ const origin = `http://127.0.0.1:${port}`
+ const pending = await fetch(`${origin}/cache-test-missing-page`, { redirect: 'manual' })
+ assert.equal(pending.status, 302)
+ assert.equal(pending.headers.get('location'), '/setup')
+ const setup = await fetch(`${origin}/setup`)
+ assert.equal(setup.status, 200)
+ await setup.text()
+ assert.equal(calls.get('/content/feature-flags'), 2)
+
+ // Model registration completing in Laravel while the same Nuxt process runs.
+ setupRequired = false
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/feature-flags'), 3)
assert.equal(calls.get('/content/plans'), 1)
} finally {
if (child && child.exitCode === null) {
diff --git a/client/tests/ssr-config-cache.test.mjs b/client/tests/ssr-config-cache.test.mjs
index 1138bc5b0..d03e4cf87 100644
--- a/client/tests/ssr-config-cache.test.mjs
+++ b/client/tests/ssr-config-cache.test.mjs
@@ -87,6 +87,41 @@ test('SSR configuration uses the real Nitro cache across requests', { timeout: 6
for (const { path, data } of endpoints) upstream.set(path, { status: 200, data })
})
+ await t.test('feature-flags: setup completion is visible on the next SSR request without restarting', async () => {
+ const path = '/content/feature-flags'
+ const pending = { self_hosted: true, setup_required: true }
+ const complete = { ...pending, setup_required: false }
+ const render = async () => {
+ states.clear()
+ await plugins.get('feature-flags.server')({ provide() {} })
+ return states.get('featureFlags').value
+ }
+
+ upstream.set(path, { status: 200, data: pending })
+ assert.deepEqual(await render(), pending)
+ assert.deepEqual(await render(), pending)
+ assert.equal(calls.length, 2, 'setup-required responses must not enter the shared cache')
+
+ // Registration clears the Laravel cache; the next page load must see it.
+ upstream.set(path, { status: 200, data: complete })
+ assert.deepEqual(await render(), complete)
+ assert.equal(calls.length, 3)
+ for (let i = 0; i < 5; i++) assert.deepEqual(await render(), complete)
+ assert.equal(calls.length, 3, 'completed installations still reuse the shared cache')
+ })
+
+ await t.test('feature-flags: HTTP clients cannot cache the setup redirect flag', async () => {
+ for (const setupRequired of [true, false]) {
+ upstream.set('/content/feature-flags', {
+ status: 200, data: { self_hosted: true, setup_required: setupRequired }
+ })
+ const response = await fetch(`${origin}/api/feature-flags`)
+ assert.equal(response.status, 200)
+ assert.equal(response.headers.get('cache-control'), 'no-store')
+ assert.equal((await response.json()).setup_required, setupRequired)
+ }
+ })
+
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 () => {
diff --git a/docs/deployment/docker.mdx b/docs/deployment/docker.mdx
index 0d24972d5..8db3c53b9 100644
--- a/docs/deployment/docker.mdx
+++ b/docs/deployment/docker.mdx
@@ -111,6 +111,11 @@ changing it.
After deployment, OpnForm will automatically redirect you to a setup page where you can create your admin account. Simply visit `http://localhost` and you'll be guided through the setup process.
+After you create the admin account, setup is complete. Reloading the dashboard
+should keep you in the application without restarting any containers. While
+setup is pending, the frontend checks the API's setup status on each page load;
+after completion, normal server-side feature-flag caching resumes.
+
<Note>
Public registration is disabled in the self-hosted version after setup is
complete. Use the admin account to invite additional users. Free