cloudflare / templates

A collection of starter templates and examples for Cloudflare Workers and Pages
https://cloudflareworkers.com
MIT License
999 stars 632 forks source link

Wordpress + CF Worker #41

Closed willstocks closed 1 year ago

willstocks commented 5 years ago

Hello!

So I just setup a CF Worker using the provided example (see here) and kept everything exactly as-is out of the box.

I am having to use the API config, as I don't have an option for KV storage at the moment (via the Workers tab, or workers.dev)

Workers KV is currently not available for workers.dev.

I'm noticing that most of my requests (https://willstocks.co.uk) are getting the following response headers applied:

x-html-edge-cache-status: Hit, Refreshed
x-html-edge-cache-version: -1

But if I look in my Workers stats, I'm seeing requests coming in (both cached and uncached (for the pages that I'm visiting for the first time since implementation)): image

The x-html-edge-cache-version never increments up, despite the fact that I have the plugin installed on my WP site. I'm also not seeing the x-HTML-Edge-Cache header for some reason?

Worth noting, I am also using W3TC on my site, but I'm seeing the headers change on refresh/consecutive visits.

Any ideas? 😁

willstocks commented 5 years ago

Also, I noticed that wordpress_test_cookie can also resulting in bypassing of the worker cache

pmeenan commented 5 years ago

I can explain parts of it anyway....

The worker serves the cached HTML immediately and then asynchronously updates the cached version from the origin when it doesn't see the headers from the plugin. That way if the plugin isn't installed and purge isn't working it will still stay up to date. That's what "Hit, Refreshed" refers to. This is likely because of W3TC which serves the HTML from disk directly from the web server and never gets a chance to add the header (which is also why you don't see it). If you are sure the plugin is installed and purging you can disable the code here.

-1 is normal for the version number when KV isn't used and it doesn't change. It is only used with KV.

As far as the worker stats go for subrequests, that's not unexpected if it is refreshing the cahced items. The main dashboard should have a view of the cache hit rate for the top-level requests.

willstocks commented 5 years ago

OK, that makes sense! :)

So the plugin is 100% installed and activated on my site... is there a clear and simple way to verify that it is actually doing what it should be doing? In terms of the W3TC page caching, I'm currently using APCu (not Disk) for the page caching: image

OK, good to know on the -1 front! I'm assuming (although the Cloudflare documentation is a bit inconsistent at the moment) that KV does not come along with the free plan (of which I'm currently using) and requires the paid for workers as a minimum. Some documentation says it's included, other documentation says it's not.

As a slight side note/to clarify - do I also need a page rule applied, to allow for full/optimal functionality or can that now be completely ignored as is replaced in its entirety by the worker (which takes in to account the cookies)? image

pmeenan commented 5 years ago

You shouldn't need a Edge Cache TTL page rule. I'd recommend Keeping the Cahce Everything and adding "Origin Cache Control" to it though. That tells the CF edge to honor the caching headers from the origin (for things other than the HTML that the worker takes care of).

On Fri, Aug 2, 2019 at 9:40 AM willstocks-tech notifications@github.com wrote:

OK, that makes sense! :)

So the plugin is 100% installed and activated on my site... is there a clear and simple way to verify that it is actually doing what it should be doing? In terms of the W3TC page caching, I'm currently using APCu (not Disk) for the page caching: [image: image] https://user-images.githubusercontent.com/43744514/62373803-7a846500-b532-11e9-84d9-5ee10ee8ba2e.png

OK, good to know on the -1 front! I'm assuming (although the Cloudflare documentation is a bit inconsistent at the moment) that KV does not come along with the free plan (of which I'm currently using) and requires the paid for workers as a minimum. Some documentation says it's included, other documentation says it's not.

As a slight side note/to clarify - do I also need a page rule applied, to allow for full/optimal functionality or can that now be completely ignored as is replaced in its entirety by the worker (which takes in to account the cookies)? [image: image] https://user-images.githubusercontent.com/43744514/62374207-5a08da80-b533-11e9-868a-cd856eaa4ba9.png

— You are receiving this because you commented. Reply to this email directly, view it on GitHub https://github.com/cloudflare/worker-examples/issues/41?email_source=notifications&email_token=AADMOBNJIISTC5L6FL2NAKDQCQ2NXA5CNFSM4II4T432YY3PNVWWK3TUL52HS4DFVREXG43VMVBW63LNMVXHJKTDN5WW2ZLOORPWSZGOD3NYZGA#issuecomment-517704856, or mute the thread https://github.com/notifications/unsubscribe-auth/AADMOBM6PPK56QII2KKAON3QCQ2NXANCNFSM4II4T43Q .

willstocks commented 5 years ago

Thanks @pmeenan! Updated Page Rule and have applied, will monitor from there as well :)

As an aside, I will log a ticket with the guys at W3TC to see if they can add some form of support for this implementation.

One final question (slightly off topic!), my site runs a service-worker (network first) - is there anything that needs to be included within that so that everything runs perfectly alongside the CF Worker?

pmeenan commented 5 years ago

The service-worker and Cloudflare worker should iteroperate fine. As far as the SW is concerned, the CW is just the web server serving the content. Think of it more like a version of W3TC, just running at the edge.

On Fri, Aug 2, 2019 at 9:54 AM willstocks-tech notifications@github.com wrote:

Thanks @pmeenan https://github.com/pmeenan! Updated Page Rule and have applied, will monitor from there as well :)

As an aside, I will log a ticket with the guys at W3TC to see if they can add some form of support for this implementation.

One final question (slightly off topic!), my site runs a service-worker (network first) - is there anything that needs to be included within that so that everything runs perfectly alongside the CF Worker?

— You are receiving this because you were mentioned. Reply to this email directly, view it on GitHub https://github.com/cloudflare/worker-examples/issues/41?email_source=notifications&email_token=AADMOBIH4GFJERZSLPNJUKLQCQ4DHA5CNFSM4II4T432YY3PNVWWK3TUL52HS4DFVREXG43VMVBW63LNMVXHJKTDN5WW2ZLOORPWSZGOD3NZ7VY#issuecomment-517709783, or mute the thread https://github.com/notifications/unsubscribe-auth/AADMOBMWSE3NYZJD3VHDC33QCQ4DHANCNFSM4II4T43Q .

willstocks commented 5 years ago

Thanks @pmeenan - is it worthwhile me disabling W3TC Page Caching (only, object, database and browser would still remain) in place of the provided plugin?

pmeenan commented 5 years ago

You can try and see if the headers start working. If so then the CF edge should absorb just about all of the traffic that the page case would be serving and it might be better to have it off.

On Fri, Aug 2, 2019 at 10:00 AM willstocks-tech notifications@github.com wrote:

Thanks @pmeenan https://github.com/pmeenan - is it worthwhile me disabling W3TC Page Caching (only, object, database and browser would still remain) in place of the provided plugin?

— You are receiving this because you were mentioned. Reply to this email directly, view it on GitHub https://github.com/cloudflare/worker-examples/issues/41?email_source=notifications&email_token=AADMOBLIHFZFXCOPXDLOT3TQCQ4WTA5CNFSM4II4T432YY3PNVWWK3TUL52HS4DFVREXG43VMVBW63LNMVXHJKTDN5WW2ZLOORPWSZGOD3N2NRY#issuecomment-517711559, or mute the thread https://github.com/notifications/unsubscribe-auth/AADMOBN4VWPUYMRJIF3O4T3QCQ4WTANCNFSM4II4T43Q .

willstocks commented 5 years ago

Have disabled W3TC cache and now I'm seeing the following on my home page consistently.

x-html-edge-cache: cache,bypass-cookies=wp-|wordpress|comment_|woocommerce_
x-html-edge-cache-status: Miss
x-html-edge-cache-version: -1

I have just cleared the comment cookies (because I was making sure pages wouldn't cache with comment form data) and I'm still seeing the same on the home page, but now not on the individual post pages (x-html-edge-cache-status: miss, cached) and then hit on next visit

willstocks commented 5 years ago

@pmeenan also FYI: https://wordpress.org/support/topic/potential-support-for-cloudflare-worker-caching-plugin/ :D

pmeenan commented 5 years ago

That would be awesome if they built a direct integration (or published their own worker to use).

On Fri, Aug 2, 2019 at 11:42 AM willstocks-tech notifications@github.com wrote:

@pmeenan https://github.com/pmeenan also FYI: https://wordpress.org/support/topic/potential-support-for-cloudflare-worker-caching-plugin/ :D

— You are receiving this because you were mentioned. Reply to this email directly, view it on GitHub https://github.com/cloudflare/worker-examples/issues/41?email_source=notifications&email_token=AADMOBIWWC7ILI24UEGD5EDQCRIXLA5CNFSM4II4T432YY3PNVWWK3TUL52HS4DFVREXG43VMVBW63LNMVXHJKTDN5WW2ZLOORPWSZGOD3ODSAI#issuecomment-517748993, or mute the thread https://github.com/notifications/unsubscribe-auth/AADMOBLPBTVZVNNULTT2KJLQCRIXLANCNFSM4II4T43Q .

willstocks commented 5 years ago

Great news @pmeenan - one of the guys from Cloudflare (CC: @signalnerve) reached out to W3TC as well on the support req. I logged 😁 Very exciting stuff here!!!

As an aside... any idea on why I might just see a straight up "miss" response for my homepage? https://github.com/cloudflare/worker-examples/issues/41#issuecomment-517717822 - never seems to cache from there-on-in 😞

lauragift21 commented 1 year ago

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.