Code Monkey home page Code Monkey logo

cf-workers-site-template's Introduction

Cloudflare Workers Site Template

This repository contains a CI/CD-optimized Cloudflare Workers template written in TypeScript for serving static sites. It allows certain routes to be proxied so that all requests can be served under the same subdomain, and therefore avoids CORS issues and benefits from Cloudflare's "under attack" DDoS protection mode.

The template is optimized for single page applications. Whenever a request hits a route where an asset cannot be found, a fallback to index.html is sent instead of a 404 error.

Usage

The template is designed to be used by CI/CD pipelines. Such a pipeline shall first transform the website into static files, for example with a Webpack build; then it should clone this repository and replace content in the public folder with static files to be served.

Then, authenticate with cloudflare with the following command:

$ echo "${CLOUDFLARE_API_TOKEN}" | yarn wrangler config

Set the following environment variables:

  • CLOUDFLARE_WORKER_NAME

    Name of the Cloudflare worker.

  • CLOUDFLARE_ACCOUNT_ID

    Cloudflare account ID.

  • DOMAIN

    Custom domain to be used for serving the website.

  • CLOUDFLARE_ZONE_ID

    Cloudflare zone ID of the custom domain.

Finally, publish the site with:

$ yarn run publish

Route-based Request Proxying

The worker can be configured to proxy requests to certain routes to other URLs, and thus avoiding cross-origin issues that arise when using Cloudflare's "under attack" DDoS protection mode. Simply replace the file ./src/external/proxies.json with a JSON-formatted list of proxy rules. For example:

[
  {
    "prefix": "/api/",
    "target": "https://api.example.com/"
  }
]

In the example configuration above, assuming the worker is served at https://example.com/, a request to https://example.com/api/login will be proxied to https://api.example.com/login, for instance.

License

MIT

cf-workers-site-template's People

Contributors

xjonathanlei avatar eric-convergence avatar tonyconv 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.