Skip to content

Commit

Permalink
Update code from service worker to module worker format
Browse files Browse the repository at this point in the history
  • Loading branch information
vicb committed Nov 12, 2024
1 parent ac31928 commit a35567c
Show file tree
Hide file tree
Showing 7 changed files with 383 additions and 376 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ title: Subdomains and subdirectories
pcx_content_type: how-to
sidebar:
order: 14

---

## Run APO on a subdomain
Expand Down Expand Up @@ -36,24 +35,20 @@ If you choose to run APO only on a subdirectory, the rest of the domain should b
The `cf-edge-cache: no-cache` instructs the APO service to bypass caching for non-WordPress parts of the site. You can implement this option with Cloudflare Workers using the example below.

```js
addEventListener('fetch', event => {
event.respondWith(handleRequest(event.request));
});

async function handleRequest(request) {
/**
* Response properties are immutable. To change them, construct a new
* Response object. Response headers can be modified through the headers `set` method.
*/
const originalResponse = await fetch(request);

let response = new Response(originalResponse.body, originalResponse);

// Add a header using set method
response.headers.set('cf-edge-cache', 'no-cache');

return response;
}
export default {
async fetch(request, env, ctx) {
const originalResponse = await fetch(request);

/**
* Response properties are immutable. To change them, construct a new
* Response object. Response headers can be modified through the headers `set` method.
*/
const response = new Response(originalResponse.body, originalResponse);
response.headers.set("cf-edge-cache", "no-cache");

return response;
},
};
```

### Use Cache Rules
Expand Down
123 changes: 63 additions & 60 deletions src/content/docs/images/transform-images/control-origin-access.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -3,23 +3,20 @@ pcx_content_type: reference
title: Control origin access
sidebar:
order: 24

---

You can serve resized images without giving access to the original image. Images can be hosted on another server outside of your zone, and the true source of the image can be entirely hidden. The origin server may require authentication to disclose the original image, without needing visitors to be aware of it. Access to the full-size image may be prevented by making it impossible to manipulate resizing parameters.

All these behaviors are completely customizable, because they are handled by custom code of a script running [on the edge in a Cloudflare Worker](/images/transform-images/transform-via-workers/).

```js
addEventListener("fetch", event => {
event.respondWith(handleRequest(event.request))
})

async function handleRequest(request) {
// Here you can compute arbitrary imageURL and
// resizingOptions from any request data ...
return fetch(imageURL, {cf:{image:resizingOptions}})
}
export default {
async fetch(request, env, ctx) {
// Here you can compute arbitrary imageURL and
// resizingOptions from any request data ...
return fetch(imageURL, { cf: { image: resizingOptions } });
},
};
```

This code will be run for every request, but the source code will not be accessible to website visitors. This allows the code to perform security checks and contain secrets required to access the images in a controlled manner.
Expand All @@ -28,27 +25,27 @@ The examples below are only suggestions, and do not have to be followed exactly.

:::caution[Warning]


When testing image transformations, make sure you deploy the script and test it from a regular web browser window. The preview in the dashboard does not simulate transformations.


:::

## Hiding the image server

```js
async function handleRequest(request) {
const resizingOptions = {/* resizing options will be demonstrated in the next example */}

const hiddenImageOrigin = "https://secret.example.com/hidden-directory"
const requestURL = new URL(request.url)
// Append the request path such as "/assets/image1.jpg" to the hiddenImageOrigin.
// You could also process the path to add or remove directories, modify filenames, etc.
const imageURL = hiddenImageOrigin + requestURL.path
// This will fetch image from the given URL, but to the website's visitors this
// will appear as a response to the original request. Visitor’s browser will
// not see this URL.
return fetch(imageURL, {cf:{image:resizingOptions}})
const resizingOptions = {
/* resizing options will be demonstrated in the next example */
};

const hiddenImageOrigin = "https://secret.example.com/hidden-directory";
const requestURL = new URL(request.url);
// Append the request path such as "/assets/image1.jpg" to the hiddenImageOrigin.
// You could also process the path to add or remove directories, modify filenames, etc.
const imageURL = hiddenImageOrigin + requestURL.path;
// This will fetch image from the given URL, but to the website's visitors this
// will appear as a response to the original request. Visitor’s browser will
// not see this URL.
return fetch(imageURL, { cf: { image: resizingOptions } });
}
```

Expand Down Expand Up @@ -79,28 +76,34 @@ You do not have to include actual pixel dimensions in the URL. You can embed siz

```js
async function handleRequest(request) {
const requestURL = new URL(request.url)
const resizingOptions = {}

// The regex selects the first path component after the "images"
// prefix, and the rest of the path (e.g. "/images/first/rest")
const match = requestURL.path.match(/images\/([^/]+)\/(.+)/)

// You can require the first path component to be one of the
// predefined sizes only, and set actual dimensions accordingly.
switch (match && match[1]) {
case "small": resizingOptions.width = 300; break;
case "medium": resizingOptions.width = 600; break;
case "large": resizingOptions.width = 900; break;
default:
throw Error("invalid size");
}

// The remainder of the path may be used to locate the original
// image, e.g. here "/images/small/image1.jpg" would map to
// "https://storage.example.com/bucket/image1.jpg" resized to 300px.
const imageURL = "https://storage.example.com/bucket/" + match[2]
return fetch(imageURL, {cf:{image:resizingOptions}})
const requestURL = new URL(request.url);
const resizingOptions = {};

// The regex selects the first path component after the "images"
// prefix, and the rest of the path (e.g. "/images/first/rest")
const match = requestURL.path.match(/images\/([^/]+)\/(.+)/);

// You can require the first path component to be one of the
// predefined sizes only, and set actual dimensions accordingly.
switch (match && match[1]) {
case "small":
resizingOptions.width = 300;
break;
case "medium":
resizingOptions.width = 600;
break;
case "large":
resizingOptions.width = 900;
break;
default:
throw Error("invalid size");
}

// The remainder of the path may be used to locate the original
// image, e.g. here "/images/small/image1.jpg" would map to
// "https://storage.example.com/bucket/image1.jpg" resized to 300px.
const imageURL = "https://storage.example.com/bucket/" + match[2];
return fetch(imageURL, { cf: { image: resizingOptions } });
}
```

Expand All @@ -111,7 +114,7 @@ Cloudflare image transformations cache resized images to aid performance. Images
```js null {9}
// generate signed headers (application specific)
const signedHeaders = generatedSignedHeaders();

fetch(private_url, {
headers: signedHeaders
cf: {
Expand All @@ -125,20 +128,20 @@ fetch(private_url, {

When using this code, the following headers are passed through to the origin, and allow your request to be successful:

* `Authorization`
* `Cookie`
* `x-amz-content-sha256`
* `x-amz-date`
* `x-ms-date`
* `x-ms-version`
* `x-sa-date`
* `cf-access-client-id`
* `cf-access-client-secret`
- `Authorization`
- `Cookie`
- `x-amz-content-sha256`
- `x-amz-date`
- `x-ms-date`
- `x-ms-version`
- `x-sa-date`
- `cf-access-client-id`
- `cf-access-client-secret`

For more information, refer to:

* [AWS docs](https://docs.aws.amazon.com/AmazonS3/latest/API/sig-v4-authenticating-requests.html)
* [Azure docs](https://docs.microsoft.com/en-us/rest/api/storageservices/List-Containers2#request-headers)
* [Google Cloud docs](https://cloud.google.com/storage/docs/aws-simple-migration)
* [Cloudflare Zero Trust docs](/cloudflare-one/identity/service-tokens/)
* [SecureAuth docs](https://docs.secureauth.com/2104/en/authentication-api-guide.html)
- [AWS docs](https://docs.aws.amazon.com/AmazonS3/latest/API/sig-v4-authenticating-requests.html)
- [Azure docs](https://docs.microsoft.com/en-us/rest/api/storageservices/List-Containers2#request-headers)
- [Google Cloud docs](https://cloud.google.com/storage/docs/aws-simple-migration)
- [Cloudflare Zero Trust docs](/cloudflare-one/identity/service-tokens/)
- [SecureAuth docs](https://docs.secureauth.com/2104/en/authentication-api-guide.html)
Loading

0 comments on commit a35567c

Please sign in to comment.