大网数据广告活动
网站运营与故障问答

Cloudflare: Cache static HTML when URL has Query String

· 52CE 我爱测

Quick CloudFlare 'new user' question for you: BACKGROUND: The HTML pages served up by my origin server never change. All dynamic content is download via JavaScript after the page is loaded in the user's browser. The dynamic conte…

问题原文

原帖链接 · Festus Martingale · CC BY-SA 3.0

Quick CloudFlare 'new user' question for you:

BACKGROUND: The HTML pages served up by my origin server never change. All dynamic content is download via JavaScript after the page is loaded in the user's browser. The dynamic content derived by the JavaScript depends in part upon the URL's query string but the query string itself has no affect on the HTML content that's originally downloaded. The following URLs therefore all generate exactly the same HTML from the origin prior to being acted upon by JavaScript in the browser:

https://www.example.com/private/my-investments?portfolioID=296

https://www.example.com/private/my-investments?portfolioID=325

https://www.example.com/private/my-investments?portfolioID=319

QUESTION: Given that CloudFlare does not cache HTML by default, how shall I construct my Page Rules so that a single cached copy of https://www.example.com/private/my-investments?[anyValue] is returned to all users? Here's what I've tried:

"CACHE EVERYTHING:" Using the 'Cache Everything' directive causes my HTML to be cached (good) but it uses the entire query string as part of the cache key (bad). So if a user hits the following URL...

https://www.example.com/private/my-investments?portfolioID=296

...and then ten seconds later hits the same URL with a different query string, like:

https://www.example.com/private/my-investments?portfolioID=325

...CloudFlare treats that latter request as a cache MISS (cf-cache-status:MISS) and hits my origin server again. This in spite of the fact that the HTML returned from the origin is identical for both URLs.

"IGNORE QUERY STRING CACHING:" I thought for sure I'd fix this problem by selecting the "IGNORE QUERY STRING CACHING" option. But when using this option in a Page Rule, no HTML is cached at all; Only .js, .css, and .png files are cached. And once again my origin gets hit for every single call for HTML data. :(

I feel like I must be missing something obvious. Any thoughts?

UPDATE: Had an idea I want to run by you. Could I solve this problem by changing my internal URL scheme to use '#' instead of '?' So instead of a URL like...

https://www.example.com/private/my-investments?portfolioID=296

..I'd use something like:

https://www.example.com/private/my-investments#portfolioID=296

This would prevent the query string parameters from being transmitted to CloudFlare at all... What do you think?

Festus

已采纳解答

原帖链接 · Chris Rutherfurd · CC BY-SA 3.0

(Answer added for @Festus Martingale to mark question as answered).

Replacing the ? with a # solved the problem as it prevents the query string from being transmitted to cloudflare as it appears as a hash fragment.

摘自公开归档,保留原文语言,版式经整理,外部媒体与邮箱地址已移除。

资料来源与适用范围

资料来源
Webmasters Stack Exchange
适用范围
以原帖系统、软件版本与发布日期为准(2016-03-10)
内容语言
中文;命令、路径和错误标识保留技术原文
本地收录日期
2026-11-15
授权说明
CC BY-SA 3.0

正文与所需资源由本站本地存储。查看来源及更新记录 ↗

请按适用版本和实际日志确认原因;本站没有对每一种系统或设备逐项执行文中的操作。