Git Product home page Git Product logo

cloudflare-image-proxy's Introduction

CF Image proxy

Image proxy and CDN for Cloudflare Workers.

Build Status Prettier Code Formatting

Features

  • Free ๐Ÿ’ช
  • Super simple to setup and self-host
  • Perfect lighthouse scores
  • Handles CORS for you
  • Normalizes origin URLs
  • Respects pragma: no-cache and related headers
  • Used in hundreds of prod sites

Setup

  1. Create a new blank Cloudflare Worker.
  2. Fork / clone this repo
  3. Update the missing values in wrangler.toml
  4. npm install
  5. npm run dev to test locally
  6. npm run deploy to deploy to cloudflare workers ๐Ÿ’ช

wrangler.toml

name = "cf-image-proxy"
type = "javascript"
webpack_config = "webpack.config.js"
account_id = "TODO"
workers_dev = true

[env.production]
zone_id = "TODO"
route = "TODO"

You can find your account_id and zone_id in your Cloudflare Workers settings.

Your route should look like "exampledomain.com/*".

Cloudflare Polish

You can optionally enable Polish in your Cloudflare zone settings if you want to enable on-the-fly image optimization as part of your CDN. In many cases, this will serve images to supported clients in an optimized webp format.

This may increase costs, so it's not recommended for everyone. The CF worker should support both configurations without issue.

CDN

By default, all assets will be served with a cache-control header set to public, immutable, s-maxage=31536000, max-age=31536000, stale-while-revalidate=60, which effectively makes them cached at all levels indefinitely (or more practically until Cloudflare or your browser purges the asset from its cache).

If you want to change this cache-control header or add additional headers, see src/fetch-request.js.

Usage

Next.js Notion Starter Kit

If you're using this image proxy as part of nextjs-notion-starter-kit, all you need to do is set imageCDNHost in your site.config.js and your image proxy will be used automatically.

If you're not using this Next.js Notion boilerplate, then read on.

General Usage

In the application where you want to consume your proxied images, you'll need to replace your third-party image URLs.

You can replace them with your proxy domain plus a path that contains the URI-encoded version of your original domain. In TypeScript, this looks like the following:

const imageCDNHost = 'https://exampledomain.com'

export const mapImageUrl = (imageUrl: string) => {
  if (imageUrl.startsWith('data:')) {
    return imageUrl
  }

  if (imageCDNHost) {
    // Our proxy uses Cloudflare's global CDN to cache these image assets
    return `${imageCDNHost}/${encodeURIComponent(imageUrl)}`
  } else {
    return imageUrl
  }
}

Technical Notes

A few notes about the implementation:

  • It is hosted via Cloudflare (CF) edge workers.
  • It is transpiled by webpack before uploading to CF.
  • CF runs our worker via V8 directly in an environment mimicking web workers.
  • This means that our worker does not have access to Node.js primitives such as fs, dns and http.
  • It does have access to a custom web fetch API.

TODO

  • Initial release extracted from Notion2Site
  • Support restricting the origin domain in order to prevent abuse
  • Add a snazzy demo

License

MIT ยฉ Travis Fischer

Support my OSS work by following me on twitter twitter

cloudflare-image-proxy's People

Contributors

transitive-bullshit avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.