Skip to content
Permalink

Comparing changes

Choose two branches to see what’s changed or to start a new pull request. If you need to, you can also or learn more about diff comparisons.

Open a pull request

Create a new pull request by comparing changes across two branches. If you need to, you can also . Learn more about diff comparisons here.
base repository: pmeenan/cf-workers
Failed to load repositories. Confirm that selected base ref is valid, then try again.
Loading
base: master
Choose a base ref
...
head repository: PetrDlouhy/cf-workers
Failed to load repositories. Confirm that selected head ref is valid, then try again.
Loading
compare: master
Choose a head ref
Able to merge. These branches can be automatically merged.
  • 2 commits
  • 2 files changed
  • 1 contributor

Commits on Sep 30, 2020

  1. Verified

    This commit was signed with the committer’s verified signature.
    Murderlon Merlijn Vos
    Copy the full SHA
    8aca0a6 View commit details
  2. Copy the full SHA
    28a93b0 View commit details
Showing with 34 additions and 1 deletion.
  1. +13 −1 cache-bypass-on-cookie/README.md
  2. +21 −0 cache-bypass-on-cookie/cache-bypass-on-cookie.js
14 changes: 13 additions & 1 deletion cache-bypass-on-cookie/README.md
Original file line number Diff line number Diff line change
@@ -7,4 +7,16 @@ As-configured it is set up for a default WordPress deployment with the default W

The worker currently bypasses ALL requests, not just HTML requests. It can be modified to only inspect HTML requests or pretty much any other arbitrary rules for when to bypass the cache.

The worker does not actively manage the cache (extending cache times, purging, etc) = it strictly bypasses any existing caches when the configured rules are matched.
The worker does not actively manage the cache (extending cache times, purging, etc) = it strictly bypasses any existing caches when the configured rules are matched.


## INSTALLATION:

1) Copy content of the cache-bypass-on-cookie.js file to the script section in worker editor.
2) Configure variables at the beginning to fit your needs.
3) Add route to fit URLs where you need to bypass your cache (i.e. `*.example.com/*`) under Workers menu.
4) Add new page rule like `https://*.example.com/*cf_cache_bust*` with `Cache Level: Bypass` before your other caching rules. This is needed if you want your headers to be delivered from server to user when bypassing cache (by both `BYPASS_URL_PATTERNS` and `BYPASS_COOKIE_PREFIXES`)

### Cache control from server

If you need more precise control over what will be cached from the server, you can set `cache-cookie=no-cache` or `cache-cookie=cache` cookie to achieve that. Be aware, that if going from cached mode to non-cached mode you will need to go over page listed in `BYPASS_URL_PATTERNS`.
21 changes: 21 additions & 0 deletions cache-bypass-on-cookie/cache-bypass-on-cookie.js
Original file line number Diff line number Diff line change
@@ -11,6 +11,10 @@ const BYPASS_URL_PATTERNS = [
/\/wp-admin\/.*/
];

const NO_BYPASS_URL_PATTERNS = [
/.*cf_cache_bust.*/
]

/**
* Main worker entry point.
*/
@@ -59,6 +63,14 @@ function bypassCache(request) {
// Bypass the cache for all requests to a URL that matches any of the URL path bypass patterns
const url = new URL(request.url);
const path = url.pathname + url.search;

if (NO_BYPASS_URL_PATTERNS.length) {
for (let pattern of NO_BYPASS_URL_PATTERNS) {
if (path.match(pattern)) {
return false;
}
}
}
if (BYPASS_URL_PATTERNS.length) {
for (let pattern of BYPASS_URL_PATTERNS) {
if (path.match(pattern)) {
@@ -74,6 +86,15 @@ function bypassCache(request) {
if (cookieHeader && cookieHeader.length && BYPASS_COOKIE_PREFIXES.length) {
const cookies = cookieHeader.split(';');
for (let cookie of cookies) {

// If cache-cookie is set, drive cache explicitly
if(cookie.trim() == 'cache-cookie=no-cache'){
return true;
};
if(cookie.trim() == 'cache-cookie=cache'){
return false;
};

// See if the cookie starts with any of the logged-in user prefixes
for (let prefix of BYPASS_COOKIE_PREFIXES) {
if (cookie.trim().startsWith(prefix)) {