From 5ee371498add4be44645a0a61e4f6a7b3a091f13 Mon Sep 17 00:00:00 2001 From: Tomas Valenta Date: Sat, 22 Mar 2025 13:03:57 -0700 Subject: [PATCH] fix: clean up middleware rewrite fetch + use next native rewrite response + prefix landing page hostname with www Simplify fetch Remove headers Add rest of headers Explicitly define fetch options Remove header manipulation Test all custom config Remove out cache header Remove no cache in header Remove cache Tweak fetch options Remove credentials Remove referrer Return to basic setup Add redirect handling Stop caching Remove redirect Remove no cache Return old settings Proxy everything Modify hostname Change domain Change hostname back Remove caching Change rewrite mechanism Add more headers Remove the rewrite Follow redirect Try www Add log Try just rewrite Only pass get requests Fix error Change headers passing Print headers Test pure rewrite Remove unused code Add html replace for ai agents list Remove logs --- apps/web/src/app/hostnames.ts | 2 +- apps/web/src/middleware.ts | 35 +++++++++++++++++++++++------------ 2 files changed, 24 insertions(+), 13 deletions(-) diff --git a/apps/web/src/app/hostnames.ts b/apps/web/src/app/hostnames.ts index 1ee4e08937..3abc62e1c7 100644 --- a/apps/web/src/app/hostnames.ts +++ b/apps/web/src/app/hostnames.ts @@ -1,5 +1,5 @@ // We are in the process of migrating to the new landing page hosting. -export const landingPageHostname = 'e2b-landing-page.com' +export const landingPageHostname = 'www.e2b-landing-page.com' export const landingPageFramerHostname = 'e2b-landing-page.framer.website' export const blogFramerHostname = 'e2b-blog.framer.website' export const changelogFramerHostname = 'e2b-changelog.framer.website' diff --git a/apps/web/src/middleware.ts b/apps/web/src/middleware.ts index 344dac7cbf..01e6310ee0 100644 --- a/apps/web/src/middleware.ts +++ b/apps/web/src/middleware.ts @@ -1,6 +1,6 @@ import { NextRequest, NextResponse } from 'next/server' -import { replaceUrls } from '@/utils/replaceUrls' import { landingPageHostname, landingPageFramerHostname } from '@/app/hostnames' +import { replaceUrls } from '@/utils/replaceUrls' export async function middleware(req: NextRequest): Promise { if (req.method !== 'GET') return NextResponse.next() @@ -48,23 +48,34 @@ export async function middleware(req: NextRequest): Promise { } } + const headers = new Headers(req.headers) + // TODO: Not on the new landing page hosting yet if (url.pathname.startsWith('/ai-agents')) { url.hostname = landingPageFramerHostname - } - const res = await fetch(url.toString(), { ...req }) + const res = await fetch(url.toString(), { + ...req, + headers, + redirect: 'follow', + }) - const htmlBody = await res.text() + const htmlBody = await res.text() - // !!! NOTE: Replace has intentionally not completed quotes to catch the rest of the path !!! - const modifiedHtmlBody = replaceUrls(htmlBody, url.pathname, 'href="', '">') + // !!! NOTE: Replace has intentionally not completed quotes to catch the rest of the path !!! + const modifiedHtmlBody = replaceUrls(htmlBody, url.pathname, 'href="', '">') - return new NextResponse(modifiedHtmlBody, { - status: res.status, - statusText: res.statusText, - headers: res.headers, - url: req.url, + return new NextResponse(modifiedHtmlBody, { + status: res.status, + statusText: res.statusText, + headers: res.headers, + url: req.url, + }) + } + + return NextResponse.rewrite(url.toString(), { + ...req, + headers, }) } @@ -80,4 +91,4 @@ export const config = { '/pricing/:path*', '/cookbook/:path*', ], -} +} \ No newline at end of file