Skip to content

Beautiful, AI-native markdown IDE and LLM wiki

PRD-9086 Stop browsers disk-caching OK API responses (#5816)

Andrew Mikofalvy6318cbf86484
diff --git a/.changeset/prd-9086-api-no-store.md b/.changeset/prd-9086-api-no-store.md
new file mode 100644
index 000000000..c9b80e6fe
--- /dev/null
+++ b/.changeset/prd-9086-api-no-store.md
@@ -0,0 +1,5 @@
+---
+"@inkeep/open-knowledge": patch
+---
+
+Stop the app and browser tabs from rewriting the file list into their disk cache on every refresh, which could add up to hundreds of gigabytes of disk writes in busy projects.
diff --git a/knip.config.ts b/knip.config.ts
index d3638888e..65f0e8807 100644
--- a/knip.config.ts
+++ b/knip.config.ts
@@ -147,15 +147,11 @@ export default {
'packages/server': {
entry: ['src/**/*.test.ts', 'src/parse-worker.ts'],
project: 'src/**',
- ignoreDependencies: [
- '@types/shell-quote',
- ],
+ ignoreDependencies: ['@types/shell-quote'],
},
'packages/cli': {
entry: ['src/**/*.test.ts', 'scripts/*.ts', 'tests/**/*.ts', 'src/parse-worker.ts'],
- ignoreDependencies: [
- 'yjs',
- ],
+ ignoreDependencies: ['yjs'],
},
'packages/desktop': {
entry: [
diff --git a/packages/app/tests/integration/api-error-envelope/document-list.test.ts b/packages/app/tests/integration/api-error-envelope/document-list.test.ts
index c8beee994..8051cd0cb 100644
--- a/packages/app/tests/integration/api-error-envelope/document-list.test.ts
+++ b/packages/app/tests/integration/api-error-envelope/document-list.test.ts
@@ -28,6 +28,32 @@ describe('document-list envelope (RFC 9457)', () => {
expect((body as Record<string, unknown>).ok).toBeUndefined();
});
+ test('every listing shape is no-store, so a browser never writes it to its disk cache', async () => {
+ const base = `http://127.0.0.1:${server.port}/api/documents`;
+ const listing = await fetch(base);
+ expect(listing.status).toBe(200);
+ expect(listing.headers.get('cache-control')).toBe('no-store');
+ await listing.body?.cancel();
+
+ const showAll = await fetch(`${base}?showAll=true&dir=&depth=1`);
+ expect(showAll.status).toBe(200);
+ expect(showAll.headers.get('cache-control')).toBe('no-store');
+ await showAll.body?.cancel();
+
+ const streamed = await fetch(`${base}?showAll=true&dir=&depth=1`, {
+ headers: { Accept: 'application/x-ndjson' },
+ });
+ expect(streamed.status).toBe(200);
+ expect(streamed.headers.get('content-type')).toBe('application/x-ndjson');
+ expect(streamed.headers.get('cache-control')).toBe('no-store');
+ await streamed.body?.cancel();
+
+ const pages = await fetch(`http://127.0.0.1:${server.port}/api/pages`);
+ expect(pages.status).toBe(200);
+ expect(pages.headers.get('cache-control')).toBe('no-store');
+ await pages.body?.cancel();
+ });
+
test('directory traversal attempt emits urn:ok:error:invalid-request', async () => {
const res = await fetch(
`http://127.0.0.1:${server.port}/api/documents?dir=${encodeURIComponent('../etc')}`,
diff --git a/packages/app/tests/integration/bug3-source-mode-writeback.test.ts b/packages/app/tests/integration/bug3-source-mode-writeback.test.ts
index 4549c74b3..a50b00123 100644
--- a/packages/app/tests/integration/bug3-source-mode-writeback.test.ts
+++ b/packages/app/tests/integration/bug3-source-mode-writeback.test.ts
@@ -1,4 +1,3 @@
-
import { readFileSync } from 'node:fs';
import { join } from 'node:path';
import { setTimeout as wait } from 'node:timers/promises';
diff --git a/packages/app/tests/integration/indented-jsx-byte-sacred.test.ts b/packages/app/tests/integration/indented-jsx-byte-sacred.test.ts
index 4fc2c11f9..6fcdc23f2 100644
--- a/packages/app/tests/integration/indented-jsx-byte-sacred.test.ts
+++ b/packages/app/tests/integration/indented-jsx-byte-sacred.test.ts
@@ -1,4 +1,3 @@
-
import { setTimeout as wait } from 'node:timers/promises';
import { normalizeBridge } from '@inkeep/open-knowledge-core';
import { updateYFragment } from '@tiptap/y-tiptap';
diff --git a/packages/app/tests/integration/interior-edit-freshness.test.ts b/packages/app/tests/integration/interior-edit-freshness.test.ts
index 9f2f6feb6..40b5cdfd6 100644
--- a/packages/app/tests/integration/interior-edit-freshness.test.ts
+++ b/packages/app/tests/integration/interior-edit-freshness.test.ts
@@ -1,4 +1,3 @@
-
import { setTimeout as wait } from 'node:timers/promises';
import { updateYFragment, yXmlFragmentToProseMirrorRootNode } from '@tiptap/y-tiptap';
import { afterAll, beforeAll, describe, expect, test } from 'vitest';
diff --git a/packages/app/tests/integration/keystroke-cadence-danger-space.test.ts b/packages/app/tests/integration/keystroke-cadence-danger-space.test.ts
index 2a085742b..93666af70 100644
--- a/packages/app/tests/integration/keystroke-cadence-danger-space.test.ts
+++ b/packages/app/tests/integration/keystroke-cadence-danger-space.test.ts
@@ -1,4 +1,3 @@
-
import { setTimeout as wait } from 'node:timers/promises';
import { updateYFragment, yXmlFragmentToProseMirrorRootNode } from '@tiptap/y-tiptap';
import { afterAll, beforeAll, describe, expect, test } from 'vitest';
diff --git a/packages/app/tests/integration/producer-guard-wiring.test.ts b/packages/app/tests/integration/producer-guard-wiring.test.ts
index 878d5196b..867598d34 100644
--- a/packages/app/tests/integration/producer-guard-wiring.test.ts
+++ b/packages/app/tests/integration/producer-guard-wiring.test.ts
@@ -1,4 +1,3 @@
-
import { setTimeout as wait } from 'node:timers/promises';
import { updateYFragment, yXmlFragmentToProseMirrorRootNode } from '@tiptap/y-tiptap';
import { afterAll, beforeAll, describe, expect, test } from 'vitest';
diff --git a/packages/app/tests/integration/qa-039-reconciliation-counters-http.test.ts b/packages/app/tests/integration/qa-039-reconciliation-counters-http.test.ts
index 4ada3cf26..0970ac12f 100644
--- a/packages/app/tests/integration/qa-039-reconciliation-counters-http.test.ts
+++ b/packages/app/tests/integration/qa-039-reconciliation-counters-http.test.ts
@@ -1,4 +1,3 @@
-
import { randomUUID } from 'node:crypto';
import { afterEach, describe, expect, test } from 'vitest';
import { agentWriteMd, createTestServer, type TestServer } from './test-harness';
diff --git a/packages/app/tests/integration/qa-045-restore-after-gc.test.ts b/packages/app/tests/integration/qa-045-restore-after-gc.test.ts
index cbe2b705f..b632185e1 100644
--- a/packages/app/tests/integration/qa-045-restore-after-gc.test.ts
+++ b/packages/app/tests/integration/qa-045-restore-after-gc.test.ts
@@ -1,4 +1,3 @@
-
import { execFileSync } from 'node:child_process';
import { randomUUID } from 'node:crypto';
import { join } from 'node:path';
diff --git a/packages/app/tests/integration/qa-050-rollback-undo-shipped-path.test.ts b/packages/app/tests/integration/qa-050-rollback-undo-shipped-path.test.ts
index 89bc9b4d9..c530ce4ba 100644
--- a/packages/app/tests/integration/qa-050-rollback-undo-shipped-path.test.ts
+++ b/packages/app/tests/integration/qa-050-rollback-undo-shipped-path.test.ts
@@ -1,4 +1,3 @@
-
import { randomUUID } from 'node:crypto';
import { afterAll, afterEach, beforeAll, describe, expect, test } from 'vitest';
import type * as Y from 'yjs';
diff --git a/packages/app/tests/integration/undo-after-rollback.test.ts b/packages/app/tests/integration/undo-after-rollback.test.ts
index a78aeb0e4..d3767adc9 100644
--- a/packages/app/tests/integration/undo-after-rollback.test.ts
+++ b/packages/app/tests/integration/undo-after-rollback.test.ts
@@ -1,4 +1,3 @@
-
import { ROLLBACK_ORIGIN } from '@inkeep/open-knowledge-server';
import { afterAll, beforeAll, describe, expect, test } from 'vitest';
import * as Y from 'yjs';
diff --git a/packages/app/tests/integration/undo-recycle-reset.test.ts b/packages/app/tests/integration/undo-recycle-reset.test.ts
index e7cc69c3f..b076f955c 100644
--- a/packages/app/tests/integration/undo-recycle-reset.test.ts
+++ b/packages/app/tests/integration/undo-recycle-reset.test.ts
@@ -1,4 +1,3 @@
-
import { writeFileSync } from 'node:fs';
import { join } from 'node:path';
import { setTimeout as wait } from 'node:timers/promises';
diff --git a/packages/app/tests/integration/undo-rederive-survival.test.ts b/packages/app/tests/integration/undo-rederive-survival.test.ts
index 9ddcd0730..7935e35f8 100644
--- a/packages/app/tests/integration/undo-rederive-survival.test.ts
+++ b/packages/app/tests/integration/undo-rederive-survival.test.ts
@@ -1,4 +1,3 @@
-
import { prependFrontmatter } from '@inkeep/open-knowledge-core';
import { Plugin, PluginKey } from '@tiptap/pm/state';
import { updateYFragment, yXmlFragmentToProseMirrorRootNode } from '@tiptap/y-tiptap';
diff --git a/packages/core/scripts/dump-schema.ts b/packages/core/scripts/dump-schema.ts
index 8f9853666..9641bba6f 100644
--- a/packages/core/scripts/dump-schema.ts
+++ b/packages/core/scripts/dump-schema.ts
@@ -42,7 +42,7 @@ import { ACTIVE_MDAST_PLUGINS } from '../src/markdown/pipeline.ts';
const SCRIPT_URL = import.meta.url;
const SCRIPT_PATH = fileURLToPath(SCRIPT_URL);
-const DEFAULT_OUT = new URL('../schema-snapshot.json', SCRIPT_URL).pathname;
+const DEFAULT_OUT = fileURLToPath(new URL('../schema-snapshot.json', SCRIPT_URL));
interface AttrSnapshot {
hasDefault: boolean;
diff --git a/packages/core/scripts/regen-schema-snapshot.ts b/packages/core/scripts/regen-schema-snapshot.ts
index 4ec829d90..0aa90f890 100644
--- a/packages/core/scripts/regen-schema-snapshot.ts
+++ b/packages/core/scripts/regen-schema-snapshot.ts
@@ -13,6 +13,7 @@
* mark coverage from the snapshot the next time someone regenerates.
*/
import { writeFileSync } from 'node:fs';
+import { fileURLToPath } from 'node:url';
import { getSchema } from '@tiptap/core';
import { sharedExtensions } from '../src/extensions/shared.ts';
@@ -82,6 +83,6 @@ const extensionOrder = sharedExtensions.map((ext) => {
});
const snap: SchemaSnapshot = { nodes, marks, extensionOrder };
-const out = new URL('../src/schema-snapshot.json', import.meta.url).pathname;
+const out = fileURLToPath(new URL('../src/schema-snapshot.json', import.meta.url));
writeFileSync(out, `${JSON.stringify(snap, null, 2)}\n`);
console.log(`Wrote ${out}`);
diff --git a/packages/core/src/bridge/compose.ts b/packages/core/src/bridge/compose.ts
index 2a597ec3d..f4cc8a286 100644
--- a/packages/core/src/bridge/compose.ts
+++ b/packages/core/src/bridge/compose.ts
@@ -1,4 +1,3 @@
-
import { prependFrontmatter, stripFrontmatter } from '../extensions/frontmatter.ts';
export type ComposeAdjustment = 'none' | 'body' | 'frontmatter' | 'unresolved';
diff --git a/packages/core/src/bridge/doc-boundary-space.ts b/packages/core/src/bridge/doc-boundary-space.ts
index 01ef76a9f..8fb428b90 100644
--- a/packages/core/src/bridge/doc-boundary-space.ts
+++ b/packages/core/src/bridge/doc-boundary-space.ts
@@ -1,4 +1,3 @@
-
import { FRONTMATTER_RE, stripFrontmatter } from '../extensions/frontmatter.ts';
import { carriedEdgeEmpties, type DocEdgeEmpties } from '../markdown/doc-edge-blank-runs.ts';
diff --git a/packages/core/src/markdown/comment-promoter.ts b/packages/core/src/markdown/comment-promoter.ts
index fb0ba5a7c..fc267cbea 100644
--- a/packages/core/src/markdown/comment-promoter.ts
+++ b/packages/core/src/markdown/comment-promoter.ts
@@ -1,4 +1,3 @@
-
import type { Nodes, Paragraph, PhrasingContent, Root, RootContent, Text } from 'mdast';
import { SKIP, visit } from 'unist-util-visit';
import type { VFile } from 'vfile';
diff --git a/packages/core/src/markdown/merged-walker.ts b/packages/core/src/markdown/merged-walker.ts
index fa3cfcd86..bfd7e3109 100644
--- a/packages/core/src/markdown/merged-walker.ts
+++ b/packages/core/src/markdown/merged-walker.ts
@@ -1,4 +1,3 @@
-
import type { Nodes, Parent, Root } from 'mdast';
import { SKIP, visit } from 'unist-util-visit';
import type { VFile } from 'vfile';
diff --git a/packages/core/src/markdown/safe-url.ts b/packages/core/src/markdown/safe-url.ts
index 8be632ba4..4d3250d3a 100644
--- a/packages/core/src/markdown/safe-url.ts
+++ b/packages/core/src/markdown/safe-url.ts
@@ -1,4 +1,3 @@
-
export const SAFE_URL_SCHEMES = ['https', 'http', 'mailto', 'tel', 'ftp', 'sms'] as const;
const SCHEME_ALT = SAFE_URL_SCHEMES.map((s) => `${s}:`).join('|');
diff --git a/packages/core/src/schema-invariant.test.ts b/packages/core/src/schema-invariant.test.ts
index fa1d4f7b5..0fc7f0b4c 100644
--- a/packages/core/src/schema-invariant.test.ts
+++ b/packages/core/src/schema-invariant.test.ts
@@ -1,4 +1,5 @@
import { existsSync, readFileSync } from 'node:fs';
+import { fileURLToPath } from 'node:url';
import { getSchema, resolveExtensions } from '@tiptap/core';
import type { ContentMatch, NodeType } from '@tiptap/pm/model';
import { describe, expect, test } from 'vitest';
@@ -76,7 +77,7 @@ function captureSchemaShape(): SchemaSnapshot {
return { nodes, marks, extensionOrder };
}
-const SNAPSHOT_PATH = new URL('./schema-snapshot.json', import.meta.url).pathname;
+const SNAPSHOT_PATH = fileURLToPath(new URL('./schema-snapshot.json', import.meta.url));
function loadSnapshot(): SchemaSnapshot | null {
if (!existsSync(SNAPSHOT_PATH)) return null;
diff --git a/packages/server/src/http/api-pipeline.test.ts b/packages/server/src/http/api-pipeline.test.ts
index 4caabaf22..3b6d53df6 100644
--- a/packages/server/src/http/api-pipeline.test.ts
+++ b/packages/server/src/http/api-pipeline.test.ts
@@ -50,6 +50,15 @@ async function bootNativeRig(opts: { ephemeral?: boolean } = {}): Promise<Native
},
};
}
+ if (pathname === '/api/native-revalidated') {
+ return {
+ template: pathname,
+ dispatch: async (_req, res) => {
+ res.writeHead(200, { 'Content-Type': 'application/json', 'Cache-Control': 'no-cache' });
+ res.end('{}');
+ },
+ };
+ }
if (pathname === '/api/native-throw') {
return {
template: pathname,
@@ -72,6 +81,7 @@ async function bootNativeRig(opts: { ephemeral?: boolean } = {}): Promise<Native
'/api/agent-write-md',
'/api/native-mutating',
'/api/native-upload',
+ '/api/native-revalidated',
'/api/native-throw',
'/api/native-empty',
'/api/native-declined',
@@ -182,6 +192,34 @@ describe('natively-mounted /api routes run the shared admission pipeline', () =>
}
});
+ test('API answers default to Cache-Control: no-store, refusals included', async () => {
+ const rig = await bootNativeRig();
+ try {
+ const served = await fetch(`${rig.baseUrl}/api/native-ping`);
+ expect(served.status).toBe(200);
+ expect(served.headers.get('cache-control')).toBe('no-store');
+
+ const refused = await fetch(`${rig.baseUrl}/api/native-ping`, {
+ headers: { Origin: 'https://evil.example' },
+ });
+ expect(refused.status).toBe(403);
+ expect(refused.headers.get('cache-control')).toBe('no-store');
+ } finally {
+ await rig.close();
+ }
+ });
+
+ test('a route that names its own Cache-Control keeps it over the default', async () => {
+ const rig = await bootNativeRig();
+ try {
+ const res = await fetch(`${rig.baseUrl}/api/native-revalidated`);
+ expect(res.status).toBe(200);
+ expect(res.headers.get('cache-control')).toBe('no-cache');
+ } finally {
+ await rig.close();
+ }
+ });
+
test('a foreign Origin is refused before dispatch with the legacy problem shape', async () => {
const rig = await bootNativeRig();
try {
diff --git a/packages/server/src/http/api-pipeline.ts b/packages/server/src/http/api-pipeline.ts
index 8b9f71fd6..4199ad84f 100644
--- a/packages/server/src/http/api-pipeline.ts
+++ b/packages/server/src/http/api-pipeline.ts
@@ -219,6 +219,9 @@ export function createApiRequestPipeline(opts: ApiPipelineOptions): ApiRequestPi
}
if (url.startsWith('/api/')) {
+ if (typeof response.setHeader === 'function') {
+ response.setHeader('Cache-Control', 'no-store');
+ }
const origin = request.headers.origin;
const admission = admitRequestOrigin(origin, method, policy);
if (!admission.admitted) {
diff --git a/packages/server/src/http/document-routes.ts b/packages/server/src/http/document-routes.ts
index 2e336a539..a6a2db703 100644
--- a/packages/server/src/http/document-routes.ts
+++ b/packages/server/src/http/document-routes.ts
@@ -278,7 +278,7 @@ export function createDocumentRoutes(deps: DocumentRouteDeps): DocumentRoutes {
'Content-Type': 'application/x-ndjson',
'Transfer-Encoding': 'chunked',
'X-Content-Type-Options': 'nosniff',
- 'Cache-Control': 'no-cache',
+ 'Cache-Control': 'no-store',
});
const writeStreamError = createStreamingErrorWriter(res, 'document-list');