angular 2 universal cache rendered pages on server
Two questions regarding angular universal:
How do i get angular to cache the rendered pages on the server side, as it is taking about 2s just for initial response on the client side, because angular is generating the page on server side each time.
On frontend it seems like angular is breaking the site, self.context.(funcname) does not exist for example. These issues which I know are related to the scope in which the function is being called, does work if I do not use universal (incl preboot).
Any ideas.
Just to put an answer to this:
I simple made a custom solution in server.ts:
const cache = {};
function ngApp(req, res) {
let baseUrl = '/';
let url = req.originalUrl || '/';
res.setHeader('Cache-Control', 'public, max-age=300');
if (cache.hasOwnProperty(url)) {
var hit = cache[url];
if (hit[0] > Date.now()) {
res.status(200).send(hit[1]);
return;
}
}
res.render('index', {
req,
res,
ngModule: MainModule,
preboot: {
appRoot: ['app'],
uglify: false,
buffer: true
},
async: false,
baseUrl: baseUrl,
requestUrl: req.originalUrl,
originUrl: 'http://localhost:3000'
},(err, html) => {
cache[url] = [Date.now()+180000,html];
res.status(200).send(html);
});
}
app.get('*', ngApp);
app.get('/', ngApp);
So basically I check an in-memory variable if I have an existing cache entry for the requested url, if not, or if it has expired, then I rerender the page and update/create a cache entry.
This has improved the delivering of follow up requests a lot. Locally it is down from around 1sec to only 10ms.
链接地址: http://www.djcxy.com/p/31378.html上一篇: 没有弹出的侧面渲染
下一篇: 服务器上的角度2通用高速缓存呈现页面