- Origin hooks (
onOriginRequest,onOriginResponse) run between the cache and your origin. They run only on a cache MISS. This is the default. - Client hooks (
onClientRequest,onClientResponse) run between the client and the cache. They run on every request, including cache hits. To use them, enable Run script before cache in your Pull Zone, under General > Origin. See before cache execution.
Choose a hook
The servePullZone function
The servePullZone function creates a middleware handler that integrates with your Pull Zone. It returns a chainable object for adding request and response middleware.
Function signature
The
url option is only used during local development. When deployed to
bunny.net, requests are proxied to the origin configured in your Pull Zone
settings.Middleware methods
TheservePullZone function returns a PullZoneHandler object with chainable middleware methods:
onClientRequest
This hook runs only when your Pull Zone has
before cache execution enabled. If it is off, the
hook does not run, and no error shows.
Return value:
- Return
Promise<Request>to continue to the cache lookup with the (modified) request. On a cache MISS, the request then goes to the origin. - Return
Promise<Response>to short-circuit and respond immediately without hitting the cache.onClientResponsedoes not run for this response.
onClientResponse
This hook runs only when your Pull Zone has
before cache execution enabled.
Return value:
- Return
Promise<Response>orResponsewith the (modified) response to send to the client.
onOriginRequest
Intercepts requests before they are sent to the origin server. It runs only on a cache MISS. You can modify the request or short-circuit by returning a response directly.
Return value:
- Return
Promise<Request>to continue to the origin with the (modified) request - Return
Promise<Response>to short-circuit and respond immediately without hitting the origin. The CDN handles this response like an origin response, so it can be cached.
onOriginResponse
Intercepts responses from the origin server before they are sent to the client. Modifications occur before the response is cached.
Return value:
- Return
Promise<Response>with the (modified) response to send to the client
Enable before cache scripts
Before cache execution is turned on per Pull Zone. Navigate to your Pull Zone, then go to General > Origin and enable Run script before cache.Learn more about before cache execution.
Workflow
When a client makes a request to a Pull Zone, the request passes through middleware at different stages:onClientRequest and onClientResponse run only when your Pull Zone has
before cache execution enabled.onClientRequest- Called before the request is sent to the cache. Modify the request or return a response to short-circuit.onOriginRequest- Called before the request is sent to the origin, so only when the cache returns a MISS. Modify the request or return a response to short-circuit.- Origin fetch - The request is sent to your origin server.
onOriginResponse- Called after the origin responds. Modify the response before it’s sent to the client and cached.onClientResponse- Called just before a response is sent to the client, unless you short-circuited at theonClientRequestlayer.
Example
This example gates a route behind a feature flag, and adds a custom header to responses from the origin. The flag check runs inonClientRequest, so it also
applies to cached responses. Enable before cache execution to run it.