(request: Request, env: Env)
| 89 | |
| 90 | export default { |
| 91 | async fetch(request: Request, env: Env): Promise<Response> { |
| 92 | const url = new URL(request.url); |
| 93 | |
| 94 | // Serve static assets with per-route meta tag rewriting for SEO |
| 95 | const response = await env.ASSETS.fetch(request); |
| 96 | |
| 97 | // Only rewrite HTML responses (SPA pages, not JS/CSS/images) |
| 98 | const contentType = response.headers.get("content-type") || ""; |
| 99 | if (!contentType.includes("text/html")) { |
| 100 | return response; |
| 101 | } |
| 102 | |
| 103 | const path = url.pathname === "" ? "/" : url.pathname; |
| 104 | // Normalize: strip trailing slash (except root) |
| 105 | const normalizedPath = |
| 106 | path !== "/" && path.endsWith("/") ? path.slice(0, -1) : path; |
| 107 | const meta = ROUTE_META[normalizedPath] || ROUTE_META["/"]; |
| 108 | const canonical = `https://pollinations.ai${normalizedPath === "/" ? "" : normalizedPath}`; |
| 109 | const jsonLd = getJsonLd(normalizedPath); |
| 110 | |
| 111 | return new HTMLRewriter() |
| 112 | .on("title", { |
| 113 | element(el) { |
| 114 | el.setInnerContent(meta.title); |
| 115 | }, |
| 116 | }) |
| 117 | .on('link[rel="canonical"]', { |
| 118 | element(el) { |
| 119 | el.setAttribute("href", canonical); |
| 120 | }, |
| 121 | }) |
| 122 | .on('meta[name="description"]', { |
| 123 | element(el) { |
| 124 | el.setAttribute("content", meta.description); |
| 125 | }, |
| 126 | }) |
| 127 | .on('meta[property="og:title"]', { |
| 128 | element(el) { |
| 129 | el.setAttribute("content", meta.title); |
| 130 | }, |
| 131 | }) |
| 132 | .on('meta[property="og:description"]', { |
| 133 | element(el) { |
| 134 | el.setAttribute("content", meta.description); |
| 135 | }, |
| 136 | }) |
| 137 | .on('meta[property="og:url"]', { |
| 138 | element(el) { |
| 139 | el.setAttribute("content", canonical); |
| 140 | }, |
| 141 | }) |
| 142 | .on('meta[name="twitter:title"]', { |
| 143 | element(el) { |
| 144 | el.setAttribute("content", meta.title); |
| 145 | }, |
| 146 | }) |
| 147 | .on('meta[name="twitter:description"]', { |
| 148 | element(el) { |
no test coverage detected