From 219ab574db10807c028f2e6726825e082f5496e2 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Kamil=20Og=C3=B3rek?= Date: Sat, 17 Jun 2023 19:13:04 +0200 Subject: [PATCH] fix: Exclude Twitter/Slack bots from root page redirects (#1318) --- www/routes/index.tsx | 10 +++++++++- 1 file changed, 9 insertions(+), 1 deletion(-) diff --git a/www/routes/index.tsx b/www/routes/index.tsx index 6fdd4591..03cc8784 100644 --- a/www/routes/index.tsx +++ b/www/routes/index.tsx @@ -11,10 +11,18 @@ import Projects from "../components/Projects.tsx"; import projects from "../data/showcase.json" assert { type: "json" }; import Header from "../components/Header.tsx"; +function isOpenGraphUA(header: string | null): boolean { + if (!header) { + return false; + } + return header.startsWith("Twitterbot") || header.startsWith("Slackbot"); +} + export const handler: Handlers = { GET(req, ctx) { const accept = req.headers.get("accept"); - if (accept && !accept.includes("text/html")) { + const userAgent = req.headers.get("user-agent"); + if (!accept?.includes("text/html") && !isOpenGraphUA(userAgent)) { const path = `https://deno.land/x/fresh@${VERSIONS[0]}/init.ts`; return new Response(`Redirecting to ${path}`, { headers: { "Location": path },