This repository has been archived by the owner on Oct 3, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
middleware.js
63 lines (54 loc) · 2.2 KB
/
middleware.js
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
51
52
53
54
55
56
57
58
59
60
61
62
63
import { NextResponse } from 'next/server';
export function middleware(req) {
const url = req.nextUrl.clone(); // clone the request url
const { pathname, searchParams } = req.nextUrl; // get pathname of request (e.g. /blog-slug)
const hostname = req.headers.get('host'); // get hostname of request (e.g. demo.vercel.pub)
//console.log('[middleware] host header:', hostname);
if (pathname.startsWith('/_next/image')) {
return NextResponse.next();
}
if (pathname.includes('/en-US')) {
console.log('[middleware] pathname includes en-US, redirecting....');
url.pathname = pathname.replace('/en-US', '');
return NextResponse.redirect(url);
}
let currentHost;
if (!hostname) {
// revalidate requests from lambda come through (intermittently) without the 'host' header,
// despite us setting it explicitly on revalidate requests to the /api/revalidate endpoint
currentHost = searchParams.get('site');
} else {
currentHost = hostname
.replace(`.localhost:3000`, '')
.replace(`.tinynewsco.dev:3000`, '')
.replace(`.tinynewsco.org:3000`, '')
.replace(`.tinynewsco.dev`, '')
.replace(`.tinynewsco.org`, '')
.replace(`.vercel.app`, '')
.replace(`.vercel.app:3000`, ''); // TBD if we need to change this
// use the query param 'site' if currentHost isn't usable
if (
(currentHost === 'localhost:3000' ||
currentHost.includes('ngrok.io') ||
!currentHost) &&
searchParams
) {
currentHost = searchParams.get('site');
}
}
if (
(!pathname.includes('.') || pathname.includes('.xml')) && // exclude all files in the public folder
!pathname.startsWith('/api') // exclude all API routes
) {
// strip default locale from incoming request pathname
const pathWithoutLocale = pathname.replace('/en-US', '');
// console.log(
// `[middleware] host:pathname ${currentHost}:${pathWithoutLocale}`
// );
url.pathname = `/_sites/${currentHost}${pathWithoutLocale}`;
// console.log('[middleware] updated path:', url.pathname);
return NextResponse.rewrite(url);
} else if (pathname.startsWith('/api/auth/callback/google')) {
console.log(`[middleware] google oauth callback`);
}
}