-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathserver-setup-dev.ts
34 lines (28 loc) · 1.08 KB
/
server-setup-dev.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
33
34
import { Express } from 'express';
import { NextServer } from 'next/dist/server/next';
const DEV_NAIS_DOMAIN = 'ansatt.dev.nav.no';
const APP_ORIGIN = process.env.APP_ORIGIN;
// Redirects requests for other domains to the ansatt.dev.nav.no
export const serverSetupDev = (expressApp: Express, nextApp: NextServer) => {
const nextRequestHandler = nextApp.getRequestHandler();
expressApp.all('*', (req, res, next) => {
res.setHeader('X-Robots-Tag', 'noindex');
next();
});
// These paths should never redirect, to ensure the site will load correctly
// when accessed from the Content Studio editor
expressApp.all(
['/draft/*', '/archive/*', '/editor/*', '/_next/*', '/gfx/*', '/api/*'],
(req, res) => {
return nextRequestHandler(req, res);
}
);
if (APP_ORIGIN.endsWith(DEV_NAIS_DOMAIN)) {
expressApp.all('*', (req, res, next) => {
if (!req.hostname.endsWith(DEV_NAIS_DOMAIN)) {
return res.redirect(302, `${APP_ORIGIN}${req.path}`);
}
next();
});
}
};