Stream Server-Sent Events from an Edge Script, and keep the isolate alive while the connection is open.
An Edge Script can answer a request with a stream of Server-Sent Events. The script returns a Response that holds a ReadableStream, and the edge delivers each event as the script writes it.The CDN page describes how the network treats an event stream, including the heartbeat and the caching rules. This page describes what a script must do.
This example sends one event immediately, and one more every 15 seconds. The interval also acts as the heartbeat that keeps the connection open.
import * as BunnySDK from "@bunny.net/edgescript-sdk";const HEARTBEAT_MS = 15_000;BunnySDK.net.http.serve(async (request: Request) => { const encoder = new TextEncoder(); let timer: number; let onClosed: () => void; // Resolves when the client goes away and the runtime cancels the stream. const closed = new Promise<void>((resolve) => { onClosed = resolve; }); const stream = new ReadableStream({ start(controller) { let id = 0; const send = () => { id += 1; controller.enqueue( encoder.encode( `id: ${id}\nevent: tick\ndata: ${new Date().toISOString()}\n\n`, ), ); }; // Send the first event at once. The client waits until this byte. send(); timer = setInterval(send, HEARTBEAT_MS); }, cancel() { clearInterval(timer); onClosed(); }, }); // It is important to call waitUntil; otherwise the script may be evicted // while the stream is still open. Bunny.v1.waitUntil(closed); return new Response(stream, { headers: { "content-type": "text/event-stream", "cache-control": "no-cache", }, });});
Return the Response at once, and let the stream fill in afterwards. A script that waits before it returns is treated as a slow origin: the edge allows 60 seconds for a response to start, and answers with 504 Gateway Timeout when nothing arrives. This applies to any await before the return, and a subrequest is the common case.
// Wrong. If the subrequest takes longer than 60 seconds, the client gets a 504// and never sees the stream.const upstream = await fetch(slowOrigin);return new Response(upstream.body, { headers });
// Right. The response goes back at once, and the subrequest runs inside the// stream, so a slow origin delays an event instead of failing the request.const stream = new ReadableStream({ async start(controller) { const upstream = await fetch(slowOrigin); // ... write events from the upstream body },});return new Response(stream, { headers });
Write the first event as soon as the stream opens, and do not wait for the
first real update. The client receives no part of the response until your
script writes the first byte, and this includes the response headers.
The isolate that answers a request can be evicted once the handler returns. An event stream lives longer than the handler, so pass a promise to waitUntil that resolves only when the stream ends. Without it, the script can stop in the middle of a stream.Always resolve that promise when the stream closes. A promise that never settles holds the isolate open after the client has gone.
A stream that a script returns is not cached, and it needs no Cache-Control header. The edge serves every request to a standalone script from the script itself.Set Cache-Control: no-cache anyway if the same code also runs behind a pull zone as middleware, where the normal caching rules apply.
The Edge Scripting limits apply, with one point to note: the 30-second CPU time limit measures processor time, and not the time a connection stays open. A stream that waits between events uses almost no CPU time. Pricing follows the same rule.Keep the work inside the stream small. A script that builds each event with a subrequest reaches the subrequest limit quickly.
{"docsConfig":{"theme":"mint","$schema":"https://mintlify.com/docs.json","name":"bunny.net Documentation","colors":{"primary":"#EF6126","light":"#EF6126","dark":"#EF6126"},"logo":{"light":"https://mintcdn.com/bunnynet-cb9733c2/54-4KCQKoNVNuoyW/logo/bunny-logo-dark.svg?fit=max\u0026auto=format\u0026n=54-4KCQKoNVNuoyW\u0026q=85\u0026s=62c091ea1aaea93d0a981902b7783e37","dark":"https://mintcdn.com/bunnynet-cb9733c2/54-4KCQKoNVNuoyW/logo/bunny-logo-light.svg?fit=max\u0026auto=format\u0026n=54-4KCQKoNVNuoyW\u0026q=85\u0026s=3610e32d68a9fa458b0116ac58ca812a"},"favicon":"https://mintcdn.com/bunnynet-cb9733c2/1bTPx1eyQm8EK8W3/favicon.png?fit=max\u0026auto=format\u0026n=1bTPx1eyQm8EK8W3\u0026q=85\u0026s=85267b63e804fac7a4dd94599bb8c6e8","navbar":{"links":[{"href":"https://bunny.net/docs/api-reference","label":"API Reference"},{"href":"https://github.com/BunnyWay","label":"GitHub"}],"primary":{"type":"button","label":"Get Started","href":"https://dash.bunny.net/auth/register"}},"footer":{"socials":{"discord":"https://discord.gg/bunnynet","github":"https://github.com/BunnyWay","x":"https://x.com/BunnyCDN","bluesky":"https://bsky.app/profile/bunny.net","linkedin":"https://www.linkedin.com/company/bunnynet","facebook":"https://www.facebook.com/bunnycdn"}},"icons":{"library":"lucide"},"contextual":{"options":["copy","claude","chatgpt","cursor","view","vscode"],"display":"header"},"interaction":{"drilldown":false},"metadata":{"timestamp":true},"styling":{"latex":false},"navigation":{"pages":[]}},"deployment":{"deploymentMetadataValue":{"subdomain":"bunnynet-cb9733c2","hasAudioNarration":false,"actualSubdomain":"bunnynet-cb9733c2","customDomains":["bunny.net"],"gitSource":{"type":"github","owner":"bunnyway","repo":"documentation","deployBranch":"main","contentDirectory":"","isPrivate":false,"mountPath":"","label":""},"chroma":{"liveDeploymentHistoryId":"6ae49f2d21d44d6b52dcfe35","collectionId":"5e262b87-ade9-45d8-9b63-06888c8d59a3","schemaVersion":"v3","latestBuiltDeploymentHistoryId":"6ae49f2d21d44d6b52dcfe35"},"feedback":{"thumbs":true,"contextualFeedback":{"allowEmail":true},"agentFeedback":true,"edits":true,"issues":false},"entitlements":{"AI_CHAT":{"status":"ENABLED"},"WEB_MCP":{"status":"ENABLED"},"NARRATION":{"status":"ENABLED"}},"buildId":"6ae586f29229f31e96cc51a9","clientVersion":"0.0.4043","searchFilterCounts":[{"language":"default","tag":"Documentation","count":752},{"language":"default","tag":"API Reference","count":318},{"language":"default","tag":"Core Platform API","count":109},{"language":"default","tag":"CDN","count":82},{"language":"default","tag":"Shield API","count":66},{"language":"default","tag":"Stream","count":66},{"language":"default","tag":"Magic Containers API","count":56},{"language":"default","tag":"Storage","count":54},{"language":"default","tag":"Guides","count":54},{"language":"default","tag":"Reference","count":52}],"searchSettings":{"isFiltersEnabled":true}},"assistantConfig":{"enableStarterQuestions":false,"deflection":{"enabled":true,"type":"link","email":"support@bunny.net","url":"https://bunny.net/contact","showHelpButton":true}},"basePath":"/docs","hasChatPermissions":true,"hasWebMcpPermissions":true,"toggles":[{"name":"kill-isr","enabled":true,"variant":{"name":"disabled","enabled":false,"feature_enabled":true},"impressionData":false},{"name":"agent-mintlify-slack","enabled":true,"variant":{"name":"disabled","enabled":false,"feature_enabled":true},"impressionData":false},{"name":"agent-integrations-settings","enabled":true,"variant":{"name":"disabled","enabled":false,"feature_enabled":true},"impressionData":false},{"name":"automations-pr-grouping","enabled":true,"variant":{"name":"disabled","enabled":false,"feature_enabled":true},"impressionData":false},{"name":"custom-prefix","enabled":true,"variant":{"name":"disabled","enabled":false,"feature_enabled":true},"impressionData":false},{"name":"analytics-v7","enabled":true,"variant":{"name":"disabled","enabled":false,"feature_enabled":true},"impressionData":false},{"name":"user-flows-analytics","enabled":true,"variant":{"name":"disabled","enabled":false,"feature_enabled":true},"impressionData":false},{"name":"no-publish-flow","enabled":true,"variant":{"name":"disabled","enabled":false,"feature_enabled":true},"impressionData":false},{"name":"workos-onboarding","enabled":true,"variant":{"name":"disabled","enabled":false,"feature_enabled":true},"impressionData":false},{"name":"llms-txt-v2","enabled":true,"variant":{"name":"disabled","enabled":false,"feature_enabled":true},"impressionData":false},{"name":"snapshot-serving-live","enabled":true,"variant":{"name":"disabled","enabled":false,"feature_enabled":true},"impressionData":false},{"name":"stripTemperatureMiddleware","enabled":true,"variant":{"name":"disabled","enabled":false,"feature_enabled":true},"impressionData":false},{"name":"support-platform-pylon-zendesk","enabled":true,"variant":{"name":"disabled","enabled":false,"feature_enabled":true},"impressionData":false},{"name":"snapshot-serving","enabled":true,"variant":{"name":"disabled","enabled":false,"feature_enabled":true},"impressionData":false},{"name":"instant-rollback","enabled":true,"variant":{"name":"disabled","enabled":false,"feature_enabled":true},"impressionData":false},{"name":"granular-notifications-server","enabled":true,"variant":{"name":"disabled","enabled":false,"feature_enabled":true},"impressionData":false},{"name":"granular-notifications","enabled":true,"variant":{"name":"disabled","enabled":false,"feature_enabled":true},"impressionData":false},{"name":"automations-fable","enabled":true,"variant":{"name":"disabled","enabled":false,"feature_enabled":true},"impressionData":false},{"name":"fixed-ai-action-pricing","enabled":true,"variant":{"name":"disabled","enabled":false,"feature_enabled":true},"impressionData":false},{"name":"automation-resolve-merge-conflict","enabled":true,"variant":{"name":"disabled","enabled":false,"feature_enabled":true},"impressionData":false},{"name":"scoped-publishing","enabled":true,"variant":{"name":"disabled","enabled":false,"feature_enabled":true},"impressionData":false},{"name":"mintql","enabled":true,"variant":{"name":"disabled","enabled":false,"feature_enabled":true},"impressionData":false},{"name":"page-level-analytics","enabled":true,"variant":{"name":"disabled","enabled":false,"feature_enabled":true},"impressionData":false},{"name":"editor-version-history-v2","enabled":true,"variant":{"name":"disabled","enabled":false,"feature_enabled":true},"impressionData":false},{"name":"billing-page-v2","enabled":true,"variant":{"name":"disabled","enabled":false,"feature_enabled":true},"impressionData":false},{"name":"full-page-search","enabled":true,"variant":{"name":"disabled","enabled":false,"feature_enabled":true},"impressionData":false},{"name":"workspace-projects","enabled":true,"variant":{"name":"disabled","enabled":false,"feature_enabled":true},"impressionData":false},{"name":"assistant-v5","enabled":true,"variant":{"name":"disabled","enabled":false,"feature_enabled":true},"impressionData":false},{"name":"byok-sep-25","enabled":true,"variant":{"name":"disabled","enabled":false,"feature_enabled":true},"impressionData":false},{"name":"fork-pr-previews","enabled":true,"variant":{"name":"disabled","enabled":false,"feature_enabled":true},"impressionData":false},{"name":"revalidation-jwt","enabled":true,"variant":{"name":"disabled","enabled":false,"feature_enabled":true},"impressionData":false},{"name":"vite-origin","enabled":true,"variant":{"name":"disabled","enabled":false,"feature_enabled":true},"impressionData":false},{"name":"billing-updated-9-15","enabled":true,"variant":{"name":"disabled","enabled":false,"feature_enabled":true},"impressionData":false},{"name":"workos-sso-scim","enabled":true,"variant":{"name":"disabled","enabled":false,"feature_enabled":true},"impressionData":false},{"name":"hobby-abuse-scan","enabled":true,"variant":{"name":"disabled","enabled":false,"feature_enabled":true},"impressionData":false},{"name":"dashboard-inbox","enabled":true,"variant":{"name":"disabled","enabled":false,"feature_enabled":true},"impressionData":false},{"name":"workos-signup","enabled":true,"variant":{"name":"disabled","enabled":false,"feature_enabled":true},"impressionData":false},{"name":"turbopuffer-dual-write","enabled":true,"variant":{"name":"disabled","enabled":false,"feature_enabled":true},"impressionData":false},{"name":"workos-enabled","enabled":true,"variant":{"name":"disabled","enabled":false,"feature_enabled":true},"impressionData":false},{"name":"approve-review-flow","enabled":true,"variant":{"name":"disabled","enabled":false,"feature_enabled":true},"impressionData":false},{"name":"sso-setup-deadline-banner","enabled":true,"variant":{"name":"disabled","enabled":false,"feature_enabled":true},"impressionData":false}],"privacy":{"consentBanner":{"enabled":true}},"googleFontsInlined":false},"customCss":[{"filePath":"style.css","content":".dark img[src*=\"/logo/frameworks/angular.svg\"],.dark img[src*=\"/logo/frameworks/eleventy.svg\"],.dark img[src*=\"/logo/frameworks/express.svg\"],.dark img[src*=\"/logo/frameworks/mariadb.svg\"],.dark img[src*=\"/logo/frameworks/nextjs.svg\"],.dark img[src*=\"/logo/frameworks/sphinx.svg\"]{filter:invert(1)}html:not(.dark) img[src*=\"/logo/frameworks/zola.svg\"]{filter:invert(1)}"}],"page":{"pagePath":"scripting/server-sent-events","slug":"/scripting/server-sent-events","pageMetadata":{"title":"Server-Sent Events","description":"Stream Server-Sent Events from an Edge Script, and keep the isolate alive while the connection is open.","href":"/scripting/server-sent-events"},"description":{"compiledSource":"\"use strict\";\nconst {jsx: _jsx} = arguments[0];\nconst {useMDXComponents: _provideComponents} = arguments[0];\nfunction _createMdxContent(props) {\n const _components = {\n p: \"p\",\n ..._provideComponents(),\n ...props.components\n };\n return _jsx(_components.p, {\n children: \"Stream Server-Sent Events from an Edge Script, and keep the isolate alive while the connection is open.\"\n });\n}\nfunction MDXContent(props = {}) {\n const {wrapper: MDXLayout} = {\n ..._provideComponents(),\n ...props.components\n };\n return MDXLayout ? _jsx(MDXLayout, {\n ...props,\n children: _jsx(_createMdxContent, {\n ...props\n })\n }) : _createMdxContent(props);\n}\nreturn {\n default: MDXContent\n};\n","frontmatter":{},"scope":{}},"pageType":"default","mdx":{"mdxSource":{"compiledSource":"\"use strict\";const{Fragment:_Fragment,jsx:_jsx,jsxs:_jsxs}=arguments[0];const{useMDXComponents:_provideComponents}=arguments[0];function _createMdxContent(props){const _components={a:`a`,code:`code`,li:`li`,p:`p`,pre:`pre`,span:`span`,ul:`ul`,..._provideComponents(),...props.components},{CodeBlock,Heading,Warning}=_components;if(!CodeBlock)_missingMdxReference(`CodeBlock`,true);if(!Heading)_missingMdxReference(`Heading`,true);if(!Warning)_missingMdxReference(`Warning`,true);return _jsxs(_Fragment,{children:[_jsxs(_components.p,{children:[`An Edge Script can answer a request with a stream of `,_jsx(_components.a,{href:`/cdn/server-sent-events`,children:`Server-Sent Events`}),`. The script returns a `,_jsx(_components.code,{children:`Response`}),` that holds a `,_jsx(_components.code,{children:`ReadableStream`}),`, and the edge delivers each event as the script writes it.`]}),`\n`,_jsxs(_components.p,{children:[`The `,_jsx(_components.a,{href:`/cdn/server-sent-events`,children:`CDN page`}),` describes how the network treats an event stream, including the heartbeat and the caching rules. This page describes what a script must do.`]}),`\n`,_jsx(Heading,{level:`2`,id:`quickstart`,children:`Quickstart`}),`\n`,_jsx(_components.p,{children:`This example sends one event immediately, and one more every 15 seconds. The interval also acts as the heartbeat that keeps the connection open.`}),`\n`,_jsx(CodeBlock,{filename:``,numberOfLines:`49`,language:`typescript`,children:_jsx(_components.pre,{className:`shiki shiki-themes github-light-default dark-plus`,style:{backgroundColor:`#ffffff`,\"--shiki-dark-bg\":`#0B0C0E`,color:`#1f2328`,\"--shiki-dark\":`#D4D4D4`},language:`typescript`,children:_jsxs(_components.code,{language:`typescript`,numberOfLines:`49`,children:[_jsxs(_components.span,{className:`line`,children:[_jsx(_components.span,{style:{color:`#CF222E`,\"--shiki-dark\":`#C586C0`},children:`import`}),_jsx(_components.span,{style:{color:`#0550AE`,\"--shiki-dark\":`#569CD6`},children:` *`}),_jsx(_components.span,{style:{color:`#CF222E`,\"--shiki-dark\":`#C586C0`},children:` as`}),_jsx(_components.span,{style:{color:`#1F2328`,\"--shiki-dark\":`#9CDCFE`},children:` BunnySDK`}),_jsx(_components.span,{style:{color:`#CF222E`,\"--shiki-dark\":`#C586C0`},children:` from`}),_jsx(_components.span,{style:{color:`#0A3069`,\"--shiki-dark\":`#CE9178`},children:` \"@bunny.net/edgescript-sdk\"`}),_jsx(_components.span,{style:{color:`#1F2328`,\"--shiki-dark\":`#D4D4D4`},children:`;`})]}),`\n`,_jsx(_components.span,{className:`line`}),`\n`,_jsxs(_components.span,{className:`line`,children:[_jsx(_components.span,{style:{color:`#CF222E`,\"--shiki-dark\":`#569CD6`},children:`const`}),_jsx(_components.span,{style:{color:`#0550AE`,\"--shiki-dark\":`#4FC1FF`},children:` HEARTBEAT_MS`}),_jsx(_components.span,{style:{color:`#CF222E`,\"--shiki-dark\":`#D4D4D4`},children:` =`}),_jsx(_components.span,{style:{color:`#0550AE`,\"--shiki-dark\":`#B5CEA8`},children:` 15_000`}),_jsx(_components.span,{style:{color:`#1F2328`,\"--shiki-dark\":`#D4D4D4`},children:`;`})]}),`\n`,_jsx(_components.span,{className:`line`}),`\n`,_jsxs(_components.span,{className:`line`,children:[_jsx(_components.span,{style:{color:`#1F2328`,\"--shiki-dark\":`#9CDCFE`},children:`BunnySDK`}),_jsx(_components.span,{style:{color:`#1F2328`,\"--shiki-dark\":`#D4D4D4`},children:`.`}),_jsx(_components.span,{style:{color:`#1F2328`,\"--shiki-dark\":`#9CDCFE`},children:`net`}),_jsx(_components.span,{style:{color:`#1F2328`,\"--shiki-dark\":`#D4D4D4`},children:`.`}),_jsx(_components.span,{style:{color:`#1F2328`,\"--shiki-dark\":`#9CDCFE`},children:`http`}),_jsx(_components.span,{style:{color:`#1F2328`,\"--shiki-dark\":`#D4D4D4`},children:`.`}),_jsx(_components.span,{style:{color:`#8250DF`,\"--shiki-dark\":`#DCDCAA`},children:`serve`}),_jsx(_components.span,{style:{color:`#1F2328`,\"--shiki-dark\":`#D4D4D4`},children:`(`}),_jsx(_components.span,{style:{color:`#CF222E`,\"--shiki-dark\":`#569CD6`},children:`async`}),_jsx(_components.span,{style:{color:`#1F2328`,\"--shiki-dark\":`#D4D4D4`},children:` (`}),_jsx(_components.span,{style:{color:`#953800`,\"--shiki-dark\":`#9CDCFE`},children:`request`}),_jsx(_components.span,{style:{color:`#CF222E`,\"--shiki-dark\":`#D4D4D4`},children:`:`}),_jsx(_components.span,{style:{color:`#953800`,\"--shiki-dark\":`#4EC9B0`},children:` Request`}),_jsx(_components.span,{style:{color:`#1F2328`,\"--shiki-dark\":`#D4D4D4`},children:`) `}),_jsx(_components.span,{style:{color:`#CF222E`,\"--shiki-dark\":`#569CD6`},children:`=>`}),_jsx(_components.span,{style:{color:`#1F2328`,\"--shiki-dark\":`#D4D4D4`},children:` {`})]}),`\n`,_jsxs(_components.span,{className:`line`,children:[_jsx(_components.span,{style:{color:`#CF222E`,\"--shiki-dark\":`#569CD6`},children:` const`}),_jsx(_components.span,{style:{color:`#0550AE`,\"--shiki-dark\":`#4FC1FF`},children:` encoder`}),_jsx(_components.span,{style:{color:`#CF222E`,\"--shiki-dark\":`#D4D4D4`},children:` =`}),_jsx(_components.span,{style:{color:`#CF222E`,\"--shiki-dark\":`#569CD6`},children:` new`}),_jsx(_components.span,{style:{color:`#8250DF`,\"--shiki-dark\":`#DCDCAA`},children:` TextEncoder`}),_jsx(_components.span,{style:{color:`#1F2328`,\"--shiki-dark\":`#D4D4D4`},children:`();`})]}),`\n`,_jsxs(_components.span,{className:`line`,children:[_jsx(_components.span,{style:{color:`#CF222E`,\"--shiki-dark\":`#569CD6`},children:` let`}),_jsx(_components.span,{style:{color:`#1F2328`,\"--shiki-dark\":`#9CDCFE`},children:` timer`}),_jsx(_components.span,{style:{color:`#CF222E`,\"--shiki-dark\":`#D4D4D4`},children:`:`}),_jsx(_components.span,{style:{color:`#0550AE`,\"--shiki-dark\":`#4EC9B0`},children:` number`}),_jsx(_components.span,{style:{color:`#1F2328`,\"--shiki-dark\":`#D4D4D4`},children:`;`})]}),`\n`,_jsxs(_components.span,{className:`line`,children:[_jsx(_components.span,{style:{color:`#CF222E`,\"--shiki-dark\":`#569CD6`},children:` let`}),_jsx(_components.span,{style:{color:`#8250DF`,\"--shiki-dark\":`#DCDCAA`},children:` onClosed`}),_jsx(_components.span,{style:{color:`#CF222E`,\"--shiki-dark\":`#D4D4D4`},children:`:`}),_jsx(_components.span,{style:{color:`#1F2328`,\"--shiki-dark\":`#D4D4D4`},children:` () `}),_jsx(_components.span,{style:{color:`#CF222E`,\"--shiki-dark\":`#569CD6`},children:`=>`}),_jsx(_components.span,{style:{color:`#0550AE`,\"--shiki-dark\":`#4EC9B0`},children:` void`}),_jsx(_components.span,{style:{color:`#1F2328`,\"--shiki-dark\":`#D4D4D4`},children:`;`})]}),`\n`,_jsx(_components.span,{className:`line`}),`\n`,_jsx(_components.span,{className:`line`,children:_jsx(_components.span,{style:{color:`#6E7781`,\"--shiki-dark\":`#6A9955`},children:` // Resolves when the client goes away and the runtime cancels the stream.`})}),`\n`,_jsxs(_components.span,{className:`line`,children:[_jsx(_components.span,{style:{color:`#CF222E`,\"--shiki-dark\":`#569CD6`},children:` const`}),_jsx(_components.span,{style:{color:`#0550AE`,\"--shiki-dark\":`#4FC1FF`},children:` closed`}),_jsx(_components.span,{style:{color:`#CF222E`,\"--shiki-dark\":`#D4D4D4`},children:` =`}),_jsx(_components.span,{style:{color:`#CF222E`,\"--shiki-dark\":`#569CD6`},children:` new`}),_jsx(_components.span,{style:{color:`#0550AE`,\"--shiki-dark\":`#4EC9B0`},children:` Promise`}),_jsx(_components.span,{style:{color:`#1F2328`,\"--shiki-dark\":`#D4D4D4`},children:`\u003c`}),_jsx(_components.span,{style:{color:`#0550AE`,\"--shiki-dark\":`#4EC9B0`},children:`void`}),_jsx(_components.span,{style:{color:`#1F2328`,\"--shiki-dark\":`#D4D4D4`},children:`>((`}),_jsx(_components.span,{style:{color:`#953800`,\"--shiki-dark\":`#9CDCFE`},children:`resolve`}),_jsx(_components.span,{style:{color:`#1F2328`,\"--shiki-dark\":`#D4D4D4`},children:`) `}),_jsx(_components.span,{style:{color:`#CF222E`,\"--shiki-dark\":`#569CD6`},children:`=>`}),_jsx(_components.span,{style:{color:`#1F2328`,\"--shiki-dark\":`#D4D4D4`},children:` {`})]}),`\n`,_jsxs(_components.span,{className:`line`,children:[_jsx(_components.span,{style:{color:`#1F2328`,\"--shiki-dark\":`#9CDCFE`},children:` onClosed`}),_jsx(_components.span,{style:{color:`#CF222E`,\"--shiki-dark\":`#D4D4D4`},children:` =`}),_jsx(_components.span,{style:{color:`#1F2328`,\"--shiki-dark\":`#9CDCFE`},children:` resolve`}),_jsx(_components.span,{style:{color:`#1F2328`,\"--shiki-dark\":`#D4D4D4`},children:`;`})]}),`\n`,_jsx(_components.span,{className:`line`,children:_jsx(_components.span,{style:{color:`#1F2328`,\"--shiki-dark\":`#D4D4D4`},children:` });`})}),`\n`,_jsx(_components.span,{className:`line`}),`\n`,_jsxs(_components.span,{className:`line`,children:[_jsx(_components.span,{style:{color:`#CF222E`,\"--shiki-dark\":`#569CD6`},children:` const`}),_jsx(_components.span,{style:{color:`#0550AE`,\"--shiki-dark\":`#4FC1FF`},children:` stream`}),_jsx(_components.span,{style:{color:`#CF222E`,\"--shiki-dark\":`#D4D4D4`},children:` =`}),_jsx(_components.span,{style:{color:`#CF222E`,\"--shiki-dark\":`#569CD6`},children:` new`}),_jsx(_components.span,{style:{color:`#8250DF`,\"--shiki-dark\":`#DCDCAA`},children:` ReadableStream`}),_jsx(_components.span,{style:{color:`#1F2328`,\"--shiki-dark\":`#D4D4D4`},children:`({`})]}),`\n`,_jsxs(_components.span,{className:`line`,children:[_jsx(_components.span,{style:{color:`#8250DF`,\"--shiki-dark\":`#DCDCAA`},children:` start`}),_jsx(_components.span,{style:{color:`#1F2328`,\"--shiki-dark\":`#D4D4D4`},children:`(`}),_jsx(_components.span,{style:{color:`#953800`,\"--shiki-dark\":`#9CDCFE`},children:`controller`}),_jsx(_components.span,{style:{color:`#1F2328`,\"--shiki-dark\":`#D4D4D4`},children:`) {`})]}),`\n`,_jsxs(_components.span,{className:`line`,children:[_jsx(_components.span,{style:{color:`#CF222E`,\"--shiki-dark\":`#569CD6`},children:` let`}),_jsx(_components.span,{style:{color:`#1F2328`,\"--shiki-dark\":`#9CDCFE`},children:` id`}),_jsx(_components.span,{style:{color:`#CF222E`,\"--shiki-dark\":`#D4D4D4`},children:` =`}),_jsx(_components.span,{style:{color:`#0550AE`,\"--shiki-dark\":`#B5CEA8`},children:` 0`}),_jsx(_components.span,{style:{color:`#1F2328`,\"--shiki-dark\":`#D4D4D4`},children:`;`})]}),`\n`,_jsx(_components.span,{className:`line`}),`\n`,_jsxs(_components.span,{className:`line`,children:[_jsx(_components.span,{style:{color:`#CF222E`,\"--shiki-dark\":`#569CD6`},children:` const`}),_jsx(_components.span,{style:{color:`#8250DF`,\"--shiki-dark\":`#DCDCAA`},children:` send`}),_jsx(_components.span,{style:{color:`#CF222E`,\"--shiki-dark\":`#D4D4D4`},children:` =`}),_jsx(_components.span,{style:{color:`#1F2328`,\"--shiki-dark\":`#D4D4D4`},children:` () `}),_jsx(_components.span,{style:{color:`#CF222E`,\"--shiki-dark\":`#569CD6`},children:`=>`}),_jsx(_components.span,{style:{color:`#1F2328`,\"--shiki-dark\":`#D4D4D4`},children:` {`})]}),`\n`,_jsxs(_components.span,{className:`line`,children:[_jsx(_components.span,{style:{color:`#1F2328`,\"--shiki-dark\":`#9CDCFE`},children:` id`}),_jsx(_components.span,{style:{color:`#CF222E`,\"--shiki-dark\":`#D4D4D4`},children:` +=`}),_jsx(_components.span,{style:{color:`#0550AE`,\"--shiki-dark\":`#B5CEA8`},children:` 1`}),_jsx(_components.span,{style:{color:`#1F2328`,\"--shiki-dark\":`#D4D4D4`},children:`;`})]}),`\n`,_jsxs(_components.span,{className:`line`,children:[_jsx(_components.span,{style:{color:`#1F2328`,\"--shiki-dark\":`#9CDCFE`},children:` controller`}),_jsx(_components.span,{style:{color:`#1F2328`,\"--shiki-dark\":`#D4D4D4`},children:`.`}),_jsx(_components.span,{style:{color:`#8250DF`,\"--shiki-dark\":`#DCDCAA`},children:`enqueue`}),_jsx(_components.span,{style:{color:`#1F2328`,\"--shiki-dark\":`#D4D4D4`},children:`(`})]}),`\n`,_jsxs(_components.span,{className:`line`,children:[_jsx(_components.span,{style:{color:`#1F2328`,\"--shiki-dark\":`#9CDCFE`},children:` encoder`}),_jsx(_components.span,{style:{color:`#1F2328`,\"--shiki-dark\":`#D4D4D4`},children:`.`}),_jsx(_components.span,{style:{color:`#8250DF`,\"--shiki-dark\":`#DCDCAA`},children:`encode`}),_jsx(_components.span,{style:{color:`#1F2328`,\"--shiki-dark\":`#D4D4D4`},children:`(`})]}),`\n`,_jsxs(_components.span,{className:`line`,children:[_jsx(_components.span,{style:{color:`#0A3069`,\"--shiki-dark\":`#CE9178`},children:\" `id: \"}),_jsx(_components.span,{style:{color:`#0A3069`,\"--shiki-dark\":`#569CD6`},children:\"${\"}),_jsx(_components.span,{style:{color:`#1F2328`,\"--shiki-dark\":`#9CDCFE`},children:`id`}),_jsx(_components.span,{style:{color:`#0A3069`,\"--shiki-dark\":`#569CD6`},children:`}`}),_jsx(_components.span,{style:{color:`#CF222E`,\"--shiki-dark\":`#D7BA7D`},children:`\\\\n`}),_jsx(_components.span,{style:{color:`#0A3069`,\"--shiki-dark\":`#CE9178`},children:`event: tick`}),_jsx(_components.span,{style:{color:`#CF222E`,\"--shiki-dark\":`#D7BA7D`},children:`\\\\n`}),_jsx(_components.span,{style:{color:`#0A3069`,\"--shiki-dark\":`#CE9178`},children:`data: `}),_jsx(_components.span,{style:{color:`#0A3069`,\"--shiki-dark\":`#569CD6`},children:\"${\"}),_jsx(_components.span,{style:{color:`#CF222E`,\"--shiki-dark\":`#569CD6`},children:`new`}),_jsx(_components.span,{style:{color:`#8250DF`,\"--shiki-dark\":`#DCDCAA`},children:` Date`}),_jsx(_components.span,{style:{color:`#0A3069`,\"--shiki-dark\":`#D4D4D4`},children:`().`}),_jsx(_components.span,{style:{color:`#8250DF`,\"--shiki-dark\":`#DCDCAA`},children:`toISOString`}),_jsx(_components.span,{style:{color:`#0A3069`,\"--shiki-dark\":`#D4D4D4`},children:`()`}),_jsx(_components.span,{style:{color:`#0A3069`,\"--shiki-dark\":`#569CD6`},children:`}`}),_jsx(_components.span,{style:{color:`#CF222E`,\"--shiki-dark\":`#D7BA7D`},children:`\\\\n\\\\n`}),_jsx(_components.span,{style:{color:`#0A3069`,\"--shiki-dark\":`#CE9178`},children:\"`\"}),_jsx(_components.span,{style:{color:`#1F2328`,\"--shiki-dark\":`#D4D4D4`},children:`,`})]}),`\n`,_jsx(_components.span,{className:`line`,children:_jsx(_components.span,{style:{color:`#1F2328`,\"--shiki-dark\":`#D4D4D4`},children:` ),`})}),`\n`,_jsx(_components.span,{className:`line`,children:_jsx(_components.span,{style:{color:`#1F2328`,\"--shiki-dark\":`#D4D4D4`},children:` );`})}),`\n`,_jsx(_components.span,{className:`line`,children:_jsx(_components.span,{style:{color:`#1F2328`,\"--shiki-dark\":`#D4D4D4`},children:` };`})}),`\n`,_jsx(_components.span,{className:`line`}),`\n`,_jsx(_components.span,{className:`line`,children:_jsx(_components.span,{style:{color:`#6E7781`,\"--shiki-dark\":`#6A9955`},children:` // Send the first event at once. The client waits until this byte.`})}),`\n`,_jsxs(_components.span,{className:`line`,children:[_jsx(_components.span,{style:{color:`#8250DF`,\"--shiki-dark\":`#DCDCAA`},children:` send`}),_jsx(_components.span,{style:{color:`#1F2328`,\"--shiki-dark\":`#D4D4D4`},children:`();`})]}),`\n`,_jsx(_components.span,{className:`line`}),`\n`,_jsxs(_components.span,{className:`line`,children:[_jsx(_components.span,{style:{color:`#1F2328`,\"--shiki-dark\":`#9CDCFE`},children:` timer`}),_jsx(_components.span,{style:{color:`#CF222E`,\"--shiki-dark\":`#D4D4D4`},children:` =`}),_jsx(_components.span,{style:{color:`#8250DF`,\"--shiki-dark\":`#DCDCAA`},children:` setInterval`}),_jsx(_components.span,{style:{color:`#1F2328`,\"--shiki-dark\":`#D4D4D4`},children:`(`}),_jsx(_components.span,{style:{color:`#1F2328`,\"--shiki-dark\":`#9CDCFE`},children:`send`}),_jsx(_components.span,{style:{color:`#1F2328`,\"--shiki-dark\":`#D4D4D4`},children:`, `}),_jsx(_components.span,{style:{color:`#0550AE`,\"--shiki-dark\":`#4FC1FF`},children:`HEARTBEAT_MS`}),_jsx(_components.span,{style:{color:`#1F2328`,\"--shiki-dark\":`#D4D4D4`},children:`);`})]}),`\n`,_jsx(_components.span,{className:`line`,children:_jsx(_components.span,{style:{color:`#1F2328`,\"--shiki-dark\":`#D4D4D4`},children:` },`})}),`\n`,_jsxs(_components.span,{className:`line`,children:[_jsx(_components.span,{style:{color:`#8250DF`,\"--shiki-dark\":`#DCDCAA`},children:` cancel`}),_jsx(_components.span,{style:{color:`#1F2328`,\"--shiki-dark\":`#D4D4D4`},children:`() {`})]}),`\n`,_jsxs(_components.span,{className:`line`,children:[_jsx(_components.span,{style:{color:`#8250DF`,\"--shiki-dark\":`#DCDCAA`},children:` clearInterval`}),_jsx(_components.span,{style:{color:`#1F2328`,\"--shiki-dark\":`#D4D4D4`},children:`(`}),_jsx(_components.span,{style:{color:`#1F2328`,\"--shiki-dark\":`#9CDCFE`},children:`timer`}),_jsx(_components.span,{style:{color:`#1F2328`,\"--shiki-dark\":`#D4D4D4`},children:`);`})]}),`\n`,_jsxs(_components.span,{className:`line`,children:[_jsx(_components.span,{style:{color:`#8250DF`,\"--shiki-dark\":`#DCDCAA`},children:` onClosed`}),_jsx(_components.span,{style:{color:`#1F2328`,\"--shiki-dark\":`#D4D4D4`},children:`();`})]}),`\n`,_jsx(_components.span,{className:`line`,children:_jsx(_components.span,{style:{color:`#1F2328`,\"--shiki-dark\":`#D4D4D4`},children:` },`})}),`\n`,_jsx(_components.span,{className:`line`,children:_jsx(_components.span,{style:{color:`#1F2328`,\"--shiki-dark\":`#D4D4D4`},children:` });`})}),`\n`,_jsx(_components.span,{className:`line`}),`\n`,_jsx(_components.span,{className:`line`,children:_jsx(_components.span,{style:{color:`#6E7781`,\"--shiki-dark\":`#6A9955`},children:` // It is important to call waitUntil; otherwise the script may be evicted`})}),`\n`,_jsx(_components.span,{className:`line`,children:_jsx(_components.span,{style:{color:`#6E7781`,\"--shiki-dark\":`#6A9955`},children:` // while the stream is still open.`})}),`\n`,_jsxs(_components.span,{className:`line`,children:[_jsx(_components.span,{style:{color:`#1F2328`,\"--shiki-dark\":`#9CDCFE`},children:` Bunny`}),_jsx(_components.span,{style:{color:`#1F2328`,\"--shiki-dark\":`#D4D4D4`},children:`.`}),_jsx(_components.span,{style:{color:`#1F2328`,\"--shiki-dark\":`#9CDCFE`},children:`v1`}),_jsx(_components.span,{style:{color:`#1F2328`,\"--shiki-dark\":`#D4D4D4`},children:`.`}),_jsx(_components.span,{style:{color:`#8250DF`,\"--shiki-dark\":`#DCDCAA`},children:`waitUntil`}),_jsx(_components.span,{style:{color:`#1F2328`,\"--shiki-dark\":`#D4D4D4`},children:`(`}),_jsx(_components.span,{style:{color:`#1F2328`,\"--shiki-dark\":`#9CDCFE`},children:`closed`}),_jsx(_components.span,{style:{color:`#1F2328`,\"--shiki-dark\":`#D4D4D4`},children:`);`})]}),`\n`,_jsx(_components.span,{className:`line`}),`\n`,_jsxs(_components.span,{className:`line`,children:[_jsx(_components.span,{style:{color:`#CF222E`,\"--shiki-dark\":`#C586C0`},children:` return`}),_jsx(_components.span,{style:{color:`#CF222E`,\"--shiki-dark\":`#569CD6`},children:` new`}),_jsx(_components.span,{style:{color:`#8250DF`,\"--shiki-dark\":`#DCDCAA`},children:` Response`}),_jsx(_components.span,{style:{color:`#1F2328`,\"--shiki-dark\":`#D4D4D4`},children:`(`}),_jsx(_components.span,{style:{color:`#1F2328`,\"--shiki-dark\":`#9CDCFE`},children:`stream`}),_jsx(_components.span,{style:{color:`#1F2328`,\"--shiki-dark\":`#D4D4D4`},children:`, {`})]}),`\n`,_jsxs(_components.span,{className:`line`,children:[_jsx(_components.span,{style:{color:`#1F2328`,\"--shiki-dark\":`#9CDCFE`},children:` headers:`}),_jsx(_components.span,{style:{color:`#1F2328`,\"--shiki-dark\":`#D4D4D4`},children:` {`})]}),`\n`,_jsxs(_components.span,{className:`line`,children:[_jsx(_components.span,{style:{color:`#0A3069`,\"--shiki-dark\":`#CE9178`},children:` \"content-type\"`}),_jsx(_components.span,{style:{color:`#1F2328`,\"--shiki-dark\":`#9CDCFE`},children:`:`}),_jsx(_components.span,{style:{color:`#0A3069`,\"--shiki-dark\":`#CE9178`},children:` \"text/event-stream\"`}),_jsx(_components.span,{style:{color:`#1F2328`,\"--shiki-dark\":`#D4D4D4`},children:`,`})]}),`\n`,_jsxs(_components.span,{className:`line`,children:[_jsx(_components.span,{style:{color:`#0A3069`,\"--shiki-dark\":`#CE9178`},children:` \"cache-control\"`}),_jsx(_components.span,{style:{color:`#1F2328`,\"--shiki-dark\":`#9CDCFE`},children:`:`}),_jsx(_components.span,{style:{color:`#0A3069`,\"--shiki-dark\":`#CE9178`},children:` \"no-cache\"`}),_jsx(_components.span,{style:{color:`#1F2328`,\"--shiki-dark\":`#D4D4D4`},children:`,`})]}),`\n`,_jsx(_components.span,{className:`line`,children:_jsx(_components.span,{style:{color:`#1F2328`,\"--shiki-dark\":`#D4D4D4`},children:` },`})}),`\n`,_jsx(_components.span,{className:`line`,children:_jsx(_components.span,{style:{color:`#1F2328`,\"--shiki-dark\":`#D4D4D4`},children:` });`})}),`\n`,_jsx(_components.span,{className:`line`,children:_jsx(_components.span,{style:{color:`#1F2328`,\"--shiki-dark\":`#D4D4D4`},children:`});`})}),`\n`]})})}),`\n`,_jsx(Heading,{level:`2`,id:`return-the-response-before-you-wait`,children:`Return the response before you wait`}),`\n`,_jsxs(_components.p,{children:[`Return the `,_jsx(_components.code,{children:`Response`}),` at once, and let the stream fill in afterwards. A script that waits before it returns is treated as a slow origin: the edge allows 60 seconds for a response to start, and answers with `,_jsx(_components.code,{children:`504 Gateway Timeout`}),` when nothing arrives. This applies to any `,_jsx(_components.code,{children:`await`}),` before the `,_jsx(_components.code,{children:`return`}),`, and a subrequest is the common case.`]}),`\n`,_jsx(CodeBlock,{filename:``,numberOfLines:`4`,language:`typescript`,children:_jsx(_components.pre,{className:`shiki shiki-themes github-light-default dark-plus`,style:{backgroundColor:`#ffffff`,\"--shiki-dark-bg\":`#0B0C0E`,color:`#1f2328`,\"--shiki-dark\":`#D4D4D4`},language:`typescript`,children:_jsxs(_components.code,{language:`typescript`,numberOfLines:`4`,children:[_jsx(_components.span,{className:`line`,children:_jsx(_components.span,{style:{color:`#6E7781`,\"--shiki-dark\":`#6A9955`},children:`// Wrong. If the subrequest takes longer than 60 seconds, the client gets a 504`})}),`\n`,_jsx(_components.span,{className:`line`,children:_jsx(_components.span,{style:{color:`#6E7781`,\"--shiki-dark\":`#6A9955`},children:`// and never sees the stream.`})}),`\n`,_jsxs(_components.span,{className:`line`,children:[_jsx(_components.span,{style:{color:`#CF222E`,\"--shiki-dark\":`#569CD6`},children:`const`}),_jsx(_components.span,{style:{color:`#0550AE`,\"--shiki-dark\":`#4FC1FF`},children:` upstream`}),_jsx(_components.span,{style:{color:`#CF222E`,\"--shiki-dark\":`#D4D4D4`},children:` =`}),_jsx(_components.span,{style:{color:`#CF222E`,\"--shiki-dark\":`#C586C0`},children:` await`}),_jsx(_components.span,{style:{color:`#8250DF`,\"--shiki-dark\":`#DCDCAA`},children:` fetch`}),_jsx(_components.span,{style:{color:`#1F2328`,\"--shiki-dark\":`#D4D4D4`},children:`(`}),_jsx(_components.span,{style:{color:`#1F2328`,\"--shiki-dark\":`#9CDCFE`},children:`slowOrigin`}),_jsx(_components.span,{style:{color:`#1F2328`,\"--shiki-dark\":`#D4D4D4`},children:`);`})]}),`\n`,_jsxs(_components.span,{className:`line`,children:[_jsx(_components.span,{style:{color:`#CF222E`,\"--shiki-dark\":`#C586C0`},children:`return`}),_jsx(_components.span,{style:{color:`#CF222E`,\"--shiki-dark\":`#569CD6`},children:` new`}),_jsx(_components.span,{style:{color:`#8250DF`,\"--shiki-dark\":`#DCDCAA`},children:` Response`}),_jsx(_components.span,{style:{color:`#1F2328`,\"--shiki-dark\":`#D4D4D4`},children:`(`}),_jsx(_components.span,{style:{color:`#1F2328`,\"--shiki-dark\":`#9CDCFE`},children:`upstream`}),_jsx(_components.span,{style:{color:`#1F2328`,\"--shiki-dark\":`#D4D4D4`},children:`.`}),_jsx(_components.span,{style:{color:`#1F2328`,\"--shiki-dark\":`#9CDCFE`},children:`body`}),_jsx(_components.span,{style:{color:`#1F2328`,\"--shiki-dark\":`#D4D4D4`},children:`, { `}),_jsx(_components.span,{style:{color:`#1F2328`,\"--shiki-dark\":`#9CDCFE`},children:`headers`}),_jsx(_components.span,{style:{color:`#1F2328`,\"--shiki-dark\":`#D4D4D4`},children:` });`})]}),`\n`]})})}),`\n`,_jsx(CodeBlock,{filename:``,numberOfLines:`9`,language:`typescript`,children:_jsx(_components.pre,{className:`shiki shiki-themes github-light-default dark-plus`,style:{backgroundColor:`#ffffff`,\"--shiki-dark-bg\":`#0B0C0E`,color:`#1f2328`,\"--shiki-dark\":`#D4D4D4`},language:`typescript`,children:_jsxs(_components.code,{language:`typescript`,numberOfLines:`9`,children:[_jsx(_components.span,{className:`line`,children:_jsx(_components.span,{style:{color:`#6E7781`,\"--shiki-dark\":`#6A9955`},children:`// Right. The response goes back at once, and the subrequest runs inside the`})}),`\n`,_jsx(_components.span,{className:`line`,children:_jsx(_components.span,{style:{color:`#6E7781`,\"--shiki-dark\":`#6A9955`},children:`// stream, so a slow origin delays an event instead of failing the request.`})}),`\n`,_jsxs(_components.span,{className:`line`,children:[_jsx(_components.span,{style:{color:`#CF222E`,\"--shiki-dark\":`#569CD6`},children:`const`}),_jsx(_components.span,{style:{color:`#0550AE`,\"--shiki-dark\":`#4FC1FF`},children:` stream`}),_jsx(_components.span,{style:{color:`#CF222E`,\"--shiki-dark\":`#D4D4D4`},children:` =`}),_jsx(_components.span,{style:{color:`#CF222E`,\"--shiki-dark\":`#569CD6`},children:` new`}),_jsx(_components.span,{style:{color:`#8250DF`,\"--shiki-dark\":`#DCDCAA`},children:` ReadableStream`}),_jsx(_components.span,{style:{color:`#1F2328`,\"--shiki-dark\":`#D4D4D4`},children:`({`})]}),`\n`,_jsxs(_components.span,{className:`line`,children:[_jsx(_components.span,{style:{color:`#CF222E`,\"--shiki-dark\":`#569CD6`},children:` async`}),_jsx(_components.span,{style:{color:`#8250DF`,\"--shiki-dark\":`#DCDCAA`},children:` start`}),_jsx(_components.span,{style:{color:`#1F2328`,\"--shiki-dark\":`#D4D4D4`},children:`(`}),_jsx(_components.span,{style:{color:`#953800`,\"--shiki-dark\":`#9CDCFE`},children:`controller`}),_jsx(_components.span,{style:{color:`#1F2328`,\"--shiki-dark\":`#D4D4D4`},children:`) {`})]}),`\n`,_jsxs(_components.span,{className:`line`,children:[_jsx(_components.span,{style:{color:`#CF222E`,\"--shiki-dark\":`#569CD6`},children:` const`}),_jsx(_components.span,{style:{color:`#0550AE`,\"--shiki-dark\":`#4FC1FF`},children:` upstream`}),_jsx(_components.span,{style:{color:`#CF222E`,\"--shiki-dark\":`#D4D4D4`},children:` =`}),_jsx(_components.span,{style:{color:`#CF222E`,\"--shiki-dark\":`#C586C0`},children:` await`}),_jsx(_components.span,{style:{color:`#8250DF`,\"--shiki-dark\":`#DCDCAA`},children:` fetch`}),_jsx(_components.span,{style:{color:`#1F2328`,\"--shiki-dark\":`#D4D4D4`},children:`(`}),_jsx(_components.span,{style:{color:`#1F2328`,\"--shiki-dark\":`#9CDCFE`},children:`slowOrigin`}),_jsx(_components.span,{style:{color:`#1F2328`,\"--shiki-dark\":`#D4D4D4`},children:`);`})]}),`\n`,_jsx(_components.span,{className:`line`,children:_jsx(_components.span,{style:{color:`#6E7781`,\"--shiki-dark\":`#6A9955`},children:` // ... write events from the upstream body`})}),`\n`,_jsx(_components.span,{className:`line`,children:_jsx(_components.span,{style:{color:`#1F2328`,\"--shiki-dark\":`#D4D4D4`},children:` },`})}),`\n`,_jsx(_components.span,{className:`line`,children:_jsx(_components.span,{style:{color:`#1F2328`,\"--shiki-dark\":`#D4D4D4`},children:`});`})}),`\n`,_jsxs(_components.span,{className:`line`,children:[_jsx(_components.span,{style:{color:`#CF222E`,\"--shiki-dark\":`#C586C0`},children:`return`}),_jsx(_components.span,{style:{color:`#CF222E`,\"--shiki-dark\":`#569CD6`},children:` new`}),_jsx(_components.span,{style:{color:`#8250DF`,\"--shiki-dark\":`#DCDCAA`},children:` Response`}),_jsx(_components.span,{style:{color:`#1F2328`,\"--shiki-dark\":`#D4D4D4`},children:`(`}),_jsx(_components.span,{style:{color:`#1F2328`,\"--shiki-dark\":`#9CDCFE`},children:`stream`}),_jsx(_components.span,{style:{color:`#1F2328`,\"--shiki-dark\":`#D4D4D4`},children:`, { `}),_jsx(_components.span,{style:{color:`#1F2328`,\"--shiki-dark\":`#9CDCFE`},children:`headers`}),_jsx(_components.span,{style:{color:`#1F2328`,\"--shiki-dark\":`#D4D4D4`},children:` });`})]}),`\n`]})})}),`\n`,_jsx(Warning,{children:_jsx(_components.p,{children:`Write the first event as soon as the stream opens, and do not wait for the\nfirst real update. The client receives no part of the response until your\nscript writes the first byte, and this includes the response headers.`})}),`\n`,_jsx(Heading,{level:`2`,id:`keep-the-isolate-alive`,children:`Keep the isolate alive`}),`\n`,_jsxs(_components.p,{children:[`The isolate that answers a request can be evicted once the handler returns. An event stream lives longer than the handler, so pass a promise to `,_jsx(_components.a,{href:`/scripting/runtime#waituntil`,children:_jsx(_components.code,{children:`waitUntil`})}),` that resolves only when the stream ends. Without it, the script can stop in the middle of a stream.`]}),`\n`,_jsx(_components.p,{children:`Always resolve that promise when the stream closes. A promise that never settles holds the isolate open after the client has gone.`}),`\n`,_jsx(Heading,{level:`2`,id:`caching`,children:`Caching`}),`\n`,_jsxs(_components.p,{children:[`A stream that a script returns is not cached, and it needs no `,_jsx(_components.code,{children:`Cache-Control`}),` header. The edge serves every request to a standalone script from the script itself.`]}),`\n`,_jsxs(_components.p,{children:[`Set `,_jsx(_components.code,{children:`Cache-Control: no-cache`}),` anyway if the same code also runs behind a pull zone as `,_jsx(_components.a,{href:`/scripting/middleware/overview`,children:`middleware`}),`, where the normal `,_jsx(_components.a,{href:`/cdn/server-sent-events#keep-the-stream-out-of-the-cache`,children:`caching rules`}),` apply.`]}),`\n`,_jsx(Heading,{level:`2`,id:`limits`,children:`Limits`}),`\n`,_jsxs(_components.p,{children:[`The `,_jsx(_components.a,{href:`/scripting/limits`,children:`Edge Scripting limits`}),` apply, with one point to note: the 30-second CPU time limit measures processor time, and not the time a connection stays open. A stream that waits between events uses almost no CPU time. `,_jsx(_components.a,{href:`/scripting/pricing`,children:`Pricing`}),` follows the same rule.`]}),`\n`,_jsx(_components.p,{children:`Keep the work inside the stream small. A script that builds each event with a subrequest reaches the subrequest limit quickly.`}),`\n`,_jsx(Heading,{level:`2`,id:`references`,children:`References`}),`\n`,_jsxs(_components.ul,{children:[`\n`,_jsx(_components.li,{children:_jsx(_components.a,{href:`/cdn/server-sent-events`,children:`Server-Sent Events on the CDN`})}),`\n`,_jsxs(_components.li,{children:[_jsx(_components.a,{href:`/scripting/websockets`,children:`WebSockets`}),`, for two-way communication`]}),`\n`,_jsx(_components.li,{children:_jsx(_components.a,{href:`/scripting/runtime#waituntil`,children:`Runtime - waitUntil`})}),`\n`]})]})}function MDXContent(props={}){const{wrapper:MDXLayout}={..._provideComponents(),...props.components};return MDXLayout?_jsx(MDXLayout,{...props,children:_jsx(_createMdxContent,{...props})}):_createMdxContent(props)}return{default:MDXContent};function _missingMdxReference(id,component){throw new Error(`Expected `+(component?`component`:`object`)+\" `\"+id+\"` to be defined: you likely forgot to import, pass, or provide it.\")}","frontmatter":{},"scope":{"config":{},"pageMetadata":{"title":"Server-Sent Events","description":"Stream Server-Sent Events from an Edge Script, and keep the isolate alive while the connection is open.","href":"/scripting/server-sent-events"}}},"mdxSourceWithNoJs":null},"mdxExtracts":{"tableOfContents":[{"title":"Quickstart","slug":"quickstart","depth":2,"children":[]},{"title":"Return the response before you wait","slug":"return-the-response-before-you-wait","depth":2,"children":[]},{"title":"Keep the isolate alive","slug":"keep-the-isolate-alive","depth":2,"children":[]},{"title":"Caching","slug":"caching","depth":2,"children":[]},{"title":"Limits","slug":"limits","depth":2,"children":[]},{"title":"References","slug":"references","depth":2,"children":[]}],"codeExamples":{}},"jsLinks":[],"subdomain":"bunnynet-cb9733c2","siteName":"bunny.net Documentation","title":"Server-Sent Events - bunny.net Documentation","relatedPagesConfig":{"enabled":false,"limit":5,"mode":"automatic"},"navMaps":{"firstHrefInVersion":{},"firstHrefInLanguage":{}},"lastModified":"2026-08-31T11:55:14.709Z","gitLastModified":"2026-08-31T11:54:34.000Z","originalFileLocation":"scripting/server-sent-events.mdx"},"headHtml":"\u003cscript data-mint-head>(function(){var d=document.documentElement,p=null;if(!p){try{p=localStorage.getItem(\"isDarkMode\")}catch(e){}if(p==='true')p='dark';else if(p==='false')p='light';if(p!=='light'\u0026\u0026p!=='dark'\u0026\u0026p!=='system')p=\"system\"}var r=p==='system'?(matchMedia(\"(prefers-color-scheme: dark)\").matches?'dark':'light'):p;d.classList.remove(r==='dark'?'light':'dark');d.classList.add(r);d.setAttribute(\"data-theme-preference\",p)})();\u003c/script>\u003cmeta data-mint-head name=\"description\" content=\"Stream Server-Sent Events from an Edge Script, and keep the isolate alive while the connection is open.\">\u003cmeta data-mint-head name=\"application-name\" content=\"bunny.net Documentation\">\u003cmeta data-mint-head name=\"generator\" content=\"Mintlify\">\u003clink data-mint-head rel=\"icon\" type=\"image/png\" sizes=\"192x192\" href=\"/docs/mintlify-assets/_mintlify/favicons/bunnynet-cb9733c2/Zv4NOenQlgPS3TXm/_generated/favicon/android-chrome-192x192.png\" media=\"(prefers-color-scheme: light)\">\u003clink data-mint-head rel=\"apple-touch-icon\" type=\"image/png\" sizes=\"180x180\" href=\"/docs/mintlify-assets/_mintlify/favicons/bunnynet-cb9733c2/Zv4NOenQlgPS3TXm/_generated/favicon/apple-touch-icon.png\">\u003clink data-mint-head rel=\"icon\" type=\"image/png\" sizes=\"16x16\" href=\"/docs/mintlify-assets/_mintlify/favicons/bunnynet-cb9733c2/Zv4NOenQlgPS3TXm/_generated/favicon/favicon-16x16.png\" media=\"(prefers-color-scheme: light)\">\u003clink data-mint-head rel=\"icon\" type=\"image/png\" sizes=\"32x32\" href=\"/docs/mintlify-assets/_mintlify/favicons/bunnynet-cb9733c2/Zv4NOenQlgPS3TXm/_generated/favicon/favicon-32x32.png\" media=\"(prefers-color-scheme: light)\">\u003clink data-mint-head rel=\"shortcut icon\" type=\"image/x-icon\" href=\"/docs/mintlify-assets/_mintlify/favicons/bunnynet-cb9733c2/Zv4NOenQlgPS3TXm/_generated/favicon/favicon.ico\" media=\"(prefers-color-scheme: light)\">\u003clink data-mint-head rel=\"icon\" type=\"image/png\" sizes=\"192x192\" href=\"/docs/mintlify-assets/_mintlify/favicons/bunnynet-cb9733c2/Zv4NOenQlgPS3TXm/_generated/favicon-dark/android-chrome-192x192.png\" media=\"(prefers-color-scheme: dark)\">\u003clink data-mint-head rel=\"icon\" type=\"image/png\" sizes=\"16x16\" href=\"/docs/mintlify-assets/_mintlify/favicons/bunnynet-cb9733c2/Zv4NOenQlgPS3TXm/_generated/favicon-dark/favicon-16x16.png\" media=\"(prefers-color-scheme: dark)\">\u003clink data-mint-head rel=\"icon\" type=\"image/png\" sizes=\"32x32\" href=\"/docs/mintlify-assets/_mintlify/favicons/bunnynet-cb9733c2/Zv4NOenQlgPS3TXm/_generated/favicon-dark/favicon-32x32.png\" media=\"(prefers-color-scheme: dark)\">\u003clink data-mint-head rel=\"shortcut icon\" type=\"image/x-icon\" href=\"/docs/mintlify-assets/_mintlify/favicons/bunnynet-cb9733c2/Zv4NOenQlgPS3TXm/_generated/favicon-dark/favicon.ico\" media=\"(prefers-color-scheme: dark)\">\u003cmeta data-mint-head property=\"og:title\" content=\"Server-Sent Events - bunny.net Documentation\">\u003cmeta data-mint-head property=\"og:description\" content=\"Stream Server-Sent Events from an Edge Script, and keep the isolate alive while the connection is open.\">\u003cmeta data-mint-head property=\"og:site_name\" content=\"bunny.net Documentation\">\u003cmeta data-mint-head property=\"og:url\" content=\"https://bunny.net/docs/scripting/server-sent-events\">\u003cmeta data-mint-head property=\"og:type\" content=\"website\">\u003cmeta data-mint-head property=\"og:image\" content=\"https://bunnynet-cb9733c2.mintlify.app/_mintlify/api/og/v2?division=APIs\u0026amp;title=Server-Sent+Events\u0026amp;description=Stream+Server-Sent+Events+from+an+Edge+Script%2C+and+keep+the+isolate+alive+while+the+connection+is+open.\u0026amp;theme=3e3fa11ba94222b95e7f52d5\u0026amp;signature=30803feb307480c66741affb7f5b3a3cc00aab6ece311cb5dfa6001c21b9e20f\">\u003cmeta data-mint-head property=\"og:image:width\" content=\"1200\">\u003cmeta data-mint-head property=\"og:image:height\" content=\"630\">\u003cmeta data-mint-head name=\"twitter:card\" content=\"summary_large_image\">\u003cmeta data-mint-head name=\"twitter:title\" content=\"Server-Sent Events - bunny.net Documentation\">\u003cmeta data-mint-head name=\"twitter:description\" content=\"Stream Server-Sent Events from an Edge Script, and keep the isolate alive while the connection is open.\">\u003cmeta data-mint-head name=\"twitter:image\" content=\"https://bunnynet-cb9733c2.mintlify.app/_mintlify/api/og/v2?division=APIs\u0026amp;title=Server-Sent+Events\u0026amp;description=Stream+Server-Sent+Events+from+an+Edge+Script%2C+and+keep+the+isolate+alive+while+the+connection+is+open.\u0026amp;theme=3e3fa11ba94222b95e7f52d5\u0026amp;signature=30803feb307480c66741affb7f5b3a3cc00aab6ece311cb5dfa6001c21b9e20f\">\u003cmeta data-mint-head name=\"twitter:image:width\" content=\"1200\">\u003cmeta data-mint-head name=\"twitter:image:height\" content=\"630\">\u003cmeta data-mint-head name=\"msapplication-config\" content=\"/docs/mintlify-assets/_mintlify/favicons/bunnynet-cb9733c2/Zv4NOenQlgPS3TXm/_generated/favicon/browserconfig.xml\">\u003cmeta data-mint-head name=\"apple-mobile-web-app-title\" content=\"bunny.net Documentation\">\u003cmeta data-mint-head name=\"msapplication-TileColor\" content=\"#EF6126\">\u003clink data-mint-head rel=\"canonical\" href=\"https://bunny.net/docs/scripting/server-sent-events\">\u003clink data-mint-head rel=\"alternate\" type=\"application/xml\" href=\"/docs/sitemap.xml\">\u003clink data-mint-head rel=\"alternate\" type=\"text/markdown\" href=\"/docs/scripting/server-sent-events.md\">\u003cscript data-mint-head type=\"application/ld+json\">{\"@context\":\"https://schema.org\",\"@graph\":[{\"@type\":\"Organization\",\"@id\":\"https://bunny.net/#organization\",\"name\":\"bunny.net Documentation\",\"url\":\"https://bunny.net\",\"logo\":{\"@type\":\"ImageObject\",\"url\":\"https://mintcdn.com/bunnynet-cb9733c2/54-4KCQKoNVNuoyW/logo/bunny-logo-dark.svg?fit=max\u0026auto=format\u0026n=54-4KCQKoNVNuoyW\u0026q=85\u0026s=62c091ea1aaea93d0a981902b7783e37\"}},{\"@type\":\"WebSite\",\"@id\":\"https://bunny.net/docs#website\",\"name\":\"bunny.net Documentation\",\"url\":\"https://bunny.net/docs\",\"publisher\":{\"@id\":\"https://bunny.net/#organization\"}},{\"@type\":\"WebPage\",\"@id\":\"https://bunny.net/docs/scripting/server-sent-events#webpage\",\"url\":\"https://bunny.net/docs/scripting/server-sent-events\",\"name\":\"Server-Sent Events\",\"description\":\"Stream Server-Sent Events from an Edge Script, and keep the isolate alive while the connection is open.\",\"dateModified\":\"2026-08-31T11:54:34.000Z\",\"isPartOf\":{\"@id\":\"https://bunny.net/docs#website\"},\"breadcrumb\":{\"@id\":\"https://bunny.net/docs/scripting/server-sent-events#breadcrumb\"}},{\"@type\":\"BreadcrumbList\",\"@id\":\"https://bunny.net/docs/scripting/server-sent-events#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"APIs\",\"item\":\"https://bunny.net/docs/scripting/runtime\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"Server-Sent Events\",\"item\":\"https://bunny.net/docs/scripting/server-sent-events\"}]},{\"@type\":[\"Article\",\"TechArticle\"],\"@id\":\"https://bunny.net/docs/scripting/server-sent-events#article\",\"headline\":\"Server-Sent Events\",\"name\":\"Server-Sent Events\",\"description\":\"Stream Server-Sent Events from an Edge Script, and keep the isolate alive while the connection is open.\",\"url\":\"https://bunny.net/docs/scripting/server-sent-events\",\"mainEntityOfPage\":{\"@id\":\"https://bunny.net/docs/scripting/server-sent-events#webpage\"},\"dateModified\":\"2026-08-31T11:54:34.000Z\",\"publisher\":{\"@id\":\"https://bunny.net/#organization\"},\"isPartOf\":{\"@id\":\"https://bunny.net/docs#website\"}}]}\u003c/script>\u003cscript data-mint-head type=\"application/ld+json\">{\"@context\":\"https://schema.org\",\"@type\":\"WebSite\",\"name\":\"bunny.net Documentation\",\"creator\":{\"@type\":\"Organization\",\"name\":\"Mintlify\",\"url\":\"https://mintlify.com\"}}\u003c/script>","siteName":"bunny.net Documentation","nav":{"global":null,"products":[{"product":"Home","icon":"book-open","pages":[{"title":"Home","href":""}]},{"product":"CDN","icon":"zap","description":"Accelerate and protect your content globally","pages":[{"title":"CDN","href":"/cdn"}]},{"product":"Magic Containers","icon":"box","description":"Deploy any app anywhere with Docker","pages":[{"title":"Magic Containers","href":"/magic-containers"}]},{"product":"Edge Scripting","icon":"code","description":"Deploy serverless code at the edge","tabs":[{"tab":"Documentation","groups":[{"group":"Documentation","pages":[{"title":"Bunny Edge Scripting","sidebarTitle":"Introduction","mode":"wide","href":"/scripting"},{"title":"Quickstart","href":"/scripting/quickstart"},{"title":"Pricing","href":"/scripting/pricing"},{"title":"Bunny Edge Scripting Changelog","sidebarTitle":"Changelog","url":"/changelog?tags=Edge%20Scripting","href":"/scripting/changelog"}]},{"group":"Script Types","pages":[{"group":"Standalone","pages":[{"title":"Standalone scripts","sidebarTitle":"Introduction","href":"/scripting/standalone/overview"},{"group":"Examples","pages":[{"title":"Fetch url","href":"/scripting/standalone/examples/fetch-url"},{"title":"Return JSON","href":"/scripting/standalone/examples/return-json"},{"title":"Return HTML","href":"/scripting/standalone/examples/return-html"},{"title":"Send an email","href":"/scripting/standalone/examples/send-email"},{"title":"Redirect to another domain","href":"/scripting/standalone/examples/redirect-domain"},{"title":"Weighted traffic splitting","href":"/scripting/standalone/examples/weighted-routing"},{"title":"Route by country","href":"/scripting/standalone/examples/geo-routing"}]}]},{"group":"Middleware","pages":[{"title":"Middleware scripts","sidebarTitle":"Introduction","href":"/scripting/middleware/overview"},{"group":"Examples","pages":[{"title":"Modify HTTP headers","href":"/scripting/middleware/examples/modify-headers"},{"title":"Modify response body (Middleware)","href":"/scripting/middleware/examples/modify-body"},{"title":"Protect cached content with middleware","sidebarTitle":"Protect cached content","href":"/scripting/middleware/examples/protect-cached-content"}]}]}]},{"group":"Deployment","pages":[{"title":"Deployments","href":"/scripting/deployments"},{"title":"Deploying your scripts with GitHub Actions","href":"/scripting/github-actions"},{"title":"GitHub Integration","href":"/scripting/github-integration"},{"title":"Environment Variables","href":"/scripting/environment-variables"},{"title":"Secrets","href":"/scripting/secrets"}]},{"group":"Observability","pages":[{"title":"Statistics","href":"/scripting/statistics"},{"title":"Logs","href":"/scripting/logs"}]},{"group":"APIs","pages":[{"title":"Runtime","href":"/scripting/runtime"},{"group":"Cache API","pages":[{"title":"Cache API","sidebarTitle":"Overview","href":"/scripting/cache/index"},{"title":"Managing caches","sidebarTitle":"Managing caches","href":"/scripting/cache/managing-caches"},{"title":"Reading \u0026 writing","sidebarTitle":"Reading \u0026 writing","href":"/scripting/cache/reading-writing"},{"title":"Cache examples","sidebarTitle":"Examples","href":"/scripting/cache/examples"}]},{"title":"HTMLRewriter","sidebarTitle":"HTMLRewriter","href":"/scripting/html-rewriter"},{"title":"Node:FS","sidebarTitle":"node:fs","href":"/scripting/node-fs"},{"title":"Node:TLS","sidebarTitle":"node:tls","href":"/scripting/node-tls"},{"title":"WebSockets","description":"Use WebSockets in an Edge Script for two-way, low-latency communication between your script and connected clients.","href":"/scripting/websockets"},{"title":"Server-Sent Events","href":"/scripting/server-sent-events"}]},{"group":"Reference","pages":[{"title":"Limits","description":"Execution limits for Edge Scripts, covering CPU time, memory, subrequests, script size, and environment variables.","href":"/scripting/limits"},{"title":"Edge Scripting Regions","sidebarTitle":"Regions and PoPs","href":"/scripting/regions"},{"title":"HTTPS \u0026 SSL Certificates","href":"/scripting/https-ssl-certificates"},{"title":"Before cache execution","sidebarTitle":"Before cache execution","href":"/scripting/before-cache"},{"title":"API Reference","url":"/api-reference/scripting","href":"/scripting/api-reference"}]}]}]},{"product":"Stream","icon":"video","description":"Video streaming and delivery platform","pages":[{"title":"Stream","href":"/stream"}]},{"product":"Storage","icon":"hard-drive","description":"Global object storage","pages":[{"title":"Storage","href":"/storage"}]},{"product":"Shield","icon":"shield","description":"Stay protected and online no matter what","pages":[{"title":"Shield","href":"/shield"}]},{"product":"Optimizer","icon":"sparkles","description":"Automatic image and web optimization.","pages":[{"title":"Optimizer","href":"/optimizer"}]},{"product":"DNS","icon":"globe","description":"Unlock the true power of DNS with an ultra-fast scriptable platform","pages":[{"title":"DNS","href":"/dns"}]},{"product":"Database","icon":"database","description":"Serverless SQLite over HTTP","pages":[{"title":"Database","href":"/database"}]},{"product":"Integrations","icon":"puzzle","description":"Official and third-party plugins and integrations","pages":[{"title":"Integrations","href":"/integrations/wordpress"}]},{"product":"CLI","icon":"terminal","description":"Manage bunny.net from your terminal","pages":[{"title":"CLI","href":"/cli"}]},{"product":"API Reference","icon":"square-terminal","description":"Build \u0026 Integrate with the Platform API","pages":[{"title":"API Reference","href":"/api-reference"}]},{"product":"Partners","icon":"handshake","description":"Manage customers, pricing, and billing as a bunny.net partner","pages":[{"title":"Partners","href":"/partners"}]}]},"basePath":"","title":"Server-Sent Events - bunny.net Documentation","buildId":"dpl_4RHMRqtvdHvKgaEfTTjFqjzja3B6","deploymentId":"dpl_4RHMRqtvdHvKgaEfTTjFqjzja3B6","dynamics":[],"chunks":[]}