### Install Dependencies with Bun Source: https://github.com/johnny-woodtke/elysiajs-cdn-cache/blob/main/example/README.md Installs project dependencies using the Bun package manager. This is a prerequisite for running the development servers. ```bash bun install ``` -------------------------------- ### Quick Start: Basic Cache Control Setup in ElysiaJS Source: https://github.com/johnny-woodtke/elysiajs-cdn-cache/blob/main/README.md A quick start guide demonstrating how to integrate the elysiajs-cdn-cache plugin into an ElysiaJS application. It shows how to set `Cache-Control` headers with `public`, `max-age`, and `s-maxage` directives for a route. ```typescript import { Elysia } from "elysia"; import { cacheControl, CacheControl } from "elysiajs-cdn-cache"; const app = new Elysia() .use(cacheControl("Cache-Control")) .get("/", ({ cacheControl }) => { // Set cache headers cacheControl.set( "Cache-Control", new CacheControl() .set("public", true) .set("max-age", 3600) .set("s-maxage", 7200) ); return { message: "Cached response!", timestamp: Date.now() }; }) .listen(3000); ``` -------------------------------- ### Start Development Servers with Bun Source: https://github.com/johnny-woodtke/elysiajs-cdn-cache/blob/main/example/README.md Starts the Elysia API server and the Next.js frontend development server using Bun scripts. Recommended to run both simultaneously for full application testing. ```bash # Terminal 1 - API Server bun run dev:api # Terminal 2 - Next.js Frontend bun run dev:next ``` -------------------------------- ### GET /api/all Source: https://github.com/johnny-woodtke/elysiajs-cdn-cache/blob/main/example/README.md This endpoint demonstrates aggressive public caching, suitable for static or rarely changing bulk data that can be heavily cached across multiple layers. ```APIDOC ## GET /api/all ### Description This endpoint demonstrates aggressive public caching, suitable for static or rarely changing bulk data that can be heavily cached across multiple layers. ### Method GET ### Endpoint /api/all ### Parameters #### Query Parameters None #### Request Body None ### Response #### Success Response (200) - **Cache-Control** (string) - Aggressive public caching directives (`public, s-maxage=120, max-age=60`) ### Response Example ``` { "items": ["item1", "item2", ...] } ``` ### Cache Headers ```typescript Cache-Control: public, s-maxage=120, max-age=60 ``` - **Browser cache**: 60 seconds - **CDN cache**: 120 seconds ``` -------------------------------- ### GET /api Source: https://github.com/johnny-woodtke/elysiajs-cdn-cache/blob/main/example/README.md This endpoint demonstrates public caching with CDN optimization. It is suitable for general API responses that can be cached by browsers and CDNs. ```APIDOC ## GET /api ### Description This endpoint demonstrates public caching with CDN optimization. It is suitable for general API responses that can be cached by browsers and CDNs. ### Method GET ### Endpoint /api ### Parameters #### Query Parameters None #### Request Body None ### Response #### Success Response (200) - **Cache-Control** (string) - Public caching directives (`public, s-maxage=120, max-age=60`) ### Response Example ``` { "message": "Publicly cached data" } ``` ### Cache Headers ```typescript Cache-Control: public, s-maxage=120, max-age=60 ``` - **Browser cache**: 60 seconds - **CDN cache**: 120 seconds ``` -------------------------------- ### Install elysiajs-cdn-cache Plugin Source: https://github.com/johnny-woodtke/elysiajs-cdn-cache/blob/main/README.md Instructions for installing the elysiajs-cdn-cache plugin using various package managers like npm, bun, yarn, and pnpm. ```bash # Using npm npm install elysiajs-cdn-cache # Using bun bun add elysiajs-cdn-cache # Using yarn yarn add elysiajs-cdn-cache # Using pnpm pnpm add elysiajs-cdn-cache ``` -------------------------------- ### Test Bulk Data Endpoint Cache with curl Source: https://github.com/johnny-woodtke/elysiajs-cdn-cache/blob/main/example/README.md Uses curl to fetch the response headers for the bulk data endpoint, confirming the aggressive public caching strategy. ```bash curl -I https://elysiajs-cdn-cache.vercel.app/api/all ``` -------------------------------- ### CacheControl.set() Method Examples (TypeScript) Source: https://context7.com/johnny-woodtke/elysiajs-cdn-cache/llms.txt Provides practical examples of using the `CacheControl.set()` method to configure various cache directives. It demonstrates setting boolean and numeric directives for different caching scenarios, including static assets, API responses, private content, and no-caching policies. ```typescript import { CacheControl } from "elysiajs-cdn-cache"; // Static assets - long-term immutable caching const staticCache = new CacheControl() .set("public", true) .set("max-age", 31536000) // 1 year .set("immutable", true); console.log(staticCache.toString()); // "public, max-age=31536000, immutable" // API responses with stale-while-revalidate const apiCache = new CacheControl() .set("public", true) .set("max-age", 60) .set("stale-while-revalidate", 300) .set("stale-if-error", 600); console.log(apiCache.toString()); // "public, max-age=60, stale-while-revalidate=300, stale-if-error=600" // Private authenticated content const privateCache = new CacheControl() .set("private", true) .set("max-age", 300) .set("must-revalidate", true); console.log(privateCache.toString()); // "private, max-age=300, must-revalidate" // No caching for real-time data const noCache = new CacheControl() .set("no-store", true) .set("no-cache", true); console.log(noCache.toString()); // "no-cache, no-store" ``` -------------------------------- ### Configure Multi-CDN Cache Control with Elysia.js Source: https://github.com/johnny-woodtke/elysiajs-cdn-cache/blob/main/example/README.md Configures the elysiajs-cdn-cache plugin to manage cache control across multiple layers: browser, general CDN, and Vercel Edge Network. ```typescript import cacheControl from "elysiajs-cdn-cache" app.use( cacheControl( "Cache-Control", // Browser cache "CDN-Cache-Control", // General CDN cache "Vercel-CDN-Cache-Control" // Vercel Edge Network ) ); ``` -------------------------------- ### Test Public API Cache with curl Source: https://github.com/johnny-woodtke/elysiajs-cdn-cache/blob/main/example/README.md Uses curl to fetch the response headers for the public API endpoint, demonstrating the applied cache control policy. ```bash curl -I https://elysiajs-cdn-cache.vercel.app/api ``` -------------------------------- ### cacheControl Plugin Setup Source: https://context7.com/johnny-woodtke/elysiajs-cdn-cache/llms.txt Demonstrates how to use the `cacheControl` plugin with single and multiple header configurations in Elysia. ```APIDOC ## POST /api/users ### Description This endpoint is used for user authentication. ### Method POST ### Endpoint /api/users ### Parameters #### Query Parameters - **username** (string) - Required - The username for authentication. - **password** (string) - Required - The password for authentication. ### Request Body ```json { "username": "testuser", "password": "password123" } ``` ### Response #### Success Response (200) - **token** (string) - The authentication token. - **expires_in** (integer) - The token expiration time in seconds. #### Response Example ```json { "token": "eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9...", "expires_in": 3600 } ``` ``` -------------------------------- ### Elysia Cache Control Plugin Setup and Usage (TypeScript) Source: https://context7.com/johnny-woodtke/elysiajs-cdn-cache/llms.txt Demonstrates how to integrate and use the `cacheControl` plugin in Elysia.js. It covers basic setup with a single header and advanced multi-CDN configurations with distinct caching policies for browser, general CDN, and platform-specific caches. ```typescript import { Elysia } from "elysia"; import { cacheControl, CacheControl } from "elysiajs-cdn-cache"; // Basic setup with single header const app = new Elysia() .use(cacheControl("Cache-Control")) .get("/", ({ cacheControl }) => { cacheControl.set( "Cache-Control", new CacheControl() .set("public", true) .set("max-age", 3600) ); return { message: "Cached response!" }; }) .listen(3000); // Multi-CDN setup with multiple headers const multiCdnApp = new Elysia() .use( cacheControl( "Cache-Control", // Browser cache "CDN-Cache-Control", // General CDN cache "Vercel-CDN-Cache-Control" // Vercel-specific cache ) ) .get("/api/data", ({ cacheControl }) => { // Browser: 5 minutes cacheControl.set( "Cache-Control", new CacheControl().set("public", true).set("max-age", 300) ); // CDN: 1 hour cacheControl.set( "CDN-Cache-Control", new CacheControl().set("public", true).set("s-maxage", 3600) ); // Vercel: 24 hours cacheControl.set( "Vercel-CDN-Cache-Control", new CacheControl().set("public", true).set("s-maxage", 86400) ); return { timestamp: Date.now() }; }) .listen(3000); ``` -------------------------------- ### Test Private User Data Cache with curl Source: https://github.com/johnny-woodtke/elysiajs-cdn-cache/blob/main/example/README.md Uses curl to fetch the response headers for a private user data endpoint, verifying that it is not cached by CDNs. ```bash curl -I https://elysiajs-cdn-cache.vercel.app/api/user123 ``` -------------------------------- ### GET /api/:id Source: https://github.com/johnny-woodtke/elysiajs-cdn-cache/blob/main/example/README.md This endpoint demonstrates private caching for personalized content. It is intended for user-specific or sensitive data that should not be cached by shared CDNs. ```APIDOC ## GET /api/:id ### Description This endpoint demonstrates private caching for personalized content. It is intended for user-specific or sensitive data that should not be cached by shared CDNs. ### Method GET ### Endpoint /api/:id ### Parameters #### Path Parameters - **id** (string) - Required - The identifier for the specific resource. #### Query Parameters None #### Request Body None ### Response #### Success Response (200) - **Cache-Control** (string) - Private caching directive (`private, max-age=60`) ### Response Example ``` { "data": "User specific data" } ``` ### Cache Headers ```typescript Cache-Control: private, max-age=60 ``` - **Browser cache**: 60 seconds (private only) - **CDN cache**: Not cached (due to `private` directive) ``` -------------------------------- ### Public API Cache Control Header Source: https://github.com/johnny-woodtke/elysiajs-cdn-cache/blob/main/example/README.md Defines cache control headers for a public API endpoint. This allows caching by browsers and CDNs with specified durations. ```http Cache-Control: public, s-maxage=120, max-age=60 ``` -------------------------------- ### Plugin Setup: Configuring Cache Headers in ElysiaJS Source: https://github.com/johnny-woodtke/elysiajs-cdn-cache/blob/main/README.md Demonstrates different ways to set up the `cacheControl` plugin in ElysiaJS, including default header, single custom header, and multiple custom headers for various CDN layers. ```typescript import { cacheControl } from "elysiajs-cdn-cache"; // Default header (Cache-Control) app.use(cacheControl()); // Single header app.use(cacheControl("CDN-Cache-Control")); // Multiple headers for different CDN layers app.use( cacheControl( "Cache-Control", // Browser cache "CDN-Cache-Control", // General CDN cache "Vercel-CDN-Cache-Control" // Vercel-specific cache ) ); ``` -------------------------------- ### Common Pattern: Caching Private Content with ElysiaJS Source: https://github.com/johnny-woodtke/elysiajs-cdn-cache/blob/main/README.md An example of how to configure caching for private, authenticated content using `private`, `max-age`, and `must-revalidate` directives. ```typescript app.get("/user/profile", ({ cacheControl }) => { cacheControl.set( "Cache-Control", new CacheControl() .set("private", true) .set("max-age", 300) .set("must-revalidate", true) ); return await getUserProfile(); }); ``` -------------------------------- ### Common Pattern: Caching Static Assets with ElysiaJS Source: https://github.com/johnny-woodtke/elysiajs-cdn-cache/blob/main/README.md An example of how to configure long cache durations and `immutable` directive for static assets served via ElysiaJS using the `elysiajs-cdn-cache` plugin. ```typescript app.get("/assets/*", ({ cacheControl }) => { cacheControl.set( "Cache-Control", new CacheControl() .set("public", true) .set("max-age", 31536000) // 1 year .set("immutable", true) ); // Serve static file... }); ``` -------------------------------- ### Private API Cache Control Header Source: https://github.com/johnny-woodtke/elysiajs-cdn-cache/blob/main/example/README.md Defines cache control headers for a private API endpoint. This restricts caching to the user's browser and prevents CDN caching. ```http Cache-Control: private, max-age=60 ``` -------------------------------- ### Configure Multi-CDN Cache Headers in ElysiaJS Source: https://github.com/johnny-woodtke/elysiajs-cdn-cache/blob/main/README.md This example shows how to configure different cache control headers for browser, general CDNs, and specific CDNs like Vercel. It utilizes the ElysiaJS cacheControl plugin to set 'max-age', 's-maxage', and other directives for varying cache durations. ```typescript app.use( cacheControl( "Cache-Control", // Controls browser caching "CDN-Cache-Control", // Controls intermediate CDNs "Vercel-CDN-Cache-Control" // Controls Vercel's Edge Network ) ); app.get("/api/content", ({ cacheControl }) => { // Browser cache: 5 minutes cacheControl.set( "Cache-Control", new CacheControl().set("public", true).set("max-age", 300) ); // General CDN: 1 hour cacheControl.set( "CDN-Cache-Control", new CacheControl().set("public", true).set("s-maxage", 3600) ); // Vercel CDN: 24 hours cacheControl.set( "Vercel-CDN-Cache-Control", new CacheControl().set("public", true).set("s-maxage", 86400) ); }); ``` -------------------------------- ### CacheControl.get() Method Source: https://context7.com/johnny-woodtke/elysiajs-cdn-cache/llms.txt The `get` method retrieves the value of a specific cache directive. It returns `true` for boolean directives that are set, a number for numeric directives, or `undefined` if the directive is not configured. ```APIDOC ## GET /api/conditional ### Description This endpoint demonstrates how to use the `cacheControl.get()` method to read incoming `Cache-Control` headers and conditionally set response caching directives. ### Method GET ### Endpoint /api/conditional ### Parameters #### Query Parameters None #### Request Body None ### Request Example ```http GET /api/conditional HTTP/1.1 Host: localhost:3000 Cache-Control: no-cache, max-age=7200 ``` ### Response #### Success Response (200) - **data** (string) - A sample response string. - **timestamp** (number) - The timestamp when the response was generated. #### Response Example ```json { "data": "response", "timestamp": 1678886400000 } ``` ``` -------------------------------- ### Route Handler Integration Source: https://context7.com/johnny-woodtke/elysiajs-cdn-cache/llms.txt The plugin injects a `cacheControl` object into Elysia route handlers with `get()` and `set()` methods for reading request headers and setting response headers respectively. This enables dynamic cache control based on request parameters, authentication status, or content type. ```APIDOC ## Elysia API with Cache Control Integration ### Description This section details how the `cacheControl` plugin integrates with Elysia route handlers, providing `get()` and `set()` methods for managing HTTP cache headers across multiple cache layers (e.g., browser, CDN). ### Method GET ### Endpoint - `/api/posts` - `/api/posts/:id` - `/api/user/profile` ### Parameters #### Path Parameters - **id** (string) - Required - The ID of the post. #### Query Parameters None #### Request Body None ### Request Example ```http GET /api/posts HTTP/1.1 Host: localhost:3000 ``` ```http GET /api/posts/123 HTTP/1.1 Host: localhost:3000 ``` ```http GET /api/user/profile HTTP/1.1 Host: localhost:3000 ``` ### Response #### Success Response (200) - **posts** (array) - List of posts (for `/api/posts`). - **id** (string) - Post ID (for `/api/posts/:id`). - **title** (string) - Post title (for `/api/posts/:id`). - **user** (object) - User profile information (for `/api/user/profile`). - **timestamp** (string) - ISO formatted timestamp. #### Response Example ```json { "posts": [ { "id": 1, "title": "Hello" } ], "timestamp": "2023-10-27T10:00:00.000Z" } ``` ```json { "id": "123", "title": "Post 123", "timestamp": "2023-10-27T10:00:00.000Z" } ``` ```json { "user": { "id": 123, "name": "John" } } ``` ``` -------------------------------- ### Get Cache Directive Value in Elysia Source: https://context7.com/johnny-woodtke/elysiajs-cdn-cache/llms.txt Demonstrates how to retrieve the value of a specific cache directive from an incoming Cache-Control header using the elysiajs-cdn-cache plugin. It shows how to check for boolean directives like 'no-cache' and numeric directives like 'max-age', returning true, a number, or undefined. ```typescript import { Elysia } from "elysia"; import { cacheControl, CacheControl } from "elysiajs-cdn-cache"; const app = new Elysia() .use(cacheControl("Cache-Control")) .get("/api/conditional", ({ cacheControl, headers }) => { // Read and parse incoming Cache-Control header from request const incomingCache = cacheControl.get("Cache-Control"); // Check specific directives from incoming request const noCache = incomingCache.get("no-cache"); const maxAge = incomingCache.get("max-age"); console.log("Client requests no-cache:", noCache); // true or undefined console.log("Client max-age preference:", maxAge); // number or undefined // Conditionally set response caching based on request if (noCache) { cacheControl.set( "Cache-Control", new CacheControl().set("no-cache", true).set("must-revalidate", true) ); } else { cacheControl.set( "Cache-Control", new CacheControl().set("public", true).set("max-age", maxAge || 3600) ); } return { data: "response", timestamp: Date.now() }; }) .listen(3000); ``` -------------------------------- ### CacheControl Class: Constructor and Parsing Source: https://github.com/johnny-woodtke/elysiajs-cdn-cache/blob/main/README.md Shows how to instantiate the `CacheControl` class, both as an empty instance and by parsing an existing cache header string. ```typescript // Create empty instance const cache = new CacheControl(); // Parse from existing header string const cache = new CacheControl("public, max-age=3600, s-maxage=7200"); ``` -------------------------------- ### CacheControl Class Constructor Source: https://context7.com/johnny-woodtke/elysiajs-cdn-cache/llms.txt Explains how to instantiate the `CacheControl` class, both with and without parsing an existing cache header string. ```APIDOC ## GET /items/{id} ### Description Retrieves a specific item by its ID. ### Method GET ### Endpoint /items/{id} ### Parameters #### Path Parameters - **id** (string) - Required - The unique identifier of the item to retrieve. #### Query Parameters - **include_details** (boolean) - Optional - If true, includes detailed information about the item. ### Response #### Success Response (200) - **id** (string) - The item's unique identifier. - **name** (string) - The name of the item. - **description** (string) - The description of the item. - **details** (object) - Optional - Detailed information about the item, only included if `include_details` is true. #### Response Example ```json { "id": "item-123", "name": "Example Item", "description": "This is a sample item.", "details": { "weight": "10kg", "dimensions": "10x10x10 cm" } } ``` ``` -------------------------------- ### Common Pattern: API Responses with CDN Caching in ElysiaJS Source: https://github.com/johnny-woodtke/elysiajs-cdn-cache/blob/main/README.md Demonstrates setting different cache strategies for browser (`Cache-Control`) and CDN (`Vercel-CDN-Cache-Control`) for API responses, including `stale-while-revalidate`. ```typescript app.get("/api/posts", ({ cacheControl }) => { cacheControl.set( "Cache-Control", new CacheControl() .set("public", true) .set("max-age", 60) // Browser: 1 minute .set("stale-while-revalidate", 300) // Allow stale for 5 minutes ); cacheControl.set( "Vercel-CDN-Cache-Control", new CacheControl().set("public", true).set("s-maxage", 3600) // Vercel CDN: 1 hour ); return await getPosts(); }); ``` -------------------------------- ### CacheControl.set() Method Source: https://context7.com/johnny-woodtke/elysiajs-cdn-cache/llms.txt Details the usage of the `set` method for configuring various cache directives, including boolean and numeric options. ```APIDOC ## PUT /users/{id} ### Description Updates an existing user's information. ### Method PUT ### Endpoint /users/{id} ### Parameters #### Path Parameters - **id** (string) - Required - The ID of the user to update. #### Request Body - **name** (string) - Optional - The user's new name. - **email** (string) - Optional - The user's new email address. ### Request Example ```json { "name": "Updated Name", "email": "updated@example.com" } ``` ### Response #### Success Response (200) - **id** (string) - The updated user's ID. - **name** (string) - The updated user's name. - **email** (string) - The updated user's email address. #### Response Example ```json { "id": "user-456", "name": "Updated Name", "email": "updated@example.com" } ``` ``` -------------------------------- ### CacheControl Class: Serialization to String Source: https://github.com/johnny-woodtke/elysiajs-cdn-cache/blob/main/README.md Demonstrates how to serialize a `CacheControl` object into its string representation, suitable for setting HTTP headers. ```typescript const cache = new CacheControl().set("public", true).set("max-age", 3600); console.log(cache.toString()); // "public, max-age=3600" ``` -------------------------------- ### Integrate Elysia CDN Cache Plugin with Route Handlers Source: https://context7.com/johnny-woodtke/elysiajs-cdn-cache/llms.txt Shows how to integrate the elysiajs-cdn-cache plugin into Elysia route handlers to dynamically control caching for different endpoints. It demonstrates setting various cache directives like 'public', 'max-age', 's-maxage', 'private', and 'must-revalidate' for different routes. ```typescript import { Elysia, t } from "elysia"; import { cacheControl, CacheControl } from "elysiajs-cdn-cache"; const app = new Elysia({ prefix: "/api" }) .use( cacheControl( "Cache-Control", "CDN-Cache-Control", "Vercel-CDN-Cache-Control" ) ) // Public list endpoint - aggressive CDN caching .get("/posts", ({ cacheControl }) => { cacheControl.set( "Cache-Control", new CacheControl() .set("public", true) .set("max-age", 60) .set("s-maxage", 3600) ); return { posts: [{ id: 1, title: "Hello" }], timestamp: new Date().toISOString() }; }) // Dynamic endpoint with ID - shorter cache .get( "/posts/:id", ({ cacheControl, params: { id } }) => { cacheControl.set( "Cache-Control", new CacheControl() .set("public", true) .set("max-age", 30) .set("stale-while-revalidate", 60) ); return { id, title: `Post ${id}`, timestamp: new Date().toISOString() }; }, { params: t.Object({ id: t.String() }), } ) // Private user data - no CDN caching .get("/user/profile", ({ cacheControl }) => { cacheControl.set( "Cache-Control", new CacheControl() .set("private", true) .set("max-age", 300) .set("must-revalidate", true) ); return { user: { id: 123, name: "John" } }; }) .listen(3000); console.log("Server running at http://localhost:3000"); ``` -------------------------------- ### CacheControl Class: Setting Cache Directives Source: https://github.com/johnny-woodtke/elysiajs-cdn-cache/blob/main/README.md Illustrates the use of the `CacheControl` class's fluent API to set various time-based and boolean cache directives. ```typescript const cache = new CacheControl() // Time-based directives .set("max-age", 3600) // max-age=3600 .set("s-maxage", 7200) // s-maxage=7200 .set("stale-while-revalidate", 60) // stale-while-revalidate=60 .set("stale-if-error", 300) // stale-if-error=300 // Boolean directives .set("public", true) // public .set("private", true) // private .set("no-cache", true) // no-cache .set("no-store", true) // no-store .set("must-revalidate", true) // must-revalidate .set("proxy-revalidate", true) // proxy-revalidate .set("immutable", true) // immutable .set("no-transform", true) // no-transform .set("must-understand", true); // must-understand ``` -------------------------------- ### Route Handler: Reading and Setting Cache Headers in ElysiaJS Source: https://github.com/johnny-woodtke/elysiajs-cdn-cache/blob/main/README.md Shows how to use the `cacheControl` object within an ElysiaJS route handler to read incoming `Cache-Control` headers and set response headers for both browser and CDN. ```typescript app.get("/api/data", ({ cacheControl }) => { // Read incoming cache headers const incomingCache = cacheControl.get("Cache-Control"); console.log("Incoming cache:", incomingCache.toString()); // Set response cache headers cacheControl.set( "Cache-Control", new CacheControl().set("public", true).set("max-age", 300) // Browser cache: 5 minutes ); // Set CDN-specific caching cacheControl.set( "CDN-Cache-Control", new CacheControl().set("public", true).set("s-maxage", 3600) // CDN cache: 1 hour ); return { timestamp: Date.now() }; }); ``` -------------------------------- ### Disable Caching for API Endpoint Source: https://github.com/johnny-woodtke/elysiajs-cdn-cache/blob/main/README.md This snippet demonstrates how to disable caching for a specific API endpoint by setting 'no-store' and 'no-cache' directives in the Cache-Control header. It uses the ElysiaJS framework and requires a function to fetch realtime data. ```typescript app.get("/api/realtime", ({ cacheControl }) => { cacheControl.set( "Cache-Control", new CacheControl().set("no-store", true).set("no-cache", true) ); return await getRealtimeData(); }); ``` === COMPLETE CONTENT === This response contains all available snippets from this library. No additional content exists. Do not make further requests.