From 1d0897b69acaf7f7b574d03e21d70b24c6171248 Mon Sep 17 00:00:00 2001 From: RoomWithOutRoof <166608075+Jah-yee@users.noreply.github.com> Date: Tue, 14 Apr 2026 06:12:27 +0800 Subject: [PATCH] Fix hardcoded production URLs to use request origin The handlePng and handlePageWithOgTags functions hardcoded https://codewar.dev, which breaks self-hosted deployments: - PNG generation fetched SVG from production instead of the current Worker origin, causing unnecessary external round-trips and returning wrong content for non-production deployments - OG/Twitter meta tags pointed to production URLs, so social previews for self-hosted instances would link back to codewar.dev Derive origin from the incoming request URL instead. Made-with: Cursor --- src/index.ts | 10 ++++++---- 1 file changed, 6 insertions(+), 4 deletions(-) diff --git a/src/index.ts b/src/index.ts index 3ffdd1c..93ea9ae 100644 --- a/src/index.ts +++ b/src/index.ts @@ -153,9 +153,10 @@ async function handlePng( } // Use Cloudflare Browser Rendering to screenshot the SVG + const origin = new URL(request.url).origin; const selfParam = selfUser ? `&self=${selfUser}` : ""; const themeParam = theme === "dark" ? `&theme=dark` : ""; - const svgUrl = `https://codewar.dev/api/svg?users=${usernames.join(",")}&range=${range}${selfParam}${themeParam}`; + const svgUrl = `${origin}/api/svg?users=${usernames.join(",")}&range=${range}${selfParam}${themeParam}`; const browser = await puppeteer.launch(env.BROWSER); const page = await browser.newPage(); @@ -261,10 +262,11 @@ async function handlePageWithOgTags( return env.ASSETS.fetch(request); } - // Build image URLs + // Build image URLs using current origin (works for self-hosted deployments) + const origin = new URL(request.url).origin; const selfParam = selfUser ? `&self=${selfUser}` : ""; - const pngUrl = `https://codewar.dev/api/png?users=${allUsers.join(",")}&range=${range}${selfParam}`; - const pageUrl = `https://codewar.dev/${url.search}`; + const pngUrl = `${origin}/api/png?users=${allUsers.join(",")}&range=${range}${selfParam}`; + const pageUrl = `${origin}/${url.search}`; const ogTags = `