diff options
| author | Alex Pooley (@zuedev) <zuedev@gmail.com> | 2025-12-19 13:54:30 +0000 |
|---|---|---|
| committer | Alex Pooley (@zuedev) <zuedev@gmail.com> | 2025-12-19 13:54:30 +0000 |
| commit | 6805b9db95209711cb684d113dde07a96fe9dbb8 (patch) | |
| tree | 87ddf256613c2319119d623ee549a04ec68614dc /server | |
| parent | b9d42d5c6102800688c08cd77de18b1b40255ba7 (diff) | |
| download | zue.dev-6805b9db95209711cb684d113dde07a96fe9dbb8.tar zue.dev-6805b9db95209711cb684d113dde07a96fe9dbb8.tar.gz zue.dev-6805b9db95209711cb684d113dde07a96fe9dbb8.tar.bz2 zue.dev-6805b9db95209711cb684d113dde07a96fe9dbb8.tar.xz zue.dev-6805b9db95209711cb684d113dde07a96fe9dbb8.zip | |
Enhance redirect and rewrite configuration for improved request handling
Diffstat (limited to 'server')
| -rw-r--r-- | server/index.js | 64 |
1 files changed, 54 insertions, 10 deletions
diff --git a/server/index.js b/server/index.js index c49529e..4acb7f0 100644 --- a/server/index.js +++ b/server/index.js @@ -1,6 +1,46 @@ import puppeteer from "@cloudflare/puppeteer"; const configuration = { + /* An array of redirect objects that define domain-based redirects. + * + * Each redirect object must contain the following properties: + * - domain: The source domain to match. + * - destination: The target URL to redirect to. + * + * Optional properties: + * - appendPath: If true, appends the original request path to the destination URL. + * - appendQuery: If true, appends the original request query string to the destination URL. + */ + redirects: [ + { + domain: "api.zue.dev", + destination: "https://zue.dev/api/", + appendPath: true, + appendQuery: true, + }, + { + domain: "96.zue.dev", + destination: "https://zue.dev/96/", + appendPath: true, + }, + { + domain: "about.zue.dev", + destination: "https://zue.dev/about/", + appendPath: true, + }, + { + domain: "bbg.zue.dev", + destination: "https://zue.dev/bbg/", + appendPath: true, + }, + ], + /* An array of rewrite objects that define domain and path-based rewrites. + * + * Each rewrite object must contain the following properties: + * - domain: The source domain to match. + * - path: The source path to match. + * - destination: The target URL to fetch and serve the content from. + */ rewrites: [ { domain: "zue.dev", @@ -25,20 +65,24 @@ export default { async fetch(request, environment, context) { const url = new URL(request.url); - if (url.hostname === "api.zue.dev") - return Response.redirect( - `https://zue.dev/api/${url.pathname}${url.search}`, - 301 + // Handle redirects from configuration object + if ( + configuration.redirects && + configuration.redirects.length > 0 && + configuration.redirects.some((r) => r.domain === url.hostname) + ) { + const redirect = configuration.redirects.find( + (r) => r.domain === url.hostname ); - if (url.hostname === "96.zue.dev") - return Response.redirect(`https://zue.dev/96/${url.pathname}`, 301); + let destination = redirect.destination; + + if (redirect.appendPath) destination += url.pathname; - if (url.hostname === "about.zue.dev") - return Response.redirect(`https://zue.dev/about/${url.pathname}`, 301); + if (redirect.appendQuery) destination += url.search; - if (url.hostname === "bbg.zue.dev") - return Response.redirect(`https://zue.dev/bbg/${url.pathname}`, 301); + return Response.redirect(destination, 301); + } // Handle rewrites from configuration object if ( |
