-
Notifications
You must be signed in to change notification settings - Fork 69
fix: SSR error prod-silence keys on the dev flag, not NODE_ENV #484
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
da18570
fix: SSR error prod-silence keys on the dev flag, not NODE_ENV
00b066c
fix: use the local dev param in loadingTemplates, not opts.dev
692b923
fix: thread dev into processSuspenseElements; add a reliable counterf…
5ee93b1
fix: thread the dev param through both SSR suspense helpers
c33da2d
docs: document the dev param on injectDSD + streamSuspenseBoundaries
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
121 changes: 121 additions & 0 deletions
121
packages/server/test/dev/ssr-error-prod-silence.test.js
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,121 @@ | ||
| /** | ||
| * SSR error prod-silence keys on the server `dev` flag, not NODE_ENV (#483). | ||
| * | ||
| * A component whose `render()` throws during SSR is isolated to a | ||
| * component-scoped error state (`defaultSSRErrorTemplate`). Dev surfaces the | ||
| * message; prod stays silent. The prod signal is the server `dev` flag threaded | ||
| * through the SSR render context, NOT `process.env.NODE_ENV` (which `webjs | ||
| * start` does not export, so a bare prod launch would otherwise leak). A | ||
| * context-free `renderToString` with no dev signal falls back to NODE_ENV. | ||
| */ | ||
| import { test, before, after } from 'node:test'; | ||
| import assert from 'node:assert/strict'; | ||
| import { mkdtempSync, rmSync, writeFileSync, mkdirSync } from 'node:fs'; | ||
| import { tmpdir } from 'node:os'; | ||
| import { dirname, join, resolve } from 'node:path'; | ||
| import { fileURLToPath, pathToFileURL } from 'node:url'; | ||
|
|
||
| import { createRequestHandler } from '../../src/dev.js'; | ||
| import { renderToString } from '../../../core/src/render-server.js'; | ||
| import { html } from '../../../core/src/html.js'; | ||
| import { WebComponent } from '../../../core/src/component.js'; | ||
|
|
||
| const __dirname = dirname(fileURLToPath(import.meta.url)); | ||
| const CORE_INDEX = pathToFileURL(resolve(__dirname, '../../../core/index.js')).toString(); | ||
|
|
||
| const SECRET = 'SECRET-DB-ERROR-9c2f1'; | ||
|
|
||
| // A component whose render throws, so the SSR walker isolates it to a | ||
| // component-scoped error state via defaultSSRErrorTemplate. | ||
| const THROW_CARD = | ||
| `import { WebComponent, html } from ${JSON.stringify(CORE_INDEX)};\n` + | ||
| `export class ThrowCard extends WebComponent {\n` + | ||
| ` render() { throw new Error('${SECRET}'); }\n` + | ||
| `}\n` + | ||
| `ThrowCard.register('throw-card');\n`; | ||
|
|
||
| const PAGE = | ||
| `import { html } from ${JSON.stringify(CORE_INDEX)};\n` + | ||
| `import './throw-card.ts';\n` + | ||
| `export default function P() {\n` + | ||
| ` return html\`<main><h1>page</h1><throw-card></throw-card></main>\`;\n` + | ||
| `}\n`; | ||
|
|
||
| let tmpRoot; | ||
| let savedNodeEnv; | ||
| before(() => { | ||
| tmpRoot = mkdtempSync(join(tmpdir(), 'webjs-ssr-err-')); | ||
| // Prove the signal is `dev`, not NODE_ENV, by running with NODE_ENV unset. | ||
| savedNodeEnv = process.env.NODE_ENV; | ||
| delete process.env.NODE_ENV; | ||
| }); | ||
| after(() => { | ||
| rmSync(tmpRoot, { recursive: true, force: true }); | ||
| if (savedNodeEnv === undefined) delete process.env.NODE_ENV; | ||
| else process.env.NODE_ENV = savedNodeEnv; | ||
| }); | ||
|
|
||
| function makeApp() { | ||
| const appDir = mkdtempSync(join(tmpRoot, 'app-')); | ||
| mkdirSync(join(appDir, 'app'), { recursive: true }); | ||
| writeFileSync(join(appDir, 'app', 'page.ts'), PAGE); | ||
| writeFileSync(join(appDir, 'app', 'throw-card.ts'), THROW_CARD); | ||
| return appDir; | ||
| } | ||
|
|
||
| test('PROD (dev:false, NODE_ENV unset): a thrown component render does NOT leak the message', async () => { | ||
| const app = await createRequestHandler({ appDir: makeApp(), dev: false }); | ||
| const res = await app.handle(new Request('http://x/')); | ||
| const body = await res.text(); | ||
| assert.ok(body.includes('page'), 'sibling content rendered (the throw is isolated)'); | ||
| assert.ok( | ||
| !body.includes(SECRET), | ||
| `prod must NOT leak the component error message even with NODE_ENV unset; body:\n${body}`, | ||
| ); | ||
| }); | ||
|
|
||
| test('DEV (dev:true): a thrown component render surfaces the message', async () => { | ||
| const app = await createRequestHandler({ appDir: makeApp(), dev: true }); | ||
| const res = await app.handle(new Request('http://x/')); | ||
| const body = await res.text(); | ||
| assert.ok(body.includes(SECRET), `dev must surface the component error message; body:\n${body}`); | ||
| }); | ||
|
|
||
| test('CORE: renderToString gates on suspenseCtx.dev, independent of NODE_ENV', async () => { | ||
|
vivek7405 marked this conversation as resolved.
|
||
| // This is the reliable counterfactual for the leak (#483). It drives a | ||
| // throwing component straight through renderToString with the suspenseCtx | ||
| // that ssr.js stamps, NODE_ENV deleted, so NO env signal masks the result: | ||
| // dev:false MUST stay silent (reverting the dev-gating to isProd() would leak | ||
| // here because isProd() is false with NODE_ENV unset), dev:true MUST surface. | ||
| class SuspenseThrow extends WebComponent { | ||
| render() { throw new Error('SUSPCTX-SECRET'); } | ||
| } | ||
| SuspenseThrow.register('suspctx-throw'); | ||
| const tpl = html`<div><suspctx-throw></suspctx-throw></div>`; | ||
| delete process.env.NODE_ENV; | ||
|
|
||
| const prod = await renderToString(tpl, { ssr: true, suspenseCtx: { pending: [], nextId: 1, dev: false } }); | ||
| assert.ok(!prod.includes('SUSPCTX-SECRET'), 'dev:false stays silent with NODE_ENV unset (the leak-prevention)'); | ||
|
|
||
| const dev = await renderToString(tpl, { ssr: true, suspenseCtx: { pending: [], nextId: 1, dev: true } }); | ||
| assert.ok(dev.includes('SUSPCTX-SECRET'), 'dev:true surfaces the message'); | ||
| }); | ||
|
|
||
| test('context-free renderToString falls back to NODE_ENV', async () => { | ||
| class CtxFreeThrow extends WebComponent { | ||
| render() { throw new Error('CTXFREE-SECRET'); } | ||
| } | ||
| CtxFreeThrow.register('ctxfree-throw'); | ||
| const tpl = html`<div><ctxfree-throw></ctxfree-throw></div>`; | ||
|
|
||
| // No dev signal + NODE_ENV unset (not 'production') means not prod, so surface. | ||
| delete process.env.NODE_ENV; | ||
| const dev = await renderToString(tpl); | ||
| assert.ok(dev.includes('CTXFREE-SECRET'), 'no dev signal + non-prod NODE_ENV surfaces the message'); | ||
|
|
||
| // No dev signal + NODE_ENV=production means prod, so stay silent. | ||
| process.env.NODE_ENV = 'production'; | ||
| const prod = await renderToString(tpl); | ||
| assert.ok(!prod.includes('CTXFREE-SECRET'), 'no dev signal + NODE_ENV=production stays silent'); | ||
| delete process.env.NODE_ENV; | ||
| }); | ||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.