-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmiddleware.ts
More file actions
186 lines (162 loc) · 5.11 KB
/
middleware.ts
File metadata and controls
186 lines (162 loc) · 5.11 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
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
import { NextRequest, NextResponse } from 'next/server';
import { withAuth } from 'next-auth/middleware';
import createIntlMiddleware from 'next-intl/middleware';
import locales from './config/locales';
const publicPages = [
'/',
'/datasets',
'/datasets/(.*)',
'/login',
'/chart',
'/sectors',
'/sectors/(.*)',
'/usecases',
'/usecases/(.*)',
'/collaboratives',
'/collaboratives/(.*)',
'/about-us',
'/publishers',
'/publishers/(.*)',
'/search',
'/search/(.*)',
'/aimodels',
'/aimodels/(.*)',
];
const baseDomain = process.env.NEXT_PUBLIC_PLATFORM_DOMAIN;
const intlMiddleware = createIntlMiddleware({
locales: locales.all,
localePrefix: 'as-needed',
defaultLocale: locales.default,
});
const authMiddleware = withAuth(
function onSuccess(req) {
return intlMiddleware(req);
},
{
pages: {
signIn: '/login',
},
}
);
const getHostname = (hostHeader: string | null) =>
(hostHeader || '').split(':')[0].toLowerCase();
const getPlatformUrl = () => {
const platformUrl = process.env.NEXT_PUBLIC_PLATFORM_URL;
if (!platformUrl) return null;
try {
return new URL(platformUrl);
} catch {
return null;
}
};
const getBaseHostname = () => {
if (baseDomain) {
return baseDomain
.replace(/^https?:\/\//, '')
.split('/')[0]
.split(':')[0]
.toLowerCase();
}
const platformUrl = getPlatformUrl();
return platformUrl?.hostname?.toLowerCase() || null;
};
const getCollaborativeSlugFromHostname = (hostname: string) => {
const normalizedHost = hostname.toLowerCase();
const baseHostname = getBaseHostname();
if (!baseHostname || normalizedHost === baseHostname) return null;
const reservedSubdomains = new Set([
'www',
'dev',
'staging',
'stage',
'test',
'qa',
'prod',
'production',
'app',
'api',
'admin',
'assets',
'static',
'cdn',
]);
// Local: <slug>.collab.localhost OR <slug>.collab.127.0.0.1
if (
baseHostname === 'collab.localhost' ||
baseHostname === 'collab.127.0.0.1'
) {
const localSuffix =
baseHostname === 'collab.localhost'
? '.collab.localhost'
: '.collab.127.0.0.1';
if (!normalizedHost.endsWith(localSuffix)) return null;
const slug = normalizedHost.slice(0, -localSuffix.length);
if (!slug || slug.includes('.')) return null;
if (reservedSubdomains.has(slug)) return null;
return slug;
}
const baseSuffix = `.${baseHostname}`;
if (!normalizedHost.endsWith(baseSuffix)) return null;
const slug = normalizedHost.slice(0, -baseSuffix.length);
if (!slug || slug.includes('.')) return null;
if (reservedSubdomains.has(slug)) return null;
return slug;
};
export default function middleware(req: NextRequest) {
const hostname = getHostname(req.headers.get('host'));
const localeRootRegex = RegExp(`^/(${locales.all.join('|')})/?$`, 'i');
const localeRootMatch = req.nextUrl.pathname.match(localeRootRegex);
const localePathRegex = RegExp(`^/(${locales.all.join('|')})(/.*)$`, 'i');
const localePathMatch = req.nextUrl.pathname.match(localePathRegex);
const baseHostname = getBaseHostname();
const isPlatformHost = Boolean(baseHostname) && hostname === baseHostname;
// Ensure collab.<domain>/ shows collaboratives listing (not main homepage)
if (
isPlatformHost &&
(req.nextUrl.pathname === '/' || Boolean(localeRootMatch))
) {
const rewriteUrl = req.nextUrl.clone();
const defaultLocalePrefix = `/${locales.default}`;
const localePrefix = localeRootMatch
? `/${localeRootMatch[1]}`
: defaultLocalePrefix;
rewriteUrl.pathname = `${localePrefix}/collaboratives`;
return NextResponse.rewrite(rewriteUrl);
}
const collaborativeSlug = getCollaborativeSlugFromHostname(hostname);
if (collaborativeSlug) {
const rewriteUrl = req.nextUrl.clone();
const defaultLocalePrefix = `/${locales.default}`;
// slug.domain/ -> /<locale>/collaboratives/<slug>
if (req.nextUrl.pathname === '/' || Boolean(localeRootMatch)) {
const localePrefix = localeRootMatch
? `/${localeRootMatch[1]}`
: defaultLocalePrefix;
rewriteUrl.pathname = `${localePrefix}/collaboratives/${collaborativeSlug}`;
return NextResponse.rewrite(rewriteUrl);
}
// slug.domain/<locale>/... -> /<locale>/collaboratives/<slug>/...
if (localePathMatch) {
rewriteUrl.pathname = `/${localePathMatch[1]}/collaboratives/${collaborativeSlug}${localePathMatch[2]}`;
return NextResponse.rewrite(rewriteUrl);
}
// slug.domain/<path> -> /<default-locale>/collaboratives/<slug>/<path>
rewriteUrl.pathname = `${defaultLocalePrefix}/collaboratives/${collaborativeSlug}${req.nextUrl.pathname}`;
return NextResponse.rewrite(rewriteUrl);
}
const publicPathnameRegex = RegExp(
`^(/(${locales.all.join('|')}))?(${publicPages
.flatMap((p) => (p === '/' ? ['', '/'] : p))
.join('|')})/?$`,
'i'
);
const isPublicPage = publicPathnameRegex.test(req.nextUrl.pathname);
if (isPublicPage) {
return intlMiddleware(req);
} else {
return (authMiddleware as any)(req);
}
}
export const config = {
matcher: ['/((?!api|_next|.*\\..*).*)'],
};