Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
- Notifications
You must be signed in to change notification settings - Fork 43
Expand file tree
/
Copy pathmiddleware.ts
More file actions
Latest commit
50 lines (42 loc) · 1.93 KB
/
Copy pathmiddleware.ts
File metadata and controls
50 lines (42 loc) · 1.93 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
/**
* Vercel Edge Middleware.
*
* The `/` route is rewritten to the Framer-hosted landing page (see
* `vercel.json`), whose HTML declares `zenstack.framer.website` as the
* canonical/og:url. This middleware proxies the page and rewrites those
* URLs to `https://zenstack.dev` so search engines index the real domain.
*
* If anything goes wrong it returns `undefined`, falling through to the
* plain rewrite in `vercel.json` (unmodified page instead of an error).
*/
constFRAMER_ORIGIN='https://zenstack.framer.website';
constCANONICAL_ORIGIN='https://zenstack.dev';
exportconstconfig={matcher: '/'};
exportdefaultasyncfunctionmiddleware(request: Request): Promise<Response|undefined>{
try{
// preserve the original query string; forward only headers that may
// affect the HTML Framer serves (never cookie/host)
const{ search }=newURL(request.url);
constrequestHeaders=newHeaders({accept: 'text/html'});
for(constnameof['user-agent','accept-language','referer','x-forwarded-for']){
constvalue=request.headers.get(name);
if(value){
requestHeaders.set(name,value);
}
}
constupstream=awaitfetch(`${FRAMER_ORIGIN}/${search}`,{headers: requestHeaders});
constcontentType=upstream.headers.get('content-type')??'';
if(!upstream.ok||!contentType.includes('text/html')){
returnundefined;
}
consthtml=awaitupstream.text();
constrewritten=html.split(FRAMER_ORIGIN).join(CANONICAL_ORIGIN);
constheaders=newHeaders(upstream.headers);
// the body was re-encoded, so the upstream encoding/length no longer apply
headers.delete('content-encoding');
headers.delete('content-length');
returnnewResponse(rewritten,{status: upstream.status, headers });
}catch{
returnundefined;
}
}