Become a Pro / website · becomeapro.ai / cloudflare-worker.mjs
| 1 | import handler from './.open-next/worker.js'; |
| 2 | import config from './next.config.js'; |
| 3 | |
| 4 | const rules = await config.headers(); |
| 5 | const 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. |
| 8 | const CANONICAL = 'becomeapro.ai'; |
| 9 | const OLD_HOSTS = new Set(['www.becomeapro.ai', 'airesourcepro.com', 'www.airesourcepro.com', 'airesource.pro', 'www.airesource.pro']); |
| 10 | |
| 11 | export default { |
| 12 | async fetch(request, env, ctx) { |
| 13 | const url = new URL(request.url); |
| 14 | if (OLD_HOSTS.has(url.hostname)) { |
| 15 | return Response.redirect(`https://${CANONICAL}${url.pathname}${url.search}`, 301); |
| 16 | } |
| 17 | const response = await handler.fetch(request, env, ctx); |
| 18 | const headers = new Headers(response.headers); |
| 19 | const routeRule = rules.find(rule => rule.source === url.pathname && !rule.has); |
| 20 | for (const rule of [siteRule, routeRule]) { |
| 21 | for (const { key, value } of rule?.headers ?? []) headers.set(key, value); |
| 22 | } |
| 23 | |
| 24 | return new Response(response.body, { status: response.status, statusText: response.statusText, headers }); |
| 25 | }, |
| 26 | }; |