Skip to content

Commit

Permalink
Add middleware
Browse files Browse the repository at this point in the history
  • Loading branch information
RockerFlower committed Dec 2, 2024
1 parent 65ddbe1 commit 4ddc86a
Showing 1 changed file with 26 additions and 0 deletions.
26 changes: 26 additions & 0 deletions functions/_middleware.ts
Original file line number Diff line number Diff line change
@@ -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();
}

0 comments on commit 4ddc86a

Please sign in to comment.