forked from angular/angular.js
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathindex.js
49 lines (41 loc) · 1.18 KB
/
index.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
'use strict';
const functions = require('firebase-functions');
const fs = require('fs');
const BROWSER_CACHE_DURATION = 60 * 60;
const CDN_CACHE_DURATION = 60 * 60 * 12;
const headers = {
'Cache-Control': `public max-age=${BROWSER_CACHE_DURATION} s-maxage=${CDN_CACHE_DURATION}`
};
const buildSnapshot = data => `<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8" />
<meta name="viewport" content="width=device-width, initial-scale=1">
<base href="/">
</head>
<body>
${data}
</body>
</html>`;
function sendFile(request, response) {
const snapshotRequested = typeof request.query._escaped_fragment_ !== 'undefined';
const filePath = `content/${snapshotRequested ? `partials${request.path}` : 'index'}.html`;
if (snapshotRequested) {
fs.readFile(filePath, {encoding: 'utf8'}, (error, data) => {
if (error) {
response
.status(404)
.end();
} else {
response
.set(headers)
.send(buildSnapshot(data));
}
});
} else {
response
.set(headers)
.sendFile(filePath, {root: __dirname});
}
}
exports.sendFile = functions.https.onRequest(sendFile);