Open History to see the last commit
1/**
2 * Client-only query bindings must not ship SSR admission or snapshot capture.
3 * Authority: the maintainer's 2026-10-26 decision that SSR fixes have no code
4 * cost for applications that do not use SSR. The reference judgment depends
5 * only on whether the application imports DbClient, never on observer internals.
6 *
7 * These finite consumer programs cross ordinary, Suspense and infinite React
8 * hooks. esbuild consumes built ESM and minifies the consumer with production
9 * defines. Every aliased package must be rebuilt after its source changes.
10 * At the final bundle checkpoint, SSR transport operations must be absent from
11 * plain programs and present in DbClient controls. The controls prevent an
12 * empty bundle or a misspelled operation from giving a false green. This checks
13 * code inclusion, not SSR correctness, byte budgets, or every consumer bundler.
14 * DbClient is the current opt-in boundary. A browser app using DbClient only
15 * for collection descriptors still retains transport; this check does not claim
16 * to isolate that case without a separate SSR API.
17 * Existing framework hydration oracles continue to own runtime behavior.
18 */
19import assert from 'node:assert/strict'
20import { dirname, join } from 'node:path'
21import { fileURLToPath } from 'node:url'
22import { build } from 'esbuild'
23import { assertPackageDistFresh } from '../packages/db/scripts/assert-dist-fresh.mjs'
24
25const root = join(dirname(fileURLToPath(import.meta.url)), '..')
26const packages = ['db', 'db-ivm', 'react-db']
27const ssrOperations = ['_captureServerQuery', '_hasSsrCollectionSnapshot']
28const calibrate = process.argv.includes('--calibrate')
29await Promise.all(packages.map((pkg) => assertPackageDistFresh(root, pkg)))
30
31// Importing a hook creates no SSR capability. Explicitly importing DbClient
32// retains its transport implementation; the positive control needs every
33// operation to remain visible after the same minifier and public-name rules.
34for (const hook of [
35 'useLiveQuery',
36 'useLiveSuspenseQuery',
37 'useLiveInfiniteQuery',
38]) {
39 for (const transport of [false, true]) {
40 const result = await build({
41 stdin: {
42 contents: `export { ${hook} } from '@tanstack/react-db';\n${transport || calibrate ? `export { DbClient } from '@tanstack/db';` : ''}`,
43 resolveDir: root,
44 },
45 alias: Object.fromEntries(
46 packages.map((pkg) => [
47 `@tanstack/${pkg}`,
48 join(root, 'packages', pkg, 'dist/esm/index.js'),
49 ]),
50 ),
51 bundle: true,
52 minify: true,
53 write: false,
54 metafile: true,
55 platform: 'browser',
56 format: 'esm',
57 external: ['react', 'react-dom'],
58 define: { 'process.env.NODE_ENV': '"production"' },
59 logLevel: 'error',
60 })
61 const code = result.outputFiles[0].text
62 assert.ok(
63 code.length > 1000,
64 `${hook}: the consumer entry reached production`,
65 )
66 for (const operation of ssrOperations) {
67 assert.equal(
68 code.includes(operation),
69 transport,
70 `${hook} ${transport ? 'DbClient control must retain' : 'client-only bundle must exclude'} ${operation}`,
71 )
72 }
73 // bytesInOutput distinguishes code that ships from inputs the bundler
74 // traversed and then discarded. Merely checking the input graph is weaker.
75 const transportBytes = Object.values(result.metafile.outputs).reduce(
76 (total, output) =>
77 total +
78 Object.entries(output.inputs).reduce(
79 (bytes, [file, input]) =>
80 bytes +
81 (file.endsWith('/live-query-observer-client.js')
82 ? input.bytesInOutput
83 : 0),
84 0,
85 ),
86 0,
87 )
88 assert.equal(
89 transportBytes > 0,
90 transport,
91 `${hook}: observer transport implementation inclusion`,
92 )
93 console.log(
94 `${hook}: ${transport ? 'transport retained' : 'SSR policy excluded'}`,
95 )
96 }
97}