MCPcopy Create free account
hub / github.com/pollinations/pollinations / fetch

Function fetch

pollinations.ai/src/worker.ts:91–163  ·  view source on GitHub ↗
(request: Request, env: Env)

Source from the content-addressed store, hash-verified

89
90export 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) {

Callers 15

fetchWithTimeoutFunction · 0.50
authorizeDeviceMethod · 0.50
pollMethod · 0.50
fetchJsonFunction · 0.50
fetchAccountJsonFunction · 0.50
fetchCachedFunction · 0.50
fetchWithAuthFunction · 0.50
generateImageUrlFunction · 0.50
generateVideoUrlFunction · 0.50
webSearchFunction · 0.50
fetchLlmTxtFunction · 0.50
fetchModelStatsFunction · 0.50

Calls 5

getJsonLdFunction · 0.85
sliceMethod · 0.80
fetchMethod · 0.65
getMethod · 0.65
transformMethod · 0.65

Tested by

no test coverage detected