-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmiddleware.ts
More file actions
274 lines (252 loc) · 8.52 KB
/
middleware.ts
File metadata and controls
274 lines (252 loc) · 8.52 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
/**
* Cloudflare Pages Functions Middleware
* Bot Detection + Prerender + Dynamic Sitemaps + Script Injection
*
* Place this file at: functions/_middleware.ts (project root)
* Cloudflare Pages auto-detects it as edge middleware.
*
* REQUIRES environment variables in Pages dashboard:
* - SUPABASE_URL
* - SUPABASE_ANON_KEY
*
* FEATURES:
* 1. Bot detection → serves prerendered HTML from Supabase cache
* 2. Dynamic sitemaps → proxies to Supabase generate-sitemap / serve-sitemap
* 3. Script injection → fetches 3rd-party scripts from script-service edge function
* and injects them into every HTML response (bots + humans)
* This ensures analytics survive AI platform rebuilds and run in first-party context.
*/
const BOT_AGENTS = [
// Search Engines
'googlebot', 'bingbot', 'yandexbot', 'baiduspider', 'duckduckbot',
'slurp', 'sogou', 'exabot', 'ia_archiver',
// AI Crawlers
'gptbot', 'chatgpt-user', 'oai-searchbot',
'claudebot', 'claude-user', 'claude-searchbot',
'google-extended', 'google-cloudvertexbot', 'gemini-deep-research',
'perplexitybot', 'perplexity-user',
'meta-externalagent', 'meta-webindexer',
'bytespider', 'amazonbot', 'duckassistbot',
'mistralai-user', 'cohere-ai', 'ccbot', 'diffbot', 'webzio', 'icc-crawler',
// Social Media
'facebookexternalhit', 'facebot', 'twitterbot', 'linkedinbot',
'pinterest', 'whatsapp', 'telegrambot', 'slackbot', 'discordbot',
'vkshare', 'redditbot', 'tumblr', 'embedly', 'quora link preview', 'outbrain',
// SEO Tools
'semrushbot', 'ahrefsbot', 'mj12bot', 'dotbot', 'rogerbot',
'screaming frog', 'seokicks', 'blexbot', 'siteexplorer', 'serpstatbot',
// Apple & Other
'applebot', 'applebot-extended', 'petalbot', 'seznambot',
'naver', 'yeti', 'qwantify', 'ecosia', 'mojeek',
// Google Specific
'mediapartners-google', 'adsbot-google', 'feedfetcher-google',
'google-read-aloud', 'storebot-google', 'google-safety',
// Archive & Research
'archive.org_bot', 'wayback', 'wget', 'curl', 'python-requests',
'go-http-client', 'java', 'libwww-perl', 'axios', 'httpie', 'postman',
// Feed Readers
'feedly', 'flipboard', 'newsblur', 'inoreader', 'theoldreader', 'feedbin',
];
interface Env {
SUPABASE_URL: string;
SUPABASE_ANON_KEY: string;
ASSETS: { fetch: (req: Request) => Promise<Response> };
}
interface ScriptCache {
head: string[];
body: string[];
fetchedAt: number;
}
// Module-level cache — persists within each Cloudflare isolate
let scriptCache: ScriptCache | null = null;
const CACHE_TTL_MS = 5 * 60 * 1000; // 5 minutes
function isBot(ua: string): boolean {
if (!ua) return false;
const lower = ua.toLowerCase();
return BOT_AGENTS.some(bot => lower.includes(bot));
}
function isStaticAsset(path: string): boolean {
return /\.(js|css|png|jpg|jpeg|gif|svg|ico|woff2?|ttf|eot|map|json|txt|pdf|mp4|webm|webp|avif)$/i.test(path);
}
/**
* Fetch scripts from the script-service edge function.
* Cached in isolate memory for 5 minutes. Falls back to stale cache or empty.
*/
async function getScripts(env: Env): Promise<{ head: string; body: string }> {
const now = Date.now();
if (scriptCache && (now - scriptCache.fetchedAt) < CACHE_TTL_MS) {
return {
head: scriptCache.head.join('\n'),
body: scriptCache.body.join('\n'),
};
}
try {
const resp = await fetch(
`${env.SUPABASE_URL}/functions/v1/script-service`,
{
headers: {
'Authorization': `Bearer ${env.SUPABASE_ANON_KEY}`,
'apikey': env.SUPABASE_ANON_KEY,
},
}
);
if (resp.ok) {
const data = await resp.json() as { head: string[]; body: string[] };
scriptCache = {
head: data.head || [],
body: data.body || [],
fetchedAt: now,
};
return {
head: scriptCache.head.join('\n'),
body: scriptCache.body.join('\n'),
};
}
} catch (e) {
// If fetch fails, use stale cache or empty
}
if (scriptCache) {
return {
head: scriptCache.head.join('\n'),
body: scriptCache.body.join('\n'),
};
}
return { head: '', body: '' };
}
/**
* Inject script tags into HTML before </head> and </body>.
*/
function injectScripts(html: string, scripts: { head: string; body: string }): string {
if (scripts.head) {
html = html.replace('</head>', `${scripts.head}\n</head>`);
}
if (scripts.body) {
html = html.replace('</body>', `${scripts.body}\n</body>`);
}
return html;
}
export const onRequest: PagesFunction<Env> = async (context) => {
const { request, env, next } = context;
const url = new URL(request.url);
const path = url.pathname;
const userAgent = request.headers.get('user-agent') || '';
// Static assets — pass through
if (isStaticAsset(path)) {
return next();
}
// Debug endpoint
if (path === '/__debug') {
return new Response(JSON.stringify({
middleware: 'pages-middleware',
version: '2.0.0',
hasSupabaseUrl: !!env.SUPABASE_URL,
hasSupabaseKey: !!env.SUPABASE_ANON_KEY,
hostname: url.hostname,
userAgent: userAgent.substring(0, 80),
isBot: isBot(userAgent),
scriptCacheAge: scriptCache ? Math.round((Date.now() - scriptCache.fetchedAt) / 1000) + 's' : 'cold',
timestamp: new Date().toISOString(),
}, null, 2), {
headers: { 'Content-Type': 'application/json' },
});
}
// Fetch scripts (cached in isolate memory)
const scripts = await getScripts(env);
// Dynamic sitemaps — proxy to Supabase generate-sitemap
if (path === '/sitemap-markets.xml' || path === '/sitemap-pillars.xml') {
const type = path.includes('markets') ? 'markets' : 'pillars';
try {
const resp = await fetch(
`${env.SUPABASE_URL}/functions/v1/generate-sitemap?type=${type}`,
{
headers: {
'Authorization': `Bearer ${env.SUPABASE_ANON_KEY}`,
'apikey': env.SUPABASE_ANON_KEY,
},
}
);
if (resp.ok) {
return new Response(await resp.text(), {
status: 200,
headers: {
'Content-Type': 'application/xml; charset=utf-8',
'Cache-Control': 'public, max-age=3600, s-maxage=86400',
'X-Sitemap-Source': 'supabase',
},
});
}
} catch (e) {
// Fall through to SPA
}
}
// Static sitemap shards — proxy to Supabase serve-sitemap
if (/^\/sitemap-(markets|pillars)-\d+\.xml$/.test(path) || path === '/sitemap-index.xml') {
const filename = path.replace(/^\//, '');
try {
const resp = await fetch(
`${env.SUPABASE_URL}/functions/v1/serve-sitemap?file=${encodeURIComponent(filename)}`,
{
headers: {
'Authorization': `Bearer ${env.SUPABASE_ANON_KEY}`,
'apikey': env.SUPABASE_ANON_KEY,
},
}
);
if (resp.ok) {
return new Response(await resp.text(), {
status: 200,
headers: {
'Content-Type': 'application/xml; charset=utf-8',
'Cache-Control': 'public, max-age=1800, s-maxage=3600',
'X-Sitemap-Source': 'supabase-static',
},
});
}
} catch (e) {
// Fall through
}
}
// Bot detection — serve prerendered HTML with scripts injected
if (isBot(userAgent)) {
try {
const prerenderUrl = `${env.SUPABASE_URL}/functions/v1/prerender?path=${encodeURIComponent(path)}`;
const res = await fetch(prerenderUrl, {
headers: {
'Authorization': `Bearer ${env.SUPABASE_ANON_KEY}`,
'apikey': env.SUPABASE_ANON_KEY,
'User-Agent': userAgent,
},
});
if (res.ok) {
const html = injectScripts(await res.text(), scripts);
return new Response(html, {
status: 200,
headers: {
'Content-Type': 'text/html; charset=utf-8',
'X-Prerendered': 'true',
'X-Scripts-Injected': 'true',
'X-Cache': res.headers.get('X-Cache') || 'hit',
'Cache-Control': 'public, max-age=3600',
},
});
}
} catch (e) {
// Fall through to SPA
}
}
// Human (or bot fallback) — serve SPA with scripts injected
const response = await next();
// Only inject into HTML responses
const contentType = response.headers.get('Content-Type') || '';
if (!contentType.includes('text/html')) {
return response;
}
const html = injectScripts(await response.text(), scripts);
return new Response(html, {
status: response.status,
headers: {
...Object.fromEntries(response.headers.entries()),
'X-Scripts-Injected': 'true',
},
});
};