Skip to content

Commit 8d0d6bc

Browse files
committed
added sitemap.xml
1 parent b0695ee commit 8d0d6bc

File tree

1 file changed

+55
-1
lines changed

1 file changed

+55
-1
lines changed

cloudflare-worker.js

Lines changed: 55 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ async function generateOGTags(url, SITE_URL, DOCS_URL) {
4040
console.log(metadataMatch);
4141
const thumbnailMatch = metadataStr.match(/thumbnail:\s*(.*)/);
4242
if (thumbnailMatch) {
43-
thumbnail = thumbnailMatch[1].trim();
43+
thumbnail = "docs/" + thumbnailMatch[1].trim();
4444
}
4545
const descriptionMatch = metadataStr.match(/description:\s*(.*)/);
4646
if (descriptionMatch) {
@@ -118,10 +118,64 @@ async function handleRequest(request, SITE_URL, DOCS_URL) {
118118
return new Response(response.body, response);
119119
}
120120

121+
async function generateSitemap(DOCS_URL, SITE_URL) {
122+
const indexResponse = await fetch(`${DOCS_URL}/index.json`);
123+
const indexData = await indexResponse.json();
124+
125+
function collectUrls(documents) {
126+
let urls = [];
127+
for (const doc of documents) {
128+
if (doc.slug) {
129+
const url = new URL(SITE_URL);
130+
// Fix: Directly append the slug to search without using searchParams
131+
url.search = `?${doc.slug}`; // This creates clean URLs without =
132+
urls.push({
133+
loc: url.href,
134+
lastmod: doc.lastModified || new Date().toISOString().split('T')[0],
135+
priority: doc.type === 'folder' ? '0.8' : '0.6'
136+
});
137+
}
138+
if (doc.type === 'folder' && doc.items) {
139+
urls = urls.concat(collectUrls(doc.items));
140+
}
141+
}
142+
return urls;
143+
}
144+
145+
const urls = collectUrls(indexData.documents);
146+
147+
const sitemap = `<?xml version="1.0" encoding="UTF-8"?>
148+
<urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9"
149+
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
150+
xsi:schemaLocation="http://www.sitemaps.org/schemas/sitemap/0.9
151+
http://www.sitemaps.org/schemas/sitemap/0.9/sitemap.xsd">
152+
${urls.map(url => `
153+
<url>
154+
<loc>${url.loc}</loc>
155+
<lastmod>${url.lastmod}</lastmod>
156+
<priority>${url.priority}</priority>
157+
</url>`).join('')}
158+
</urlset>`;
159+
160+
return new Response(sitemap.trim(), {
161+
headers: {
162+
'Content-Type': 'application/xml',
163+
'Cache-Control': 'public, max-age=3600'
164+
}
165+
});
166+
}
167+
121168
export default {
122169
async fetch(request, env, ctx) {
123170
const SITE_URL = env.SITE_URL;
124171
const DOCS_URL = env.DOCS_URL;
172+
const url = new URL(request.url);
173+
174+
// Fix: Check if path ends with sitemap.xml regardless of base path
175+
if (url.pathname === '/sitemap.xml' || url.pathname.endsWith('/sitemap.xml')) {
176+
return generateSitemap(DOCS_URL, SITE_URL);
177+
}
178+
125179
return handleRequest(request, SITE_URL, DOCS_URL);
126180
}
127181
};

0 commit comments

Comments
 (0)