-
Notifications
You must be signed in to change notification settings - Fork 3
/
server.js
142 lines (131 loc) · 3.97 KB
/
server.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
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
const next = require("next");
const Koa = require("koa");
const router = require("koa-route");
const LRUCache = require("lru-cache");
const port = parseInt(process.env.PORT, 10) || 8878;
const dev = process.env.NODE_ENV !== "production";
const test = process.env.NODE_TEST === "test";
const app = next({ dev });
const handle = app.getRequestHandler();
// This is where we cache our rendered HTML pages
const ssrCache = new LRUCache({
max: 100,
maxAge: 1000 * 60 * 60 // 1hour
});
/*
* NB: make sure to modify this to take into account anything that should trigger
* an immediate page change (e.g a locale stored in req.session)
*/
function getCacheKey(ctx) {
return ctx.url;
}
function renderAndCache(ctx, pagePath, noCache, queryParams = null) {
if (dev || test) ssrCache.reset();
if (noCache === "noCache") {
return app
.renderToHTML(ctx.req, ctx.res, pagePath, queryParams)
.then(html => {
// Let's cache this page
console.info("no cache");
ctx.body = html;
})
.catch(err => {
console.info("ERRR", err);
return app.renderError(err, ctx.req, ctx.res, pagePath, queryParams);
});
}
const key = getCacheKey(ctx.req);
// If we have a page in the cache, let's serve it
if (ssrCache.has(key)) {
console.info(`CACHE HIT: ${key}`);
ctx.body = ssrCache.get(key);
return Promise.resolve();
}
// If not let's render the page into HTML
return app
.renderToHTML(ctx.req, ctx.res, pagePath, queryParams)
.then(html => {
// Let's cache this page
console.info(`CACHE MISS: ${key}`);
ssrCache.set(key, html);
ctx.body = html;
})
.catch(err => {
console.info("ERRR", err);
return app.renderError(err, ctx.req, ctx.res, pagePath, queryParams);
});
}
app.prepare().then(() => {
const server = new Koa();
server.use(router.get("/", ctx => renderAndCache(ctx, "/index")));
server.use(router.get("/loan", ctx => renderAndCache(ctx, "/1-loan/1-home")));
server.use(
router.get("/loan/speed", ctx =>
renderAndCache(ctx, "/1-loan/2-home-speed")
)
);
server.use(
router.get("/loan/apply", ctx =>
renderAndCache(ctx, "/1-loan/4-apply-loan")
)
);
server.use(
router.get("/loan/city", ctx =>
renderAndCache(ctx, "/1-loan/5-city-apply-loan")
)
);
server.use(
router.get("/loan/speed/:id", (ctx, id) =>
renderAndCache(ctx, "/1-loan/6-speed-detail", null, { id })
)
);
server.use(
router.get("/loan/:id", (ctx, id) =>
renderAndCache(ctx, "/1-loan/3-detail", null, { id })
)
);
server.use(router.get("/card", ctx => renderAndCache(ctx, "/2-card/1-home")));
server.use(router.get("/new", ctx => renderAndCache(ctx, "/3-new/1-home")));
server.use(
router.get("/new/:id", (ctx, id) =>
renderAndCache(ctx, "/3-new/2-detail", null, { id })
)
);
server.use(
router.get("/downloadApp", ctx => renderAndCache(ctx, "/downloadApp"))
);
server.use(router.get("/login", ctx => renderAndCache(ctx, "/4-me/1-login")));
server.use(router.get("/about", ctx => renderAndCache(ctx, "/about")));
server.use(router.get("/me", ctx => renderAndCache(ctx, "/4-me/2-home")));
server.use(
router.get("/me/other", ctx => renderAndCache(ctx, "/4-me/3-other-data"))
);
server.use(
router.get("/me/loan-apply", ctx =>
renderAndCache(ctx, "/4-me/4-loan-apply")
)
);
server.use(
router.get("/me/system-message", ctx =>
renderAndCache(ctx, "/4-me/5-system-message")
)
);
server.use(
router.get("/me/apply-message", ctx =>
renderAndCache(ctx, "/4-me/6-apply-message")
)
);
server.use(router.get("/city", ctx => renderAndCache(ctx, "/city")));
server.use(async ctx => {
await handle(ctx.req, ctx.res);
ctx.respond = false;
});
server.use(async (ctx, next) => {
ctx.res.statusCode = 200;
await next();
});
server.listen(port, err => {
if (err) throw err;
console.info(`> Ready on http://localhost:${port}`);
});
});