forked from tldraw/make-real
-
Notifications
You must be signed in to change notification settings - Fork 0
/
middleware.ts
32 lines (26 loc) · 879 Bytes
/
middleware.ts
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
import { NextRequest, NextResponse } from 'next/server'
import { LINK_HOST } from './app/lib/hosts'
export const config = {
matcher: [
/*
* Match all request paths except for the ones starting with:
* - _vercel (Vercel internals)
* - _next (next internals)
* - some-file.extension (static files)
*/
'/((?!_vercel|_next/|[\\w-]+\\.\\w+).*)',
],
}
export function middleware(req: NextRequest) {
const url = req.nextUrl
const host = req.headers.get('host').toLowerCase()
const rewrittenUrl = new URL(url.toString())
if (host === LINK_HOST) {
// rewrite requests on the link host to the link site:
rewrittenUrl.pathname = `/makereal.tldraw.link${rewrittenUrl.pathname}`
} else {
// rewrite everything else to the main site:
rewrittenUrl.pathname = `/makereal.tldraw.com${rewrittenUrl.pathname}`
}
return NextResponse.rewrite(rewrittenUrl)
}