Skip to content
This repository has been archived by the owner on Feb 17, 2023. It is now read-only.

Edge cache bypass if query string #30

Closed
reinder42 opened this issue Mar 9, 2019 · 3 comments
Closed

Edge cache bypass if query string #30

reinder42 opened this issue Mar 9, 2019 · 3 comments

Comments

@reinder42
Copy link

I've used the following code to bypass the edge cache if the request URL contains a query string, so that dynamic content based on the query string can still be generated at the origin.

addEventListener("fetch", event => {

...

let hasQueryString = false;
if(request.url && (request.url.indexOf('?') !== -1)) {
    hasQueryString = true;
}

if (configured && !isImage && !hasQueryString && upstreamCache === null) {

...

Does the above code make sense to bypass the cache, if there's a query string? Or should it use some other property of request? I just wanted to check – assuming it's a fairly typical use case :-)

@pmeenan
Copy link
Contributor

pmeenan commented Mar 9, 2019

Yes, that should work fine. Depending on how your site is configured that may bypass the cache a lot more often than you'd like (it's not unusual for WordPress to have the article ID as a query string for example).

You probably don't need to check request.url since it's impossible to come into the worker without one but I'm a fan of always being safe just in case so it doesn't hurt.

@reinder42
Copy link
Author

Great, thanks!

Depending on how your site is configured that may bypass the cache a lot more often than you'd like (it's not unusual for WordPress to have the article ID as a query string for example).

Good point. My website uses friendly URLs though, so query strings are only used for stuff like coupons on sales pages.

You probably don't need to check request.url since it's impossible to come into the worker without one but I'm a fan of always being safe just in case so it doesn't hurt.

Hehe, this script will work fine even if the internet doesn't use URLs ;-)

lukeed pushed a commit that referenced this issue Apr 29, 2022
lukeed pushed a commit that referenced this issue Apr 29, 2022
Debug wrapper and default debug mode to true
@lauragift21
Copy link
Collaborator

Closing this issue because the initial question was addressed. Also the templates have moved over to the worker-sdk monorepo: https://github.com/cloudflare/workers-sdk/tree/main/templates.

if you're still experiencing issues feel free to open an issue there.

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants