-
Notifications
You must be signed in to change notification settings - Fork 4
Add before cache documentation #288
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
ee5e15e
docs: add before cache middleware hooks and workflow diagram
antho-bunny 0a1a2a1
docs: add before cache execution page with self-serve enablement steps
amir-at-bunny ff725cf
docs(middleware): clarify conditional execution of request handlers
antho-bunny 1a55e5e
docs(scripting): update SDK import to pin version 0.13.0-rc.0
antho-bunny File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,85 @@ | ||
| --- | ||
| 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." | ||
| tag: Preview | ||
| --- | ||
|
|
||
| By default, edge scripts run **after** the cache. Cached responses are served | ||
| directly by the CDN without invoking your script, which keeps cached traffic as | ||
| fast and efficient as possible. | ||
|
|
||
| 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. | ||
|
|
||
| ## Enable before cache execution | ||
|
|
||
| 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. | ||
|
|
||
| ## Middleware scripts | ||
|
|
||
| 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. | ||
|
|
||
| ```ts | ||
| import * as BunnySDK from "@bunny.net/edgescript-sdk@0.13.0-rc.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); | ||
| }); | ||
| ``` | ||
|
|
||
| See [Middleware scripts](/scripting/middleware/overview) for the full hook | ||
| reference and workflow. | ||
|
|
||
| ## Standalone scripts | ||
|
|
||
| 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](/scripting/standalone/overview) for details. | ||
|
|
||
| ## Workflow | ||
|
|
||
| ```mermaid | ||
| sequenceDiagram | ||
| participant Client | ||
| participant Script | ||
| participant Cache | ||
| participant Origin | ||
|
|
||
| Client->>Script: Request | ||
| Script->>Cache: Cache lookup | ||
| Cache->>Origin: On cache MISS | ||
| Origin-->>Cache: Response | ||
| Cache-->>Script: Response | ||
| Script-->>Client: Response | ||
| ``` |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
nice