From d61ba382ce8fb3b0c850ce3cb1bece005fc0965c Mon Sep 17 00:00:00 2001 From: Nikita Cano Date: Tue, 17 Dec 2024 12:55:04 +0000 Subject: [PATCH 1/2] [Snippets] New example: Change origin and modify paths --- .../snippets/examples/route-and-rewrite.mdx | 54 +++++++++++++++++++ 1 file changed, 54 insertions(+) create mode 100644 src/content/docs/rules/snippets/examples/route-and-rewrite.mdx diff --git a/src/content/docs/rules/snippets/examples/route-and-rewrite.mdx b/src/content/docs/rules/snippets/examples/route-and-rewrite.mdx new file mode 100644 index 00000000000000..de239e66076d8f --- /dev/null +++ b/src/content/docs/rules/snippets/examples/route-and-rewrite.mdx @@ -0,0 +1,54 @@ +--- +type: example +summary: Reroute a request to a different origin and modify the URL path. +goal: + - Routing +operation: + - Modify URL +products: + - Snippets +pcx_content_type: example +title: Change origin and modify paths +description: Route requests to a different origin, prepend a directory to the URL path, and remove specific segments. +--- + +This example demonstrates how to use Cloudflare Snippets to: + +* Reroute incoming requests to a different origin. +* Prepend a directory to the URL path. +* Remove specific segments from the URL path. + +```js +export default { + async fetch(request) { + // Clone the original request to create a new request object + const newRequest = new Request(request); + + // Add a header to identify a re-routed request at the new origin + newRequest.headers.set("X-Rerouted", "1"); + + // Clone and parse the original URL + const url = new URL(request.url); + + // Step 1: Reroute to a different origin + url.hostname = "example.com"; // Change the hostname to the new origin + + // Step 2: Append a directory to the path + url.pathname = `/new-path${url.pathname}`; // Prepend "/new-path" to the current path + + // Step 3: Remove a specific segment from the path + url.pathname = url.pathname.replace("/remove-me", ""); // Rewrite `/remove-me/something` to `/something` + + // Fetch the modified request from the updated URL + return await fetch(url, newRequest); + } +}; +``` + +This configuration will perform the following rewrites: + +| Request URL | URL after rewrite | +| ----------------------------------------- | ---------------------------------- | +| `https://subdomain.example.com/foo` | `https://example.com/new-path/foo` | +| `https://example.com/remove-me/bar` | `https://example.com/new-path/bar` | +| `https://example.net/remove-me` | `https://example.com/new-path/` | From 138ad94182d7dcc5f6c44cbe29ec9c3691ba05b0 Mon Sep 17 00:00:00 2001 From: Pedro Sousa <680496+pedrosousa@users.noreply.github.com> Date: Tue, 17 Dec 2024 15:04:56 +0000 Subject: [PATCH 2/2] PCX review (also ran prettier) --- .../snippets/examples/route-and-rewrite.mdx | 48 +++++++++---------- 1 file changed, 24 insertions(+), 24 deletions(-) diff --git a/src/content/docs/rules/snippets/examples/route-and-rewrite.mdx b/src/content/docs/rules/snippets/examples/route-and-rewrite.mdx index de239e66076d8f..ea5d4bf265065f 100644 --- a/src/content/docs/rules/snippets/examples/route-and-rewrite.mdx +++ b/src/content/docs/rules/snippets/examples/route-and-rewrite.mdx @@ -14,41 +14,41 @@ description: Route requests to a different origin, prepend a directory to the UR This example demonstrates how to use Cloudflare Snippets to: -* Reroute incoming requests to a different origin. -* Prepend a directory to the URL path. -* Remove specific segments from the URL path. +- Reroute incoming requests to a different origin. +- Prepend a directory to the URL path. +- Remove specific segments from the URL path. ```js export default { - async fetch(request) { - // Clone the original request to create a new request object - const newRequest = new Request(request); + async fetch(request) { + // Clone the original request to create a new request object + const newRequest = new Request(request); - // Add a header to identify a re-routed request at the new origin - newRequest.headers.set("X-Rerouted", "1"); + // Add a header to identify a rerouted request at the new origin + newRequest.headers.set("X-Rerouted", "1"); - // Clone and parse the original URL - const url = new URL(request.url); + // Clone and parse the original URL + const url = new URL(request.url); - // Step 1: Reroute to a different origin - url.hostname = "example.com"; // Change the hostname to the new origin + // Step 1: Reroute to a different origin + url.hostname = "example.com"; // Change the hostname to the new origin - // Step 2: Append a directory to the path - url.pathname = `/new-path${url.pathname}`; // Prepend "/new-path" to the current path + // Step 2: Append a directory to the path + url.pathname = `/new-path${url.pathname}`; // Prepend "/new-path" to the current path - // Step 3: Remove a specific segment from the path - url.pathname = url.pathname.replace("/remove-me", ""); // Rewrite `/remove-me/something` to `/something` + // Step 3: Remove a specific segment from the path + url.pathname = url.pathname.replace("/remove-me", ""); // Rewrite `/remove-me/something` to `/something` - // Fetch the modified request from the updated URL - return await fetch(url, newRequest); - } + // Fetch the modified request from the updated URL + return await fetch(url, newRequest); + }, }; ``` This configuration will perform the following rewrites: -| Request URL | URL after rewrite | -| ----------------------------------------- | ---------------------------------- | -| `https://subdomain.example.com/foo` | `https://example.com/new-path/foo` | -| `https://example.com/remove-me/bar` | `https://example.com/new-path/bar` | -| `https://example.net/remove-me` | `https://example.com/new-path/` | +| Request URL | URL after rewrite | +| ----------------------------------- | ---------------------------------- | +| `https://subdomain.example.com/foo` | `https://example.com/new-path/foo` | +| `https://example.com/remove-me/bar` | `https://example.com/new-path/bar` | +| `https://example.net/remove-me` | `https://example.com/new-path` |