-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathworker.js
More file actions
32 lines (26 loc) · 1.28 KB
/
worker.js
File metadata and controls
32 lines (26 loc) · 1.28 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
export default {
async fetch(request, env, ctx) {
const url = new URL(request.url);
const path = url.pathname;
// 1. Ignore the homepage, assets, and existing date-based URLs
if (path === "/" || path.includes("/20") || path.includes(".")) {
return fetch(request);
}
// 2. Define your logic: Since Blogger requires /YYYY/MM/
// We try to fetch the content from Blogger.
// If the clean path hits a 404, we redirect to the original.
const response = await fetch(request);
// 3. If the clean URL 404s, we try to find the match proxy the content from the original Blogger feed.
if (response.status === 404) {
const rssUrl = `https://YOUR_CUSTOM_BLOG_URL/feeds/posts/default?alt=json&q=${path.replace(/\//g, "")}`;
const feedResponse = await fetch(rssUrl);
const data = await feedResponse.json();
if (data.feed.entry && data.feed.entry.length > 0) {
const originalUrl = data.feed.entry[0].link.find(l => l.rel === 'alternate').href;
const originalResponse = await fetch(originalUrl);
return new Response(originalResponse.body, originalResponse);
}
}
return response;
},
};