Run your edge scripts before the cache layer to take control of every request, instead of only executing when the cache is missed.
By default, edge scripts run after the cache. The CDN serves cached
responses directly, without invoking your script, so cached traffic is not
slowed down by script execution.With before cache execution enabled, your script runs on every request,
before the cache lookup. This gives you full control over the request flow at
the cost of executing your script even for cached content.Post-cache execution remains the default. Enable before cache execution when
you need per-request logic, such as authentication, tenant-aware routing, or
custom caching rules.
Overriding the default handling of /.well-known/* paths in your script can
break ACME challenges and prevent automated certificate renewal.
Before cache execution is enabled at the Pull Zone level and applies to the
script linked to that Pull Zone.Navigate to your Pull Zone, then go to General > Origin and enable
Run script before cache.For standalone scripts, you can use the same Pull Zone toggle or enable it
directly in the script settings.With the Update Pull Zone
API, set EdgeScriptExecutionPhase to 2 to enable before cache execution.
The default value, 0, runs the script after the cache.
To block requests to cached content, you must enable before cache execution.
In the default mode, the CDN serves a cache HIT without the script. A check
that runs only on a cache MISS does not protect content that is already
cached.For a complete, tested example, see
Protect cached content.
When before cache execution is enabled, middleware scripts gain access to two
additional hooks that run on the client side of the cache:
onClientRequest: runs on every incoming request before the cache
lookup. Modify the request or short-circuit by returning a response
directly.
onClientResponse: runs just before the response is sent to the
client, including responses served from the cache.
If before cache execution is off, these hooks do not run, and no error shows.
import * as BunnySDK from "@bunny.net/edgescript-sdk@0.13.0";BunnySDK.net.http .servePullZone({ url: "https://your-origin.com" }) .onClientRequest((ctx) => { // Runs before the cache lookup on every request return Promise.resolve(ctx.request); }) .onClientResponse((ctx) => { // Runs before the response is sent to the client return Promise.resolve(ctx.response); });
Standalone scripts act as the origin for their Pull Zone, so by default they
only execute when the cache is missed. With before cache execution enabled,
your script handles every request before the cache lookup, and decides
whether and how responses are cached.See Standalone scripts for details.
{"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":"6ae534f2b9a26f131eb41834","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}],"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/before-cache","slug":"/scripting/before-cache","pageMetadata":{"title":"Before cache execution","sidebarTitle":"Before cache execution","description":"Run your edge scripts before the cache layer to take control of every request, instead of only executing when the cache is missed.","href":"/scripting/before-cache"},"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: \"Run your edge scripts before the cache layer to take control of every request, instead of only executing when the cache is missed.\"\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`,strong:`strong`,ul:`ul`,..._provideComponents(),...props.components},{CodeBlock,Heading,Mermaid,Warning}=_components;if(!CodeBlock)_missingMdxReference(`CodeBlock`,true);if(!Heading)_missingMdxReference(`Heading`,true);if(!Mermaid)_missingMdxReference(`Mermaid`,true);if(!Warning)_missingMdxReference(`Warning`,true);return _jsxs(_Fragment,{children:[_jsxs(_components.p,{children:[`By default, edge scripts run `,_jsx(_components.strong,{children:`after`}),` the cache. The CDN serves cached\nresponses directly, without invoking your script, so cached traffic is not\nslowed down by script execution.`]}),`\n`,_jsxs(_components.p,{children:[`With before cache execution enabled, your script runs on `,_jsx(_components.strong,{children:`every request`}),`,\nbefore the cache lookup. This gives you full control over the request flow at\nthe cost of executing your script even for cached content.`]}),`\n`,_jsx(_components.p,{children:`Post-cache execution remains the default. Enable before cache execution when\nyou need per-request logic, such as authentication, tenant-aware routing, or\ncustom caching rules.`}),`\n`,_jsx(Warning,{children:_jsxs(_components.p,{children:[`Overriding the default handling of `,_jsx(_components.code,{children:`/.well-known/*`}),` paths in your script can\nbreak ACME challenges and prevent automated certificate renewal.`]})}),`\n`,_jsx(Heading,{level:`2`,id:`enable-before-cache-execution`,children:`Enable before cache execution`}),`\n`,_jsx(_components.p,{children:`Before cache execution is enabled at the Pull Zone level and applies to the\nscript linked to that Pull Zone.`}),`\n`,_jsxs(_components.p,{children:[`Navigate to your Pull Zone, then go to `,_jsx(_components.strong,{children:`General`}),` > `,_jsx(_components.strong,{children:`Origin`}),` and enable\n`,_jsx(_components.strong,{children:`Run script before cache`}),`.`]}),`\n`,_jsx(_components.p,{children:`For standalone scripts, you can use the same Pull Zone toggle or enable it\ndirectly in the script settings.`}),`\n`,_jsxs(_components.p,{children:[`With the `,_jsx(_components.a,{href:`/api-reference/core/pull-zone/update-pull-zone`,children:`Update Pull Zone`}),`\nAPI, set `,_jsx(_components.code,{children:`EdgeScriptExecutionPhase`}),` to `,_jsx(_components.code,{children:`2`}),` to enable before cache execution.\nThe default value, `,_jsx(_components.code,{children:`0`}),`, runs the script after the cache.`]}),`\n`,_jsx(Heading,{level:`2`,id:`access-control`,children:`Access control`}),`\n`,_jsx(_components.p,{children:`To block requests to cached content, you must enable before cache execution.\nIn the default mode, the CDN serves a cache HIT without the script. A check\nthat runs only on a cache MISS does not protect content that is already\ncached.`}),`\n`,_jsxs(_components.p,{children:[`For a complete, tested example, see\n`,_jsx(_components.a,{href:`/scripting/middleware/examples/protect-cached-content`,children:`Protect cached content`}),`.`]}),`\n`,_jsx(Heading,{level:`2`,id:`middleware-scripts`,children:`Middleware scripts`}),`\n`,_jsx(_components.p,{children:`When before cache execution is enabled, middleware scripts gain access to two\nadditional hooks that run on the client side of the cache:`}),`\n`,_jsxs(_components.ul,{children:[`\n`,_jsxs(_components.li,{children:[_jsx(_components.strong,{children:_jsx(_components.code,{children:`onClientRequest`})}),`: runs on every incoming request before the cache\nlookup. Modify the request or short-circuit by returning a response\ndirectly.`]}),`\n`,_jsxs(_components.li,{children:[_jsx(_components.strong,{children:_jsx(_components.code,{children:`onClientResponse`})}),`: runs just before the response is sent to the\nclient, including responses served from the cache.`]}),`\n`]}),`\n`,_jsx(_components.p,{children:`If before cache execution is off, these hooks do not run, and no error shows.`}),`\n`,_jsx(CodeBlock,{filename:``,numberOfLines:`12`,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:`12`,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@0.13.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:`#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`})]}),`\n`,_jsxs(_components.span,{className:`line`,children:[_jsx(_components.span,{style:{color:`#1F2328`,\"--shiki-dark\":`#D4D4D4`},children:` .`}),_jsx(_components.span,{style:{color:`#8250DF`,\"--shiki-dark\":`#DCDCAA`},children:`servePullZone`}),_jsx(_components.span,{style:{color:`#1F2328`,\"--shiki-dark\":`#D4D4D4`},children:`({ `}),_jsx(_components.span,{style:{color:`#1F2328`,\"--shiki-dark\":`#9CDCFE`},children:`url:`}),_jsx(_components.span,{style:{color:`#0A3069`,\"--shiki-dark\":`#CE9178`},children:` \"https://your-origin.com\"`}),_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\":`#D4D4D4`},children:` .`}),_jsx(_components.span,{style:{color:`#8250DF`,\"--shiki-dark\":`#DCDCAA`},children:`onClientRequest`}),_jsx(_components.span,{style:{color:`#1F2328`,\"--shiki-dark\":`#D4D4D4`},children:`((`}),_jsx(_components.span,{style:{color:`#953800`,\"--shiki-dark\":`#9CDCFE`},children:`ctx`}),_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`,_jsx(_components.span,{className:`line`,children:_jsx(_components.span,{style:{color:`#6E7781`,\"--shiki-dark\":`#6A9955`},children:` // Runs before the cache lookup on every request`})}),`\n`,_jsxs(_components.span,{className:`line`,children:[_jsx(_components.span,{style:{color:`#CF222E`,\"--shiki-dark\":`#C586C0`},children:` return`}),_jsx(_components.span,{style:{color:`#0550AE`,\"--shiki-dark\":`#4EC9B0`},children:` Promise`}),_jsx(_components.span,{style:{color:`#1F2328`,\"--shiki-dark\":`#D4D4D4`},children:`.`}),_jsx(_components.span,{style:{color:`#8250DF`,\"--shiki-dark\":`#DCDCAA`},children:`resolve`}),_jsx(_components.span,{style:{color:`#1F2328`,\"--shiki-dark\":`#D4D4D4`},children:`(`}),_jsx(_components.span,{style:{color:`#1F2328`,\"--shiki-dark\":`#9CDCFE`},children:`ctx`}),_jsx(_components.span,{style:{color:`#1F2328`,\"--shiki-dark\":`#D4D4D4`},children:`.`}),_jsx(_components.span,{style:{color:`#1F2328`,\"--shiki-dark\":`#9CDCFE`},children:`request`}),_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:`#1F2328`,\"--shiki-dark\":`#D4D4D4`},children:` .`}),_jsx(_components.span,{style:{color:`#8250DF`,\"--shiki-dark\":`#DCDCAA`},children:`onClientResponse`}),_jsx(_components.span,{style:{color:`#1F2328`,\"--shiki-dark\":`#D4D4D4`},children:`((`}),_jsx(_components.span,{style:{color:`#953800`,\"--shiki-dark\":`#9CDCFE`},children:`ctx`}),_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`,_jsx(_components.span,{className:`line`,children:_jsx(_components.span,{style:{color:`#6E7781`,\"--shiki-dark\":`#6A9955`},children:` // Runs before the response is sent to the client`})}),`\n`,_jsxs(_components.span,{className:`line`,children:[_jsx(_components.span,{style:{color:`#CF222E`,\"--shiki-dark\":`#C586C0`},children:` return`}),_jsx(_components.span,{style:{color:`#0550AE`,\"--shiki-dark\":`#4EC9B0`},children:` Promise`}),_jsx(_components.span,{style:{color:`#1F2328`,\"--shiki-dark\":`#D4D4D4`},children:`.`}),_jsx(_components.span,{style:{color:`#8250DF`,\"--shiki-dark\":`#DCDCAA`},children:`resolve`}),_jsx(_components.span,{style:{color:`#1F2328`,\"--shiki-dark\":`#D4D4D4`},children:`(`}),_jsx(_components.span,{style:{color:`#1F2328`,\"--shiki-dark\":`#9CDCFE`},children:`ctx`}),_jsx(_components.span,{style:{color:`#1F2328`,\"--shiki-dark\":`#D4D4D4`},children:`.`}),_jsx(_components.span,{style:{color:`#1F2328`,\"--shiki-dark\":`#9CDCFE`},children:`response`}),_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`,_jsxs(_components.p,{children:[`See `,_jsx(_components.a,{href:`/scripting/middleware/overview`,children:`Middleware scripts`}),` for the full hook\nreference and workflow.`]}),`\n`,_jsx(Heading,{level:`2`,id:`standalone-scripts`,children:`Standalone scripts`}),`\n`,_jsxs(_components.p,{children:[`Standalone scripts act as the origin for their Pull Zone, so by default they\nonly execute when the cache is missed. With before cache execution enabled,\nyour script handles `,_jsx(_components.strong,{children:`every request`}),` before the cache lookup, and decides\nwhether and how responses are cached.`]}),`\n`,_jsxs(_components.p,{children:[`See `,_jsx(_components.a,{href:`/scripting/standalone/overview`,children:`Standalone scripts`}),` for details.`]}),`\n`,_jsx(Heading,{level:`2`,id:`workflow`,children:`Workflow`}),`\n`,_jsx(Heading,{level:`3`,id:`default`,children:`Default`}),`\n`,_jsx(_components.p,{children:`The script runs only on a cache MISS.`}),`\n`,_jsx(Mermaid,{chart:`sequenceDiagram\n participant Client\n participant Cache\n participant Script\n participant Origin\n\n Client->>Cache: Request\n alt Cache HIT\n Cache-->>Client: Cached response\n else Cache MISS\n Cache->>Script: Request\n Script->>Origin: Request\n Origin-->>Script: Response\n Script-->>Cache: Response\n Cache-->>Client: Response\n end`}),`\n`,_jsx(Heading,{level:`3`,id:`before-cache-execution`,children:`Before cache execution`}),`\n`,_jsx(_components.p,{children:`The script runs on every request, before the cache lookup.`}),`\n`,_jsx(Mermaid,{chart:`sequenceDiagram\n participant Client\n participant Script\n participant Cache\n participant Origin\n\n Client->>Script: Request\n Script->>Cache: Cache lookup\n Cache->>Origin: On cache MISS\n Origin-->>Cache: Response\n Cache-->>Script: Response\n Script-->>Client: Response`})]})}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":"Before cache execution","sidebarTitle":"Before cache execution","description":"Run your edge scripts before the cache layer to take control of every request, instead of only executing when the cache is missed.","href":"/scripting/before-cache"}}},"mdxSourceWithNoJs":null},"mdxExtracts":{"tableOfContents":[{"title":"Enable before cache execution","slug":"enable-before-cache-execution","depth":2,"children":[]},{"title":"Access control","slug":"access-control","depth":2,"children":[]},{"title":"Middleware scripts","slug":"middleware-scripts","depth":2,"children":[]},{"title":"Standalone scripts","slug":"standalone-scripts","depth":2,"children":[]},{"title":"Workflow","slug":"workflow","depth":2,"children":[{"title":"Default","slug":"default","depth":3,"children":[]},{"title":"Before cache execution","slug":"before-cache-execution","depth":3,"children":[]}]}],"codeExamples":{}},"jsLinks":[],"subdomain":"bunnynet-cb9733c2","siteName":"bunny.net Documentation","title":"Before cache execution - bunny.net Documentation","relatedPagesConfig":{"enabled":false,"limit":5,"mode":"automatic"},"navMaps":{"firstHrefInVersion":{},"firstHrefInLanguage":{}},"lastModified":"2026-10-28T10:55:28.750Z","gitLastModified":"2026-10-28T10:54:56.000Z","originalFileLocation":"scripting/before-cache.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=\"Run your edge scripts before the cache layer to take control of every request, instead of only executing when the cache is missed.\">\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=\"Before cache execution - bunny.net Documentation\">\u003cmeta data-mint-head property=\"og:description\" content=\"Run your edge scripts before the cache layer to take control of every request, instead of only executing when the cache is missed.\">\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/before-cache\">\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=Reference\u0026amp;title=Before+cache+execution\u0026amp;description=Run+your+edge+scripts+before+the+cache+layer+to+take+control+of+every+request%2C+instead+of+only+executing+when+the+cache+is+missed.\u0026amp;theme=3e3fa11ba94222b95e7f52d5\u0026amp;signature=8144c9189e50252022d35b14b3edf0a635e051311ede6cf026ce21b66de7df68\">\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=\"Before cache execution - bunny.net Documentation\">\u003cmeta data-mint-head name=\"twitter:description\" content=\"Run your edge scripts before the cache layer to take control of every request, instead of only executing when the cache is missed.\">\u003cmeta data-mint-head name=\"twitter:image\" content=\"https://bunnynet-cb9733c2.mintlify.app/_mintlify/api/og/v2?division=Reference\u0026amp;title=Before+cache+execution\u0026amp;description=Run+your+edge+scripts+before+the+cache+layer+to+take+control+of+every+request%2C+instead+of+only+executing+when+the+cache+is+missed.\u0026amp;theme=3e3fa11ba94222b95e7f52d5\u0026amp;signature=8144c9189e50252022d35b14b3edf0a635e051311ede6cf026ce21b66de7df68\">\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/before-cache\">\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/before-cache.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/before-cache#webpage\",\"url\":\"https://bunny.net/docs/scripting/before-cache\",\"name\":\"Before cache execution\",\"description\":\"Run your edge scripts before the cache layer to take control of every request, instead of only executing when the cache is missed.\",\"dateModified\":\"2026-10-28T10:54:56.000Z\",\"isPartOf\":{\"@id\":\"https://bunny.net/docs#website\"},\"breadcrumb\":{\"@id\":\"https://bunny.net/docs/scripting/before-cache#breadcrumb\"}},{\"@type\":\"BreadcrumbList\",\"@id\":\"https://bunny.net/docs/scripting/before-cache#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Reference\",\"item\":\"https://bunny.net/docs/scripting/limits\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"Before cache execution\",\"item\":\"https://bunny.net/docs/scripting/before-cache\"}]},{\"@type\":[\"Article\",\"TechArticle\"],\"@id\":\"https://bunny.net/docs/scripting/before-cache#article\",\"headline\":\"Before cache execution\",\"name\":\"Before cache execution\",\"description\":\"Run your edge scripts before the cache layer to take control of every request, instead of only executing when the cache is missed.\",\"url\":\"https://bunny.net/docs/scripting/before-cache\",\"mainEntityOfPage\":{\"@id\":\"https://bunny.net/docs/scripting/before-cache#webpage\"},\"dateModified\":\"2026-10-28T10:54:56.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","href":"/scripting/websockets"},{"title":"Server-Sent Events","href":"/scripting/server-sent-events"}]},{"group":"Reference","pages":[{"title":"Limits","href":"/scripting/limits"},{"title":"Edge Scripting Regions","sidebarTitle":"Regions and PoPs","href":"/scripting/regions"},{"title":"HTTPS \u0026 SSL Certificates","description":"How Edge Scripts verify TLS certificates when fetching external URLs and Pull Zone origins, and how to fix common certificate errors.","href":"/scripting/https-ssl-certificates"},{"title":"Before cache execution","sidebarTitle":"Before cache execution","href":"/scripting/before-cache"},{"title":"API Reference","description":"Complete API reference for deploying and managing Edge Scripts programmatically.","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":"Before cache execution - bunny.net Documentation","buildId":"dpl_4RHMRqtvdHvKgaEfTTjFqjzja3B6","deploymentId":"dpl_4RHMRqtvdHvKgaEfTTjFqjzja3B6","dynamics":[],"chunks":[]}