Cloudflare Snippet Reverse Proxy for Blogspot
Preface
Recently I saw someone using cloudflare's snippet to implement node functionality similar to worker.Idea
It seems,
On one hand, free users now have the opportunity to use snippets.
On the other hand, the functionality of snippets has been enhanced, making the difference with worker even smaller.
So I want to use snippet to implement the reverse proxy for blogspot that I previously did with worker.
Practice
The syntax in uniproxy is relatively old, it's like this
addEventListener('fetch', event => {
event.respondWith(handleRequest(event.request))
})
When used in snippet, it should be changed to this
export default {
async fetch(request, env, ctx) {
try {
return await handleRequest(request, env, ctx);
} catch (e) {
return new Response(e.message || "Internal Error", { status: 500 });
}
},
};
I let GPT do the conversion.
From the result, apart from this one change, all other code remains exactly the same.
Uploaded to Github
Worker code for reverse proxying blogspot
Send the requirement to GPT
When I use the same code for both worker and snippet,
Is there a way to identify whether the current environment is worker or snippet, so as to enable or disable KV-related code logic?
The result is to check whether the environment variable for the bound KV exists.
This logic itself is quite good, even when used for worker, it can avoid exceptions when the user has not bound KV in the worker environment.
Uploaded to Github
Demo site
https://blog.icdyct.nyc.mn/========
Postscript
This time (2025-11-24) the GPT used was
Gemini's knowledge about snippet is not up-to-date.
When I asked about the following usage, Gemini said snippet doesn't support it, only worker does. But actually now snippet supports this usage.
export default {
async fetch(request, env, ctx) {
try {
return await handleRequest(request, env, ctx);
} catch (e) {
return new Response(e.message || "Internal Error", { status: 500 });
}
},
};
========
Related Recommendations
《Set up Workers Routes for domain, block paths where worker usage exceeds limit abnormally》
《Display post labels on Blogger homepage》
