johnpoint

johnpoint

(。・∀・)ノ゙嗨
github

Migrating a blog to Cloudflare: Pitfalls to watch out for

Long time no see. Since the beginning of the new year, I have been busy with my graduation project and finding a new job for my internship. I haven't had time to document some of the tinkering I've done as blog posts. Recently, during a break from working on my graduation project, I finally optimized my blog and decided to write a blog post about it.

In the past, my blog was deployed using multiple nodes and traffic routing was done through dnspod's regional DNS resolution to direct traffic to the nearest node to ensure fast access to the blog. Initially, the synchronization of blog files between multiple nodes was done through scheduled tasks pulling from GitHub. Later, I switched to using syncthing for synchronization. This method may seem a bit silly, but it has consistently ensured smooth access to my blog over the past two years.

Recently, while browsing sukkad's blog, I learned about Cloudflare Workers, which can be used in conjunction with KV storage to deploy static websites. So I decided to first deploy my self-hosted image service (which is an HTTP service that stores images) on Cloudflare. The speed and latency during testing were quite good, so I decided to completely migrate my blog to Cloudflare.

This way, I can ensure high reliability and relatively good response speed for my blog without actively tinkering with it.

404 Page Issue#

In the worker-site/index.js file, there is a logic that returns /404.html when the file cannot be found in the URL. However, after deploying it, I noticed something was wrong. The 404 page was displayed as the source code instead of being rendered by the browser.

Using the F12 method to view the network response, I found that the response data was missing a header specifying the response format.

To fix this issue, you just need to add the appropriate header when returning the response. You can refer to the PR I submitted for the code changes.

Loading...
Ownership of this post data is guaranteed by blockchain and smart contracts to the creator alone.