From 4ddc86ae246c8d3c13b3b222b240975e6d816f45 Mon Sep 17 00:00:00 2001 From: RockerFlower Date: Mon, 2 Dec 2024 13:04:09 +0900 Subject: [PATCH] Add middleware --- functions/_middleware.ts | 26 ++++++++++++++++++++++++++ 1 file changed, 26 insertions(+) create mode 100644 functions/_middleware.ts diff --git a/functions/_middleware.ts b/functions/_middleware.ts new file mode 100644 index 0000000..029f347 --- /dev/null +++ b/functions/_middleware.ts @@ -0,0 +1,26 @@ +// functions/_middleware.ts +export async function onRequest({ request, next }: any) { + const url = new URL(request.url); + + // If the hostname ends with poap.in + if (url.hostname.endsWith('poap.in')) { + const subdomain = url.hostname.split('.')[0]; + if (subdomain !== 'www' && subdomain !== '') { + // Rewrite the path + url.pathname = `/v/${subdomain}.eth${url.pathname}`; + request = new Request(url, request); + } + } + + // If the request is for assets or API, proxy to the original site + if (url.pathname.startsWith('/assets/') || url.pathname.startsWith('/api/')) { + const targetUrl = `https://poap.in${url.pathname}${url.search}`; + return fetch(targetUrl, { + method: request.method, + headers: request.headers, + body: request.body, + }); + } + + return next(); + } \ No newline at end of file