forked from ionic-team/ionic-docs
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathprocessRequest.ts
More file actions
81 lines (70 loc) · 2.27 KB
/
Copy pathprocessRequest.ts
File metadata and controls
81 lines (70 loc) · 2.27 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
// import redirects = require('./redirects.json');
import * as redirects from './redirects.json';
import * as url from 'url';
export default (req, res, next) => {
// redirect www
if (req.headers.host.slice(0, 4) === 'www.') {
const newHost = req.headers.host.slice(4);
return res.redirect(301, 'https://' + newHost + req.originalUrl);
}
// redirect entire old sections
const parts = url.parse(req.url);
if (parts.path.indexOf('/docs/v1/cli') === 0) {
return res.redirect(301, '/docs/cli/');
}
// handle redirects
const uri = req.url.split(/[\?|#]/g);
let requrl = uri[0];
if (requrl.endsWith('/')) {
requrl = requrl.slice(0, -1);
}
if (redirects[requrl]) {
if (uri[1]) {
return res.redirect(301, redirects[requrl] + '?' + uri[1]);
}
return res.redirect(301, redirects[requrl]);
}
// don't index non production URLs, but favor HTTPS
let protocol = 'https';
if (req.hostname.indexOf('ionicframework.com') === -1) {
// res.setHeader('X-Robots-Tag', 'noindex, nofollow');
protocol = 'http';
} else {
// require https in prod
// let csp = 'default-src https: data: blob: \'unsafe-eval\' \'unsafe-inline\'; ';
// csp += 'frame-src \'self\' data: https://unpkg.com';
// res.setHeader('Content-Security-Policy', csp);
// res.setHeader('X-Content-Security-Policy', csp);
// res.setHeader('X-WebKit-CSP', csp);
}
// cache static files
const staticURLS = ['/assets/', '/favicon.ico'];
for (let i = 0; i <= staticURLS.length; i++) {
if (req.url.indexOf(staticURLS[i]) === 0) {
res.setHeader('Cache-Control', 'public, max-age=315360000000');
res.setHeader('Expires', new Date(Date.now() + 315360000000).toUTCString());
break;
}
}
// Setting default Page Vars
res.locals = {
protocol,
domain: req.get('host'),
url: req.originalUrl,
dev: req.get('host').indexOf('localhost') === 0,
};
return next();
};
function shuffle(array) {
let currentIndex = array.length;
let temporaryValue;
let randomIndex;
while (0 !== currentIndex) {
randomIndex = Math.floor(Math.random() * currentIndex);
currentIndex -= 1;
temporaryValue = array[currentIndex];
array[currentIndex] = array[randomIndex];
array[randomIndex] = temporaryValue;
}
return array;
}