Branchwell

Become a Pro / website · becomeapro.ai / cloudflare-worker.mjs

Raw 1.6 KB on 8e2ed58faade31e8c0f006487134af60b0720a7d
1import handler from './.open-next/worker.js';
2import config from './next.config.js';
3
4const rules = await config.headers();
5const siteRule = rules.find(rule => rule.source === '/(.*)');
6
7// One home: the old AI Resource Pro addresses and www all move to becomeapro.ai, keeping the path and query.
8const CANONICAL = 'becomeapro.ai';
9// Next pre-renders catalog routes without trailing slashes. Normalize public
10// library links here before looking up a static page; leave API and assets alone.
11const LIBRARY_WITH_SLASH = /^\/(?:practice|(?:trades|knowledge-work|health-care|driving-and-logistics|service|english|math|teaching)(?:\/[a-z0-9-]+){0,2})\/$/;
12const OLD_HOSTS = new Set(['www.becomeapro.ai', 'airesourcepro.com', 'www.airesourcepro.com', 'airesource.pro', 'www.airesource.pro']);
13
14export default {
15 async fetch(request, env, ctx) {
16 const url = new URL(request.url);
17 if (OLD_HOSTS.has(url.hostname)) {
18 return Response.redirect(`https://${CANONICAL}${url.pathname}${url.search}`, 301);
19 }
20 if ((request.method === 'GET' || request.method === 'HEAD') && LIBRARY_WITH_SLASH.test(url.pathname)) {
21 url.pathname = url.pathname.slice(0, -1);
22 return Response.redirect(url.href, 308);
23 }
24 const response = await handler.fetch(request, env, ctx);
25 const headers = new Headers(response.headers);
26 const routeRule = rules.find(rule => rule.source === url.pathname && !rule.has);
27 for (const rule of [siteRule, routeRule]) {
28 for (const { key, value } of rule?.headers ?? []) headers.set(key, value);
29 }
30
31 return new Response(response.body, { status: response.status, statusText: response.statusText, headers });
32 },
33};