|
| 1 | +# Cookbook: Common Integration Patterns |
| 2 | + |
| 3 | +This cookbook focuses on production-friendly patterns that appear repeatedly in Renderify integrations. |
| 4 | + |
| 5 | +## Pattern 1: Renderer-Only Embed (No Built-In LLM) |
| 6 | + |
| 7 | +Use when plan generation happens on your backend or another model pipeline. |
| 8 | + |
| 9 | +```ts |
| 10 | +import { renderPlanInBrowser } from "renderify"; |
| 11 | +import type { RuntimePlan } from "@renderify/ir"; |
| 12 | + |
| 13 | +const plan: RuntimePlan = { |
| 14 | + specVersion: "runtime-plan/v1", |
| 15 | + id: "renderer_only_card", |
| 16 | + version: 1, |
| 17 | + root: { |
| 18 | + type: "element", |
| 19 | + tag: "section", |
| 20 | + children: [{ type: "text", value: "Hello from external plan" }], |
| 21 | + }, |
| 22 | + capabilities: { domWrite: true }, |
| 23 | +}; |
| 24 | + |
| 25 | +await renderPlanInBrowser(plan, { target: "#mount" }); |
| 26 | +``` |
| 27 | + |
| 28 | +## Pattern 2: Manifest-Pinned Production Execution |
| 29 | + |
| 30 | +Use when reproducibility matters and dependency drift is not acceptable. |
| 31 | + |
| 32 | +```ts |
| 33 | +import { renderPlanInBrowser } from "renderify"; |
| 34 | + |
| 35 | +await renderPlanInBrowser(planWithPinnedManifest, { |
| 36 | + target: "#mount", |
| 37 | + autoPinLatestModuleManifest: false, |
| 38 | + runtimeOptions: { |
| 39 | + enforceModuleManifest: true, |
| 40 | + enableDependencyPreflight: true, |
| 41 | + failOnDependencyPreflightError: true, |
| 42 | + }, |
| 43 | +}); |
| 44 | +``` |
| 45 | + |
| 46 | +Checklist: |
| 47 | + |
| 48 | +- Pin every bare specifier in `moduleManifest`. |
| 49 | +- In strict mode, include `integrity` for remote modules. |
| 50 | +- Run `probe-plan` in CI before release. |
| 51 | + |
| 52 | +## Pattern 3: Untrusted Source in Browser Sandbox |
| 53 | + |
| 54 | +Use when `plan.source` originates from untrusted prompts. |
| 55 | + |
| 56 | +```ts |
| 57 | +import { renderPlanInBrowser } from "renderify"; |
| 58 | + |
| 59 | +await renderPlanInBrowser(untrustedPlan, { |
| 60 | + target: "#mount", |
| 61 | + runtimeOptions: { |
| 62 | + browserSourceSandboxMode: "worker", |
| 63 | + browserSourceSandboxTimeoutMs: 4000, |
| 64 | + browserSourceSandboxFailClosed: true, |
| 65 | + }, |
| 66 | +}); |
| 67 | +``` |
| 68 | + |
| 69 | +Recommended policy posture: |
| 70 | + |
| 71 | +- `RENDERIFY_SECURITY_PROFILE=strict` |
| 72 | +- `RENDERIFY_RUNTIME_BROWSER_SANDBOX_FAIL_CLOSED=true` |
| 73 | +- `RENDERIFY_RUNTIME_JSPM_ONLY_STRICT_MODE=true` (when you want no fallback CDN path) |
| 74 | + |
| 75 | +## Pattern 4: Streaming Prompt to Progressive UI |
| 76 | + |
| 77 | +Use when chat UX needs preview updates before final render. |
| 78 | + |
| 79 | +```ts |
| 80 | +for await (const chunk of app.renderPromptStream(prompt, { previewEveryChunks: 2 })) { |
| 81 | + if (chunk.type === "llm-delta") { |
| 82 | + appendToken(chunk.delta); |
| 83 | + } |
| 84 | + |
| 85 | + if (chunk.type === "preview") { |
| 86 | + renderPreviewHtml(chunk.html); |
| 87 | + } |
| 88 | + |
| 89 | + if (chunk.type === "final") { |
| 90 | + commitFinalHtml(chunk.final.html); |
| 91 | + } |
| 92 | + |
| 93 | + if (chunk.type === "error") { |
| 94 | + showError(chunk.error.message); |
| 95 | + } |
| 96 | +} |
| 97 | +``` |
| 98 | + |
| 99 | +## Pattern 5: Probe Before Execute (CI Gate) |
| 100 | + |
| 101 | +Use when you need deterministic "will this render?" signals without executing source/component code. |
| 102 | + |
| 103 | +```bash |
| 104 | +pnpm cli -- probe-plan examples/runtime/recharts-dashboard-plan.json |
| 105 | +``` |
| 106 | + |
| 107 | +A practical CI flow: |
| 108 | + |
| 109 | +1. Run `probe-plan` for generated plans. |
| 110 | +2. Fail pipeline if `securityIssueCount > 0`. |
| 111 | +3. Fail pipeline if `preflightIssueCount > 0` in strict production lanes. |
| 112 | + |
| 113 | +## Pattern 6: Controlled Network Scope for Remote Modules |
| 114 | + |
| 115 | +Use when outbound network egress must be restricted. |
| 116 | + |
| 117 | +```ts |
| 118 | +await renderPlanInBrowser(plan, { |
| 119 | + runtimeOptions: { |
| 120 | + allowArbitraryNetwork: false, |
| 121 | + allowedNetworkHosts: ["ga.jspm.io", "cdn.jspm.io", "esm.sh"], |
| 122 | + remoteFallbackCdnBases: ["https://esm.sh"], |
| 123 | + }, |
| 124 | +}); |
| 125 | +``` |
| 126 | + |
| 127 | +Keep `allowedNetworkHosts` and `remoteFallbackCdnBases` aligned so fallback URLs do not violate runtime host policy. |
| 128 | + |
| 129 | +## Pattern 7: Explicit JSX Helper Behavior |
| 130 | + |
| 131 | +Use when you want stable transpilation behavior across environments. |
| 132 | + |
| 133 | +```ts |
| 134 | +await renderPlanInBrowser(planWithSource, { |
| 135 | + runtimeOptions: { |
| 136 | + runtimeSourceJsxHelperMode: "always", // "auto" | "always" | "never" |
| 137 | + }, |
| 138 | +}); |
| 139 | +``` |
| 140 | + |
| 141 | +- `auto`: runtime decides helper injection based on source/runtime mode. |
| 142 | +- `always`: always inject helper path (predictable for heterogeneous input). |
| 143 | +- `never`: only for advanced setups where helper wiring is external. |
| 144 | + |
| 145 | +## Pattern 8: Serialized Target Renders (Avoid UI Race Conditions) |
| 146 | + |
| 147 | +Use when multiple concurrent renders target the same mount element. |
| 148 | + |
| 149 | +```ts |
| 150 | +await Promise.all([ |
| 151 | + renderPlanInBrowser(planA, { target: "#mount", serializeTargetRenders: true }), |
| 152 | + renderPlanInBrowser(planB, { target: "#mount", serializeTargetRenders: true }), |
| 153 | +]); |
| 154 | +``` |
| 155 | + |
| 156 | +`renderPlanInBrowser` serializes operations per target element by default (`serializeTargetRenders !== false`). Keep it enabled unless you have a custom scheduler. |
| 157 | + |
| 158 | +## Related Docs |
| 159 | + |
| 160 | +- [`docs/getting-started.md`](./getting-started.md) |
| 161 | +- [`docs/runtime-execution.md`](./runtime-execution.md) |
| 162 | +- [`docs/security.md`](./security.md) |
| 163 | +- [`docs/troubleshooting-faq.md`](./troubleshooting-faq.md) |
0 commit comments