{"id":408109,"date":"2024-06-29T19:57:43","date_gmt":"2024-06-29T19:57:43","guid":{"rendered":"http:\/\/savepearlharbor.com\/?p=408109"},"modified":"-0001-11-30T00:00:00","modified_gmt":"-0001-11-29T21:00:00","slug":"","status":"publish","type":"post","link":"https:\/\/savepearlharbor.com\/?p=408109","title":{"rendered":"<span>\u041a\u0430\u043a \u0441\u043a\u043e\u043d\u0444\u0438\u0433\u0443\u0440\u0438\u0440\u043e\u0432\u0430\u0442\u044c NextJS \u0441\u0435\u0440\u0432\u0435\u0440 \u0441 \u043f\u043e\u043b\u043d\u043e\u0439 \u043f\u043e\u0434\u0434\u0435\u0440\u0436\u043a\u043e\u0439 \u043a\u044d\u0448\u0438\u0440\u043e\u0432\u0430\u043d\u0438\u044f \u0432 Redis<\/span>"},"content":{"rendered":"<div><!--[--><!--]--><\/div>\n<div id=\"post-content-body\">\n<div>\n<div class=\"article-formatted-body article-formatted-body article-formatted-body_version-2\">\n<div xmlns=\"http:\/\/www.w3.org\/1999\/xhtml\">\n<ol>\n<li>\n<p>Next.js \u2014 JS \u0444\u0440\u0435\u0439\u043c\u0432\u043e\u0440\u043a, \u0441\u043e\u0437\u0434\u0430\u043d\u043d\u044b\u0439 \u043f\u043e\u0432\u0435\u0440\u0445 React.js \u0434\u043b\u044f \u0441\u043e\u0437\u0434\u0430\u043d\u0438\u044f \u0432\u0435\u0431-\u043f\u0440\u0438\u043b\u043e\u0436\u0435\u043d\u0438\u044f \u0441 \u043f\u043e\u0434\u0434\u0435\u0440\u0436\u043a\u043e\u0439 \u0444\u0443\u043d\u043a\u0446\u0438\u043e\u043d\u0430\u043b\u0430 \u043e\u0442\u0440\u0438\u0441\u043e\u0432\u043a\u0438 \u043f\u0440\u0438\u043b\u043e\u0436\u0435\u043d\u0438\u044f \u043d\u0430 \u0441\u0442\u043e\u0440\u043e\u043d\u0435 \u0441\u0435\u0440\u0432\u0435\u0440\u0430\u00a0<\/p>\n<\/li>\n<li>\n<p>Redis (<strong>Re<\/strong>mote\u00a0<strong>Di<\/strong>ctionary\u00a0<strong>S<\/strong>erver)- \u044d\u0442\u043e \u0431\u044b\u0441\u0442\u0440\u043e\u0435 \u0445\u0440\u0430\u043d\u0438\u043b\u0438\u0449\u0435 \u0434\u0430\u043d\u043d\u044b\u0445 \u0442\u0438\u043f\u0430 \u00ab\u043a\u043b\u044e\u0447\u2011\u0437\u043d\u0430\u0447\u0435\u043d\u0438\u0435\u00bb \u0432 \u043f\u0430\u043c\u044f\u0442\u0438, \u0430\u043a\u0442\u0438\u0432\u043d\u043e \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u0443\u0435\u043c\u043e\u0435 \u0432 \u0440\u0430\u0437\u0440\u0430\u0431\u043e\u0442\u043a\u0435 \u0441 \u0446\u0435\u043b\u044c\u044e \u043f\u043e\u0432\u044b\u0448\u0435\u043d\u0438\u044f \u043f\u0440\u043e\u0438\u0437\u0432\u043e\u0434\u0438\u0442\u0435\u043b\u044c\u043d\u043e\u0441\u0442\u0438 \u0441\u0435\u0440\u0432\u0438\u0441\u043e\u0432<\/p>\n<\/li>\n<li>\n<p>\u041a\u044d\u0448 Redis \u043f\u043e\u0437\u0432\u043e\u043b\u044f\u0435\u0442 \u043c\u0430\u043a\u0441\u0438\u043c\u0430\u043b\u044c\u043d\u043e \u044d\u0444\u0444\u0435\u043a\u0442\u0438\u0432\u043d\u043e \u043f\u0440\u0438\u043c\u0435\u043d\u044f\u0442\u044c \u0433\u043e\u0440\u0438\u0437\u043e\u043d\u0442\u0430\u043b\u044c\u043d\u043e\u0435 \u043c\u0430\u0441\u0448\u0442\u0430\u0431\u0438\u0440\u043e\u0432\u0430\u043d\u0438\u0435 \u0432\u0430\u0448\u0435\u0433\u043e \u043f\u0440\u0438\u043b\u043e\u0436\u0435\u043d\u0438\u044f, \u043f\u043e\u0441\u043a\u043e\u043b\u044c\u043a\u0443 \u0437\u0430\u0441\u0442\u0430\u0432\u043b\u044f\u0435\u0442 \u0432\u0441\u0435 \u0438\u043d\u0441\u0442\u0430\u043d\u0441\u044b \u043f\u0440\u0438\u043b\u043e\u0436\u0435\u043d\u0438\u044f \u0441\u043c\u043e\u0442\u0440\u0435\u0442\u044c \u0432 \u0435\u0434\u0438\u043d\u044b\u0439 \u0438\u0441\u0442\u043e\u0447\u043d\u0438\u043a \u0434\u0430\u043d\u043d\u044b\u0445, \u0432\u043c\u0435\u0441\u0442\u043e \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u043d\u0438\u044f \u043b\u043e\u043a\u0430\u043b\u044c\u043d\u043e\u0433\u043e \u0441\u0442\u0435\u0439\u0442\u0430<\/p>\n<\/li>\n<li>\n<p>\u0412 \u0440\u0430\u043c\u043a\u0430\u0445 \u0434\u0430\u043d\u043d\u043e\u0433\u043e \u0433\u0430\u0439\u0434\u0430 \u043c\u044b \u0441\u043e\u0431\u0435\u0440\u0435\u043c \u0441\u043e\u0431\u0441\u0442\u0432\u0435\u043d\u043d\u044b\u0439 \u0432\u0430\u0440\u0438\u0430\u043d\u0442 \u0441\u0435\u0440\u0432\u0435\u0440\u0430 \u0434\u043b\u044f \u0444\u0440\u0435\u0439\u043c\u0432\u043e\u0440\u043a\u0430 <strong>NextJS<\/strong>, \u0434\u043e\u0431\u0430\u0432\u0438\u0432 \u043a\u044d\u0448\u0438\u0440\u043e\u0432\u0430\u043d\u0438\u0435 \u0441\u0433\u0435\u043d\u0435\u0440\u0438\u0440\u043e\u0432\u0430\u043d\u044b\u0445 \u0441\u0442\u0440\u0430\u043d\u0438\u0446 \u0438 json \u0444\u0430\u0439\u043b\u043e\u0432, \u0441\u043e\u0434\u0435\u0440\u0436\u0430\u0449\u0438\u0445 \u043f\u0440\u043e\u043f\u0441\u044b \u044d\u0442\u0438\u0445 \u0441\u0442\u0440\u0430\u043d\u0438\u0446 ( \u0434\u043b\u044f \u0443\u0441\u043a\u043e\u0440\u0435\u043d\u0438\u044f \u043d\u0430\u0432\u0438\u0433\u0430\u0446\u0438\u0438 \u0437\u0430 \u0441\u0447\u0435\u0442 \u043f\u0440\u0435\u0444\u0435\u0442\u0447\u0435\u0439)<\/p>\n<\/li>\n<\/ol>\n<p><strong>\u0428\u0430\u0433 1. \u0423\u0441\u0442\u0430\u043d\u043e\u0432\u043a\u0430 NextJS<\/strong><\/p>\n<p><code>yarn create next-app<\/code><\/p>\n<p><strong>\u0428\u0430\u0433 2. \u0423\u0441\u0442\u0430\u043d\u043e\u0432\u043a\u0430 \u043f\u0430\u043a\u0435\u0442\u043e\u0432:<\/strong><\/p>\n<p><code>yarn add express redis node-gzip<\/code><\/p>\n<p><strong>\u0428\u0430\u0433 3. \u0421\u043e\u0437\u0434\u0430\u0435\u043c \u0432\u00a0 \u043a\u043e\u0440\u043d\u0435 \u043f\u0440\u043e\u0435\u043a\u0442\u0430 \u0444\u0430\u0439\u043b server.js<\/strong><\/p>\n<pre><code class=\"javascript\">const express = require('express') const next = require('next') const { promisify } = require('util') const {gzip} = require('node-gzip');  const client = redis.createClient('redis:\/\/localhost:6379') client.get = promisify(client.get)  const app = next({ dev: false }) const handle = app.getRequestHandler()  app.prepare().then(() => {    const server = express()    server.get('*', (req, res) => handle(req, res))    server.listen(3000, (err) => {     if ( err ) throw err     console.log(`> Ready on http:\/\/localhost:3000`)   }) }) <\/code><\/pre>\n<ol>\n<li>\n<p>\u041f\u043e\u0434\u043a\u043b\u044e\u0447\u0430\u0435\u043c <strong>express<\/strong> \u0434\u043b\u044f \u0441\u043e\u0437\u0434\u0430\u043d\u0438\u044f \u0441\u0435\u0440\u0432\u0435\u0440\u0430<\/p>\n<\/li>\n<li>\n<p>\u041f\u043e\u0434\u043a\u043b\u044e\u0447\u0430\u0435\u043c <strong>next<\/strong> \u0434\u043b\u044f \u0441\u043e\u0437\u0434\u0430\u043d\u0438\u044f \u0431\u0430\u0437\u043e\u0432\u043e\u0433\u043e \u043e\u0431\u0440\u0430\u0431\u043e\u0442\u0447\u0438\u043a\u0430 \u0437\u0430\u043f\u0440\u043e\u0441\u043e\u0432 NextJS\u00a0<\/p>\n<\/li>\n<li>\n<p>\u041f\u043e\u0434\u043a\u043b\u044e\u0447\u0430\u0435\u043c \u043f\u0430\u043a\u0435\u0442 \u0434\u043b\u044f \u0441\u0436\u0430\u0442\u0438\u044f\u00a0<\/p>\n<\/li>\n<li>\n<p>\u0423\u0441\u0442\u0430\u043d\u0430\u0432\u043b\u0438\u0432\u0430\u0435\u043c \u0441\u043e\u0435\u0434\u0438\u043d\u0435\u043d\u0438\u0435 \u0441 redis, \u0438 \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u0443\u0435\u043c promisify, \u0447\u0442\u043e\u0431\u044b \u0440\u0430\u0431\u043e\u0442\u0430\u0442\u044c \u0441 \u043f\u0440\u043e\u043c\u0438\u0441\u0430\u043c\u0438 \u0432\u043c\u0435\u0441\u0442\u043e \u043a\u043e\u043b\u0431\u044d\u043a\u043e\u0432.<\/p>\n<\/li>\n<li>\n<p>\u0417\u0430\u043f\u0443\u0441\u043a\u0430\u0435\u043c \u0441\u0435\u0440\u0432\u0435\u0440 \u0438 \u0443\u0441\u0442\u0430\u043d\u0430\u0432\u043b\u0438\u0432\u0430\u0435\u043c \u0431\u0430\u0437\u043e\u0432\u044b\u0439 \u043e\u0431\u0440\u0430\u0431\u043e\u0442\u0447\u0438\u043a\u00a0 \u0434\u043b\u044f \u0432\u0441\u0435\u0445 \u0437\u0430\u043f\u0440\u043e\u0441\u043e\u0432 <strong>( * )<\/strong> \u0447\u0435\u0440\u0435\u0437 <strong>handle<\/strong><\/p>\n<\/li>\n<\/ol>\n<p><strong>\u0428\u0430\u0433 4. \u0414\u043e\u0431\u0430\u0432\u043b\u044f\u0435\u043c \u043a\u043e\u043c\u0430\u043d\u0434\u0443\u00a0 \u0437\u0430\u043f\u0443\u0441\u043a\u0430 \u0441\u0435\u0440\u0432\u0435\u0440\u0430 \u0432 package.json<\/strong><\/p>\n<pre><code class=\"json\">  \"scripts\": {    ...     \"start\": \"node server.js\",    ...   },<\/code><\/pre>\n<p><strong>\u0428\u0430\u0433 5. \u0424\u043e\u0440\u043c\u0438\u0440\u043e\u0432\u0430\u043d\u0438\u0435 \u043a\u043b\u044e\u0447\u0435\u0439<\/strong><\/p>\n<p>\u0414\u043b\u044f \u0442\u043e\u0433\u043e, \u0447\u0442\u043e\u0431\u044b \u0432 \u043f\u043e\u043b\u043d\u043e\u0439 \u043c\u0435\u0440\u0435 \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u044c \u043c\u043e\u0449\u044c Redis \u0432 NextJS, \u043d\u0430\u043c \u043d\u0435\u043e\u0431\u0445\u043e\u0434\u0438\u043c\u043e \u043a\u044d\u0448\u0438\u0440\u043e\u0432\u0430\u0442\u044c 2 \u0432\u0438\u0434\u0430 \u0444\u0430\u0439\u043b\u043e\u0432:\u00a0<\/p>\n<ol>\n<li>\n<p>HTML, \u043a\u043e\u0442\u043e\u0440\u044b\u0439 \u043e\u0442\u0434\u0430\u0435\u0442\u0441\u044f \u0432 \u0431\u0440\u0430\u0443\u0437\u0435\u0440 \u043f\u0440\u0438 \u0441\u0435\u0440\u0432\u0435\u0440\u043d\u043e\u0439 \u0433\u0435\u043d\u0435\u0440\u0430\u0446\u0438\u0438 \u0441\u0442\u0440\u0430\u043d\u0438\u0446\u044b<\/p>\n<\/li>\n<li>\n<p>JSON-\u0444\u0430\u0439\u043b \u0434\u043b\u044f \u043a\u0430\u0436\u0434\u043e\u0439 \u0441\u0442\u0440\u0430\u043d\u0438\u0446\u044b, \u043a\u043e\u0442\u043e\u0440\u044b\u0439 \u0433\u0435\u043d\u0435\u0440\u0438\u0440\u0443\u0435\u0442 NextJS \u0434\u043b\u044f \u043a\u043b\u0438\u0435\u043d\u0442\u0441\u043a\u043e\u0439 \u043d\u0430\u0432\u0438\u0433\u0430\u0446\u0438\u0438 ( \u043a\u043e\u0433\u0434\u0430 \u043c\u044b \u043f\u0435\u0440\u0435\u0445\u043e\u0434\u0438\u043c \u043d\u0430 \u0441\u0442\u0440\u0430\u043d\u0438\u0446\u0443 NextJS \u0437\u0430\u043f\u0440\u0430\u0448\u0438\u0432\u0430\u0435\u0442 \u044d\u0442\u043e\u0442 JSON \u0444\u0430\u0439\u043b \u0438\u00a0 \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u0443\u0435\u0442 \u0434\u043b\u044f \u043f\u0435\u0440\u0435\u0434\u0430\u0447\u0438 \u043f\u0440\u043e\u043f\u0441\u043e\u0432 \u0441\u0442\u0440\u0430\u043d\u0438\u0446\u044b<\/p>\n<\/li>\n<\/ol>\n<p><strong>\u0428\u0430\u0433 6. \u0421\u043e\u0437\u0434\u0430\u0435\u043c \u043a\u043b\u044e\u0447\u00a0 \u0434\u043b\u044f SSR<\/strong><\/p>\n<pre><code class=\"javascript\">const getSsrKey = (req) => { \u00a0 return req.url }<\/code><\/pre>\n<p>\u0414\u043b\u044f \u0434\u0430\u043d\u043d\u043e\u0439 \u0437\u0430\u0434\u0430\u0447\u0438 \u0441\u0430\u043c\u044b\u043c \u043f\u0440\u043e\u0441\u0442\u044b\u043c \u0440\u0435\u0448\u0435\u043d\u0438\u0435\u043c \u0434\u043b\u044f \u0444\u043e\u0440\u043c\u0438\u0440\u043e\u0432\u0430\u043d\u0438\u044f \u043a\u043b\u044e\u0447\u0430 \u0431\u0443\u0434\u0435\u0442 \u0432\u0437\u044f\u0442\u044c url \u0441\u0442\u0440\u0430\u043d\u0438\u0446\u044b<\/p>\n<p><strong>\u0428\u0430\u0433 7. \u0421\u043e\u0437\u0434\u0430\u0435\u043c \u043a\u043b\u044e\u0447 \u0434\u043b\u044f JSON<\/strong><\/p>\n<pre><code class=\"javascript\">const getJsonKey = (req) => \u00a0 req.path \u00a0 \u00a0 .match(\/\\\/([^\\\/]+)+\/g) \u00a0 \u00a0 .slice(3) \u00a0 \u00a0 .join('')<\/code><\/pre>\n<p>\u041f\u0443\u0442\u044c \u043a JSON \u0444\u0430\u0439\u043b\u0430\u043c \u0432 \u0440\u0430\u043c\u043a\u0430\u0445 \u0444\u0440\u0435\u0439\u043c\u0432\u043e\u0440\u043a\u0430 NextJS \u0438\u043c\u0435\u0435\u0442 \u0432\u0438\u0434 <em>\/_next\/data\/&lt;BUILD_ID>\/your-page-name.json<\/em><\/p>\n<p>\u041f\u043e\u044d\u0442\u043e\u043c\u0443 \u0441\u0430\u043c\u043e\u0439 \u043f\u043e\u043b\u0435\u0437\u043d\u043e\u0439 \u0447\u0430\u0441\u0442\u044c\u044e \u044d\u0442\u043e\u0433\u043e \u043f\u0443\u0442\u0438 \u044f\u0432\u043b\u044f\u0435\u0442\u0441\u044f \u0442\u043e, \u0447\u0442\u043e \u0438\u0434\u0435\u0442 \u043f\u043e\u0441\u043b\u0435 \u0438\u0434\u0435\u043d\u0442\u0438\u0444\u0438\u043a\u0430\u0442\u043e\u0440\u0430 \u0441\u0431\u043e\u0440\u043a\u0438 &#8212; <strong>your-page-name.json<\/strong>, \u043f\u043e\u044d\u0442\u043e\u043c\u0443 \u0435\u0433\u043e \u043c\u044b \u0438 \u0431\u0443\u0434\u0435\u043c \u0431\u0440\u0430\u0442\u044c \u0434\u043b\u044f \u0444\u043e\u0440\u043c\u0438\u0440\u043e\u0432\u0430\u043d\u0438\u044f \u043a\u043b\u044e\u0447\u0430<\/p>\n<p><strong>\u0428\u0430\u0433 8. \u0421\u043e\u0437\u0434\u0430\u0435\u043c \u043a\u044d\u0448 HTML<\/strong><\/p>\n<pre><code class=\"javascript\">async function ssrCache(req, res) {  \u00a0 const key = getSsrKey(req)\u00a0    const cache = await client.get(key)  \u00a0 if ( cache ) { \u00a0 \u00a0 return res.send(cache) \u00a0 }  \u00a0 const data = await app.renderToHTML(req, res, req.path, { ...req.query, ...req.params })  \u00a0 if ( res.statusCode === 200 &amp;&amp; data ) client.set(key, data)  \u00a0 return res.send(data) }<\/code><\/pre>\n<p>\u0414\u043b\u044f \u0440\u0435\u043d\u0434\u0435\u0440\u0438\u043d\u0433\u0430 \u0441\u0442\u0440\u0430\u043d\u0438\u0446 nextJS \u043f\u0440\u0435\u0434\u043e\u0441\u0442\u0430\u0432\u043b\u044f\u0435\u0442 \u043c\u0435\u0442\u043e\u0434 <strong>renderToHTML<\/strong>. \u041f\u043e\u044d\u0442\u043e\u043c\u0443 \u043d\u0430\u0448\u0430 \u0437\u0430\u0434\u0430\u0447\u0430 \u0441\u043e\u0441\u0442\u043e\u0438\u0442 \u0432 \u0442\u043e\u043c, \u0447\u0442\u043e\u0431\u044b:\u00a0<\/p>\n<ol>\n<li>\n<p>\u041f\u043e\u043f\u044b\u0442\u0430\u0442\u044c\u0441\u044f \u043f\u043e\u043b\u0443\u0447\u0438\u0442\u044c \u0437\u043d\u0430\u0447\u0435\u043d\u0438\u0435 \u0438\u0437 \u043a\u044d\u0448\u0430 Redis \u043f\u043e \u043a\u043b\u044e\u0447\u0443.\u00a0<\/p>\n<\/li>\n<li>\n<p>\u0415\u0441\u043b\u0438 \u043a\u044d\u0448 \u0441\u0443\u0449\u0435\u0441\u0442\u0432\u0443\u0435\u0442, \u0442\u043e \u043c\u0433\u043d\u043e\u0432\u0435\u043d\u043d\u043e \u043e\u0442\u0434\u0430\u0442\u044c \u0432 \u0431\u0440\u0430\u0443\u0437\u0435\u0440<\/p>\n<\/li>\n<li>\n<p>\u0415\u0441\u043b\u0438 \u0436\u0435 \u043a\u044d\u0448\u0430 \u043d\u0435\u0442, \u0442\u043e \u043e\u0442\u0440\u0435\u043d\u0434\u0435\u0440\u0438\u0442\u044c \u0441\u0442\u0440\u0430\u043d\u0438\u0446\u0443 \u0441 \u043f\u043e\u043c\u043e\u0449\u044c\u044e \u043c\u0435\u0442\u043e\u0434\u0430 <strong>renderToHTML<\/strong><\/p>\n<\/li>\n<li>\n<p>\u0415\u0441\u043b\u0438 \u0441\u0442\u0440\u0430\u043d\u0438\u0446\u0430 \u0431\u044b\u043b\u0430 \u043e\u0442\u0440\u0435\u043d\u0434\u0435\u0440\u0435\u043d\u0430 \u0443\u0441\u043f\u0435\u0448\u043d\u043e, \u0437\u0430\u043a\u044d\u0448\u0438\u0440\u043e\u0432\u0430\u0442\u044c \u043f\u043e\u043b\u0443\u0447\u0435\u043d\u043d\u044b\u0439 HTML, \u043f\u043e\u0441\u043b\u0435 \u0447\u0435\u0433\u043e \u043e\u0442\u0434\u0430\u0442\u044c \u0432 \u0431\u0440\u0430\u0443\u0437\u0435\u0440<\/p>\n<\/li>\n<\/ol>\n<p><strong>\u0428\u0430\u0433 9. \u0421\u043e\u0437\u0434\u0430\u0435\u043c \u043a\u044d\u0448 JSON<\/strong><\/p>\n<pre><code class=\"javascript\">async function jsonCache(req, res) {  \u00a0 const key = getJsonKey(req)  \u00a0 const cache = await client.get(key)  \u00a0 if ( cache ) {  \u00a0 \u00a0 const headersToWrite = { \u00a0 \u00a0 \u00a0 'content-type': 'application\/json', \u00a0 \u00a0 \u00a0 'content-encoding': 'gzip' \u00a0 \u00a0 }  \u00a0 \u00a0 const buffer = JSON.parse(cache, (k, v) => {  \u00a0 \u00a0 \u00a0 if ( v !== null &amp;&amp; typeof v === 'object' &amp;&amp; 'type' in v &amp;&amp;        v.type === 'Buffer' &amp;&amp;  'data' in v &amp;&amp; Array.isArray(v.data) ) { \u00a0 \u00a0 \u00a0 \u00a0 return Buffer.from(v.data) \u00a0 \u00a0 \u00a0 }  \u00a0 \u00a0 \u00a0 return v \u00a0 \u00a0 })  \u00a0 \u00a0 Object.entries(headersToWrite).forEach(([ key, value ]) => res.setHeader(key, value))  \u00a0 \u00a0 return res.send(buffer)  \u00a0 }  \u00a0 const rawResEnd = res.end \u00a0 const rawResWrite = res.write \u00a0 const chunks = []  \u00a0 const proxyWrite = new Proxy(res.write, {  \u00a0 \u00a0 apply(target, thisArg, args) { \u00a0 \u00a0 \u00a0 const chunk = Buffer.from(args[ 0 ]) \u00a0 \u00a0 \u00a0 chunks.push(chunk) \u00a0 \u00a0 }  \u00a0 })  \u00a0 res.write = proxyWrite  \u00a0 const data = await new Promise(async (resolve) => {  \u00a0 \u00a0 res.end = async (res) => { \u00a0 \u00a0 \u00a0 resolve(res || chunks) \u00a0 \u00a0 }  \u00a0 \u00a0 await app.render(req, res, req.path, { \u00a0 \u00a0 \u00a0 ...req.query, \u00a0 \u00a0 \u00a0 ...req.params \u00a0 \u00a0 })  \u00a0 })  \u00a0 res.write = rawResWrite \u00a0 res.end = rawResEnd  \u00a0 const isChunked = Array.isArray(data) \u00a0 const response = isChunked ?\u00a0 Buffer.concat(data) : (Buffer.from(data)) \u00a0 const serializedResponse = isChunked ? JSON.stringify(response) : (JSON.stringify(await gzip(response)))  \u00a0 if ( res.statusCode === 200 &amp;&amp; data ) client.set(key, serializedResponse)  \u00a0 return res.end(response) }<\/code><\/pre>\n<p>\u0421 JSON \u043a\u044d\u0448\u0435\u043c \u0441\u0438\u0442\u0443\u0430\u0446\u0438\u044f \u043d\u0435\u043c\u043d\u043e\u0433\u043e \u0441\u043b\u043e\u0436\u043d\u0435\u0435. \u0414\u043b\u044f \u0440\u0435\u043d\u0434\u0435\u0440\u0430 JSON \u043c\u044b \u043c\u043e\u0436\u0435\u043c \u0432\u043e\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u044c\u0441\u044f \u043c\u0435\u0442\u043e\u0434\u043e\u043c\u00a0 <strong>render<\/strong>. \u041f\u0440\u043e\u0431\u043b\u0435\u043c\u0430 \u0437\u0430\u043a\u043b\u044e\u0447\u0430\u0435\u0442\u0441\u044f \u0432 \u0442\u043e\u043c, \u0447\u0442\u043e \u044d\u0442\u043e\u0442 \u043c\u0435\u0442\u043e\u0434 \u043d\u0435 \u0432\u043e\u0437\u0432\u0440\u0430\u0449\u0430\u0435\u0442 \u043d\u0438\u043a\u0430\u043a\u043e\u0433\u043e \u0437\u043d\u0430\u0447\u0435\u043d\u0438\u044f, \u0430 \u0441\u0440\u0430\u0437\u0443 \u043e\u0442\u0434\u0430\u0435\u0442 \u0435\u0433\u043e \u0432 \u0431\u0440\u0430\u0443\u0437\u0435\u0440. \u0422\u0430\u043a\u0438\u043c \u043e\u0431\u0440\u0430\u0437\u043e\u043c, \u0434\u043b\u044f \u0440\u0435\u0448\u0435\u043d\u0438\u044f \u0434\u0430\u043d\u043d\u043e\u0439 \u0437\u0430\u0434\u0430\u0447\u0438 \u043d\u0430\u043c \u043d\u0435\u043e\u0431\u0445\u043e\u0434\u0438\u043c\u043e \u043f\u043e\u0434\u043c\u0435\u043d\u0438\u0442\u044c \u0444\u0443\u043d\u043a\u0446\u0438\u043e\u043d\u0430\u043b\u044c\u043d\u043e\u0441\u0442\u044c \u0441\u043b\u0435\u0434\u0443\u044e\u0449\u0438\u0445 \u043c\u0435\u0442\u043e\u0434\u043e\u0432, \u043a\u043e\u0442\u043e\u0440\u044b\u0435 \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u0443\u044e\u0442\u0441\u044f \u0434\u043b\u044f \u0432\u044b\u0434\u0430\u0447\u0438 \u0432 \u0431\u0440\u0430\u0443\u0437\u0435\u0440:<\/p>\n<ol>\n<li>\n<p><strong>res.write <\/strong>&#8212; \u0432\u043c\u0435\u0441\u0442\u043e \u0442\u043e\u0433\u043e, \u0447\u0442\u043e\u0431\u044b \u0432\u044b\u0432\u043e\u0434\u0438\u0442\u044c \u0434\u0430\u043d\u043d\u044b\u0435 \u043c\u044b \u0441\u0434\u0435\u043b\u0430\u0435\u043c \u0442\u0430\u043a, \u0447\u0442\u043e\u0431\u044b \u044d\u0442\u0438 \u0434\u0430\u043d\u043d\u044b\u0435 \u043f\u043e\u043f\u0430\u0434\u0430\u043b\u0438 \u0432 \u043c\u0430\u0441\u0441\u0438\u0432 chunks.<\/p>\n<\/li>\n<li>\n<p><strong>res.end &#8212; <\/strong>\u0432\u043c\u0435\u0441\u0442\u043e \u0442\u043e\u0433\u043e, \u0447\u0442\u043e\u0431\u044b \u0437\u0430\u0432\u0435\u0440\u0448\u0430\u0442\u044c \u043f\u0440\u043e\u0446\u0435\u0441\u0441 \u043e\u0442\u0432\u0435\u0442\u0430 \u0431\u0443\u0434\u0435\u0442 \u0441\u0438\u0433\u043d\u0430\u043b\u0438\u0437\u0438\u0440\u043e\u0432\u0430\u0442\u044c \u043d\u0430\u043c \u043e \u0442\u043e\u043c, \u0447\u0442\u043e \u0432 <strong>chunks<\/strong> \u0443\u0436\u0435 \u043b\u0435\u0436\u0430\u0442 \u0432\u0441\u0435 \u043d\u0435\u043e\u0431\u0445\u043e\u0434\u0438\u043c\u044b\u0435 \u0434\u043b\u044f \u0432\u044b\u0432\u043e\u0434\u0430 \u0434\u0430\u043d\u043d\u044b\u0435, \u0438 \u043c\u044b \u043c\u043e\u0436\u0435\u043c \u043d\u0430\u0447\u0430\u0442\u044c \u0441 \u043d\u0438\u043c \u0440\u0430\u0431\u043e\u0442\u0430\u0442\u044c<\/p>\n<\/li>\n<li>\n<p>\u0422\u0435\u043f\u0435\u0440\u044c \u043c\u044b \u0434\u0435\u043b\u0430\u0435\u043c \u0442\u0443 \u0436\u0435 \u0441\u0430\u043c\u0443\u044e \u043f\u0440\u043e\u0432\u0435\u0440\u043a\u0443 \u043d\u0430 \u043d\u0430\u043b\u0438\u0447\u0438\u0435 \u043a\u044d\u0448\u0430, \u0438 \u0435\u0441\u043b\u0438 \u043e\u043d \u0441\u0443\u0449\u0435\u0441\u0442\u0432\u0443\u0435\u0442, \u0442\u043e \u043f\u0430\u0440\u0441\u0438\u043c JSON \u0432\u00a0 <strong>Buffer<\/strong>, \u0434\u043e\u0431\u0430\u0432\u043b\u044f\u0435\u043c \u0437\u0430\u0433\u043e\u043b\u043e\u0432\u043a\u0438 \u043e \u0442\u0438\u043f\u0435 \u0438 \u043a\u043e\u0434\u0438\u0440\u043e\u0432\u043a\u0435 \u0434\u0430\u043d\u043d\u044b\u0445 \u0438 \u043e\u0442\u0434\u0430\u0435\u043c \u0432 \u0431\u0440\u0430\u0443\u0437\u0435\u0440<\/p>\n<\/li>\n<li>\n<p>\u0415\u0441\u043b\u0438 \u043a\u044d\u0448\u0430 \u043d\u0435\u0442, \u0442\u043e \u0437\u0430\u043f\u043e\u043c\u0438\u043d\u0430\u0435\u043c \u0431\u0430\u0437\u043e\u0432\u044b\u0435 \u0444\u0443\u043d\u043a\u0446\u0438\u0438 <strong>res.write<\/strong> \u0438 <strong>res.end<\/strong> \u0432 \u043f\u0435\u0440\u0435\u043c\u0435\u043d\u043d\u0443\u044e, \u0447\u0442\u043e\u0431\u044b \u043f\u043e\u0442\u043e\u043c \u0438\u043c\u0435\u0442\u044c \u0432\u043e\u0437\u043c\u043e\u0436\u043d\u043e\u0441\u0442\u044c \u0438\u0445 \u0432\u043e\u0441\u0441\u0442\u0430\u043d\u043e\u0432\u0438\u0442\u044c<\/p>\n<\/li>\n<li>\n<p>\u041f\u0435\u0440\u0435\u043e\u043f\u0440\u0435\u0434\u0435\u043b\u044f\u0435\u043c <strong>write<\/strong> \u0438 <strong>end<\/strong> \u043f\u0440\u043e\u043a\u0441\u0438 \u0444\u0443\u043d\u043a\u0446\u0438\u044f\u043c\u0438, \u043a\u043e\u0442\u043e\u0440\u044b\u0435 \u043e\u0431\u0441\u0443\u0434\u0438\u043b\u0438 \u0432 \u043f\u0443\u043d\u043a\u0442\u0430\u0445 1 \u0438 2<\/p>\n<\/li>\n<li>\n<p>\u0420\u0435\u043d\u0434\u0435\u0440\u0438\u043c \u043a\u043e\u043d\u0442\u0435\u043d\u0442 \u0441 \u043f\u043e\u043c\u043e\u0449\u044c\u044e <strong>render<\/strong>, \u043d\u043e \u0431\u043b\u0430\u0433\u043e\u0434\u0430\u0440\u044f \u043d\u0430\u0448\u0438\u043c \u043f\u0440\u043e\u043a\u0441\u0438 \u0444\u0443\u043d\u043a\u0446\u0438\u044f\u043c \u043e\u043d \u043d\u0435 \u0431\u0443\u0434\u0435\u0442 \u043e\u0442\u0434\u0430\u043d \u0431\u0440\u0430\u0443\u0437\u0435\u0440, \u0430 \u0443\u0434\u043e\u0431\u043d\u043e \u043e\u043a\u0430\u0436\u0435\u0442\u0441\u044f \u0432 \u0441\u043f\u0435\u0446\u0438\u0430\u043b\u044c\u043d\u043e \u0441\u043e\u0437\u0434\u0430\u043d\u043d\u043e\u0439 \u043d\u0430\u043c\u0438 \u043f\u0435\u0440\u0435\u043c\u0435\u043d\u043d\u043e\u0439\u00a0<\/p>\n<\/li>\n<li>\n<p>\u0412\u043e\u0441\u0441\u0442\u0430\u043d\u0430\u0432\u043b\u0438\u0432\u0430\u0435\u043c \u0431\u0430\u0437\u043e\u0432\u044b\u0435 \u0432\u0435\u0440\u0441\u0438\u0438 \u0444\u0443\u043d\u043a\u0446\u0438\u0439 <strong>write<\/strong> \u0438 <strong>end<\/strong><\/p>\n<\/li>\n<li>\n<p>\u0414\u0430\u043b\u0435\u0435 \u043c\u043e\u0436\u0435\u0442 \u0431\u044b\u0442\u044c \u0434\u0432\u0430 \u0432\u0430\u0440\u0438\u0430\u043d\u0442\u0430 \u0442\u043e\u0433\u043e, \u043a\u0430\u043a \u043c\u044b \u043c\u043e\u0436\u0435\u043c \u0441\u043e\u0431\u0440\u0430\u0442\u044c \u043e\u0442\u0432\u0435\u0442 &#8212; \u0435\u0441\u043b\u0438 \u043a\u043e\u043d\u0442\u0435\u043d\u0442 \u043d\u0435 \u0447\u0430\u043d\u043a\u0430\u043b\u0441\u044f, \u0442\u043e \u0442\u043e\u0433\u0434\u0430 \u043d\u0430 \u0432\u044b\u0445\u043e\u0434\u0435 \u043c\u044b \u043f\u043e\u043b\u0443\u0447\u0438\u043c \u0441\u0442\u0440\u043e\u043a\u0443(JSON) \u0438 \u0441\u043e\u0437\u0434\u0430\u0434\u0438\u043c \u0431\u0443\u0444\u0435\u0440 \u0438\u0437 \u043d\u0435\u0435, \u0432 \u0438\u043d\u043e\u043c \u0441\u043b\u0443\u0447\u0430\u0435 \u0441\u043e\u0435\u0434\u0438\u043d\u044f\u0435\u043c \u043a\u0443\u0441\u043e\u0447\u043a\u0438, \u043a\u043e\u0442\u043e\u0440\u044b\u0435 \u0441\u043e\u0431\u0440\u0430\u043b\u0438 \u0441 \u043f\u043e\u043c\u043e\u0449\u044c\u044e \u043f\u0440\u043e\u043a\u0441\u0438\u0440\u043e\u0432\u0430\u043d\u0438\u044f \u0444\u0443\u043d\u043a\u0446\u0438\u0438 write \u0432 \u0435\u0434\u0438\u043d\u044b\u0439 \u0431\u0443\u0444\u0435\u0440.\u00a0<\/p>\n<\/li>\n<li>\n<p>\u0415\u0441\u043b\u0438 \u043c\u044b \u043f\u043e\u043b\u0443\u0447\u0438\u043b\u0438 \u0443\u0441\u043f\u0435\u0448\u043d\u044b\u0439 \u043e\u0442\u0432\u0435\u0442, \u0442\u043e \u0441\u0435\u0440\u0438\u0430\u043b\u0438\u0437\u0443\u0435\u043c \u0431\u0443\u0444\u0435\u0440, \u0441\u0436\u0438\u043c\u0430\u0435\u043c (\u0432 \u0441\u043b\u0443\u0447\u0430\u0435 \u043d\u0435\u043e\u0431\u0445\u043e\u0434\u0438\u043c\u043e\u0441\u0442\u0438)\u00a0\u0438 \u0441\u043e\u0445\u0440\u0430\u043d\u044f\u0435\u043c \u0432 \u043a\u044d\u0448\u0435 \u0440\u0435\u0434\u0438\u0441\u0430<\/p>\n<\/li>\n<li>\n<p>\u0417\u0430\u0432\u0435\u0440\u0448\u0430\u0435\u043c \u043e\u0442\u0432\u0435\u0442 \u043c\u0435\u0442\u043e\u0434\u043e\u043c <strong>end<\/strong><\/p>\n<\/li>\n<\/ol>\n<p><strong>\u0428\u0430\u0433 11. \u0421\u043e\u0431\u0438\u0440\u0430\u0435\u043c \u0432\u0441\u0435 \u0432\u043c\u0435\u0441\u0442\u0435\u00a0<\/strong><\/p>\n<p>\u0414\u043b\u044f <strong>SSR<\/strong> \u0441\u0442\u0440\u0430\u043d\u0438\u0446 \u043c\u044b \u043f\u0440\u0438\u043c\u0435\u043d\u044f\u0435\u043c <strong>ssrCache<\/strong>, \u0434\u043b\u044f JSON \u0444\u0430\u0439\u043b\u043e\u0432 &#8212; <strong>jsonCache<\/strong><\/p>\n<pre><code class=\"javascript\">server.get('\/', (req, res) => { \u00a0 return ssrCache(req, res) })  server.get('\/_next\/data\/*', async (req, res) => { \u00a0 return jsonCache(req, res) })  server.get('*', (req, res) => handle(req, res))<\/code><\/pre>\n<p>\u0421\u0441\u044b\u043b\u043a\u0430 \u043d\u0430 \u0440\u0435\u043f\u043e\u0437\u0438\u0442\u043e\u0440\u0438\u0439: <a href=\"https:\/\/github.com\/IAlexanderI1994\/next-redis-article\" rel=\"noopener noreferrer nofollow\">https:\/\/github.com\/IAlexanderI1994\/next-redis-article<\/a><\/p>\n<p>\u0411\u043b\u0430\u0433\u043e\u0434\u0430\u0440\u044e \u0437\u0430 \u043f\u0440\u043e\u0447\u0442\u0435\u043d\u0438\u0435. \u0411\u0443\u0434\u0443 \u0440\u0430\u0434 \u0432\u0430\u0448\u0438\u043c \u0432\u043e\u043f\u0440\u043e\u0441\u0430\u043c \u0438 \u043a\u043e\u043c\u043c\u0435\u043d\u0442\u0430\u0440\u0438\u044f\u043c.<\/p>\n<\/div>\n<\/div>\n<\/div>\n<p><!----><!----><\/div>\n<p><!----><!----><br \/> \u0441\u0441\u044b\u043b\u043a\u0430 \u043d\u0430 \u043e\u0440\u0438\u0433\u0438\u043d\u0430\u043b \u0441\u0442\u0430\u0442\u044c\u0438 <a href=\"https:\/\/habr.com\/ru\/articles\/576874\/\"> https:\/\/habr.com\/ru\/articles\/576874\/<\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<div><!--[--><!--]--><\/div>\n<div id=\"post-content-body\">\n<div>\n<div class=\"article-formatted-body article-formatted-body article-formatted-body_version-2\">\n<div xmlns=\"http:\/\/www.w3.org\/1999\/xhtml\">\n<ol>\n<li>\n<p>Next.js \u2014 JS \u0444\u0440\u0435\u0439\u043c\u0432\u043e\u0440\u043a, \u0441\u043e\u0437\u0434\u0430\u043d\u043d\u044b\u0439 \u043f\u043e\u0432\u0435\u0440\u0445 React.js \u0434\u043b\u044f \u0441\u043e\u0437\u0434\u0430\u043d\u0438\u044f \u0432\u0435\u0431-\u043f\u0440\u0438\u043b\u043e\u0436\u0435\u043d\u0438\u044f \u0441 \u043f\u043e\u0434\u0434\u0435\u0440\u0436\u043a\u043e\u0439 \u0444\u0443\u043d\u043a\u0446\u0438\u043e\u043d\u0430\u043b\u0430 \u043e\u0442\u0440\u0438\u0441\u043e\u0432\u043a\u0438 \u043f\u0440\u0438\u043b\u043e\u0436\u0435\u043d\u0438\u044f \u043d\u0430 \u0441\u0442\u043e\u0440\u043e\u043d\u0435 \u0441\u0435\u0440\u0432\u0435\u0440\u0430\u00a0<\/p>\n<\/li>\n<li>\n<p>Redis (<strong>Re<\/strong>mote\u00a0<strong>Di<\/strong>ctionary\u00a0<strong>S<\/strong>erver)- \u044d\u0442\u043e \u0431\u044b\u0441\u0442\u0440\u043e\u0435 \u0445\u0440\u0430\u043d\u0438\u043b\u0438\u0449\u0435 \u0434\u0430\u043d\u043d\u044b\u0445 \u0442\u0438\u043f\u0430 \u00ab\u043a\u043b\u044e\u0447\u2011\u0437\u043d\u0430\u0447\u0435\u043d\u0438\u0435\u00bb \u0432 \u043f\u0430\u043c\u044f\u0442\u0438, \u0430\u043a\u0442\u0438\u0432\u043d\u043e \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u0443\u0435\u043c\u043e\u0435 \u0432 \u0440\u0430\u0437\u0440\u0430\u0431\u043e\u0442\u043a\u0435 \u0441 \u0446\u0435\u043b\u044c\u044e \u043f\u043e\u0432\u044b\u0448\u0435\u043d\u0438\u044f \u043f\u0440\u043e\u0438\u0437\u0432\u043e\u0434\u0438\u0442\u0435\u043b\u044c\u043d\u043e\u0441\u0442\u0438 \u0441\u0435\u0440\u0432\u0438\u0441\u043e\u0432<\/p>\n<\/li>\n<li>\n<p>\u041a\u044d\u0448 Redis \u043f\u043e\u0437\u0432\u043e\u043b\u044f\u0435\u0442 \u043c\u0430\u043a\u0441\u0438\u043c\u0430\u043b\u044c\u043d\u043e \u044d\u0444\u0444\u0435\u043a\u0442\u0438\u0432\u043d\u043e \u043f\u0440\u0438\u043c\u0435\u043d\u044f\u0442\u044c \u0433\u043e\u0440\u0438\u0437\u043e\u043d\u0442\u0430\u043b\u044c\u043d\u043e\u0435 \u043c\u0430\u0441\u0448\u0442\u0430\u0431\u0438\u0440\u043e\u0432\u0430\u043d\u0438\u0435 \u0432\u0430\u0448\u0435\u0433\u043e \u043f\u0440\u0438\u043b\u043e\u0436\u0435\u043d\u0438\u044f, \u043f\u043e\u0441\u043a\u043e\u043b\u044c\u043a\u0443 \u0437\u0430\u0441\u0442\u0430\u0432\u043b\u044f\u0435\u0442 \u0432\u0441\u0435 \u0438\u043d\u0441\u0442\u0430\u043d\u0441\u044b \u043f\u0440\u0438\u043b\u043e\u0436\u0435\u043d\u0438\u044f \u0441\u043c\u043e\u0442\u0440\u0435\u0442\u044c \u0432 \u0435\u0434\u0438\u043d\u044b\u0439 \u0438\u0441\u0442\u043e\u0447\u043d\u0438\u043a \u0434\u0430\u043d\u043d\u044b\u0445, \u0432\u043c\u0435\u0441\u0442\u043e \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u043d\u0438\u044f \u043b\u043e\u043a\u0430\u043b\u044c\u043d\u043e\u0433\u043e \u0441\u0442\u0435\u0439\u0442\u0430<\/p>\n<\/li>\n<li>\n<p>\u0412 \u0440\u0430\u043c\u043a\u0430\u0445 \u0434\u0430\u043d\u043d\u043e\u0433\u043e \u0433\u0430\u0439\u0434\u0430 \u043c\u044b \u0441\u043e\u0431\u0435\u0440\u0435\u043c \u0441\u043e\u0431\u0441\u0442\u0432\u0435\u043d\u043d\u044b\u0439 \u0432\u0430\u0440\u0438\u0430\u043d\u0442 \u0441\u0435\u0440\u0432\u0435\u0440\u0430 \u0434\u043b\u044f \u0444\u0440\u0435\u0439\u043c\u0432\u043e\u0440\u043a\u0430 <strong>NextJS<\/strong>, \u0434\u043e\u0431\u0430\u0432\u0438\u0432 \u043a\u044d\u0448\u0438\u0440\u043e\u0432\u0430\u043d\u0438\u0435 \u0441\u0433\u0435\u043d\u0435\u0440\u0438\u0440\u043e\u0432\u0430\u043d\u044b\u0445 \u0441\u0442\u0440\u0430\u043d\u0438\u0446 \u0438 json \u0444\u0430\u0439\u043b\u043e\u0432, \u0441\u043e\u0434\u0435\u0440\u0436\u0430\u0449\u0438\u0445 \u043f\u0440\u043e\u043f\u0441\u044b \u044d\u0442\u0438\u0445 \u0441\u0442\u0440\u0430\u043d\u0438\u0446 ( \u0434\u043b\u044f \u0443\u0441\u043a\u043e\u0440\u0435\u043d\u0438\u044f \u043d\u0430\u0432\u0438\u0433\u0430\u0446\u0438\u0438 \u0437\u0430 \u0441\u0447\u0435\u0442 \u043f\u0440\u0435\u0444\u0435\u0442\u0447\u0435\u0439)<\/p>\n<\/li>\n<\/ol>\n<p><strong>\u0428\u0430\u0433 1. \u0423\u0441\u0442\u0430\u043d\u043e\u0432\u043a\u0430 NextJS<\/strong><\/p>\n<p><code>yarn create next-app<\/code><\/p>\n<p><strong>\u0428\u0430\u0433 2. \u0423\u0441\u0442\u0430\u043d\u043e\u0432\u043a\u0430 \u043f\u0430\u043a\u0435\u0442\u043e\u0432:<\/strong><\/p>\n<p><code>yarn add express redis node-gzip<\/code><\/p>\n<p><strong>\u0428\u0430\u0433 3. \u0421\u043e\u0437\u0434\u0430\u0435\u043c \u0432\u00a0 \u043a\u043e\u0440\u043d\u0435 \u043f\u0440\u043e\u0435\u043a\u0442\u0430 \u0444\u0430\u0439\u043b server.js<\/strong><\/p>\n<pre><code class=\"javascript\">const express = require('express') const next = require('next') const { promisify } = require('util') const {gzip} = require('node-gzip');  const client = redis.createClient('redis:\/\/localhost:6379') client.get = promisify(client.get)  const app = next({ dev: false }) const handle = app.getRequestHandler()  app.prepare().then(() => {    const server = express()    server.get('*', (req, res) => handle(req, res))    server.listen(3000, (err) => {     if ( err ) throw err     console.log(`> Ready on http:\/\/localhost:3000`)   }) }) <\/code><\/pre>\n<ol>\n<li>\n<p>\u041f\u043e\u0434\u043a\u043b\u044e\u0447\u0430\u0435\u043c <strong>express<\/strong> \u0434\u043b\u044f \u0441\u043e\u0437\u0434\u0430\u043d\u0438\u044f \u0441\u0435\u0440\u0432\u0435\u0440\u0430<\/p>\n<\/li>\n<li>\n<p>\u041f\u043e\u0434\u043a\u043b\u044e\u0447\u0430\u0435\u043c <strong>next<\/strong> \u0434\u043b\u044f \u0441\u043e\u0437\u0434\u0430\u043d\u0438\u044f \u0431\u0430\u0437\u043e\u0432\u043e\u0433\u043e \u043e\u0431\u0440\u0430\u0431\u043e\u0442\u0447\u0438\u043a\u0430 \u0437\u0430\u043f\u0440\u043e\u0441\u043e\u0432 NextJS\u00a0<\/p>\n<\/li>\n<li>\n<p>\u041f\u043e\u0434\u043a\u043b\u044e\u0447\u0430\u0435\u043c \u043f\u0430\u043a\u0435\u0442 \u0434\u043b\u044f \u0441\u0436\u0430\u0442\u0438\u044f\u00a0<\/p>\n<\/li>\n<li>\n<p>\u0423\u0441\u0442\u0430\u043d\u0430\u0432\u043b\u0438\u0432\u0430\u0435\u043c \u0441\u043e\u0435\u0434\u0438\u043d\u0435\u043d\u0438\u0435 \u0441 redis, \u0438 \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u0443\u0435\u043c promisify, \u0447\u0442\u043e\u0431\u044b \u0440\u0430\u0431\u043e\u0442\u0430\u0442\u044c \u0441 \u043f\u0440\u043e\u043c\u0438\u0441\u0430\u043c\u0438 \u0432\u043c\u0435\u0441\u0442\u043e \u043a\u043e\u043b\u0431\u044d\u043a\u043e\u0432.<\/p>\n<\/li>\n<li>\n<p>\u0417\u0430\u043f\u0443\u0441\u043a\u0430\u0435\u043c \u0441\u0435\u0440\u0432\u0435\u0440 \u0438 \u0443\u0441\u0442\u0430\u043d\u0430\u0432\u043b\u0438\u0432\u0430\u0435\u043c \u0431\u0430\u0437\u043e\u0432\u044b\u0439 \u043e\u0431\u0440\u0430\u0431\u043e\u0442\u0447\u0438\u043a\u00a0 \u0434\u043b\u044f \u0432\u0441\u0435\u0445 \u0437\u0430\u043f\u0440\u043e\u0441\u043e\u0432 <strong>( * )<\/strong> \u0447\u0435\u0440\u0435\u0437 <strong>handle<\/strong><\/p>\n<\/li>\n<\/ol>\n<p><strong>\u0428\u0430\u0433 4. \u0414\u043e\u0431\u0430\u0432\u043b\u044f\u0435\u043c \u043a\u043e\u043c\u0430\u043d\u0434\u0443\u00a0 \u0437\u0430\u043f\u0443\u0441\u043a\u0430 \u0441\u0435\u0440\u0432\u0435\u0440\u0430 \u0432 package.json<\/strong><\/p>\n<pre><code class=\"json\">  \"scripts\": {    ...     \"start\": \"node server.js\",    ...   },<\/code><\/pre>\n<p><strong>\u0428\u0430\u0433 5. \u0424\u043e\u0440\u043c\u0438\u0440\u043e\u0432\u0430\u043d\u0438\u0435 \u043a\u043b\u044e\u0447\u0435\u0439<\/strong><\/p>\n<p>\u0414\u043b\u044f \u0442\u043e\u0433\u043e, \u0447\u0442\u043e\u0431\u044b \u0432 \u043f\u043e\u043b\u043d\u043e\u0439 \u043c\u0435\u0440\u0435 \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u044c \u043c\u043e\u0449\u044c Redis \u0432 NextJS, \u043d\u0430\u043c \u043d\u0435\u043e\u0431\u0445\u043e\u0434\u0438\u043c\u043e \u043a\u044d\u0448\u0438\u0440\u043e\u0432\u0430\u0442\u044c 2 \u0432\u0438\u0434\u0430 \u0444\u0430\u0439\u043b\u043e\u0432:\u00a0<\/p>\n<ol>\n<li>\n<p>HTML, \u043a\u043e\u0442\u043e\u0440\u044b\u0439 \u043e\u0442\u0434\u0430\u0435\u0442\u0441\u044f \u0432 \u0431\u0440\u0430\u0443\u0437\u0435\u0440 \u043f\u0440\u0438 \u0441\u0435\u0440\u0432\u0435\u0440\u043d\u043e\u0439 \u0433\u0435\u043d\u0435\u0440\u0430\u0446\u0438\u0438 \u0441\u0442\u0440\u0430\u043d\u0438\u0446\u044b<\/p>\n<\/li>\n<li>\n<p>JSON-\u0444\u0430\u0439\u043b \u0434\u043b\u044f \u043a\u0430\u0436\u0434\u043e\u0439 \u0441\u0442\u0440\u0430\u043d\u0438\u0446\u044b, \u043a\u043e\u0442\u043e\u0440\u044b\u0439 \u0433\u0435\u043d\u0435\u0440\u0438\u0440\u0443\u0435\u0442 NextJS \u0434\u043b\u044f \u043a\u043b\u0438\u0435\u043d\u0442\u0441\u043a\u043e\u0439 \u043d\u0430\u0432\u0438\u0433\u0430\u0446\u0438\u0438 ( \u043a\u043e\u0433\u0434\u0430 \u043c\u044b \u043f\u0435\u0440\u0435\u0445\u043e\u0434\u0438\u043c \u043d\u0430 \u0441\u0442\u0440\u0430\u043d\u0438\u0446\u0443 NextJS \u0437\u0430\u043f\u0440\u0430\u0448\u0438\u0432\u0430\u0435\u0442 \u044d\u0442\u043e\u0442 JSON \u0444\u0430\u0439\u043b \u0438\u00a0 \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u0443\u0435\u0442 \u0434\u043b\u044f \u043f\u0435\u0440\u0435\u0434\u0430\u0447\u0438 \u043f\u0440\u043e\u043f\u0441\u043e\u0432 \u0441\u0442\u0440\u0430\u043d\u0438\u0446\u044b<\/p>\n<\/li>\n<\/ol>\n<p><strong>\u0428\u0430\u0433 6. \u0421\u043e\u0437\u0434\u0430\u0435\u043c \u043a\u043b\u044e\u0447\u00a0 \u0434\u043b\u044f SSR<\/strong><\/p>\n<pre><code class=\"javascript\">const getSsrKey = (req) => { \u00a0 return req.url }<\/code><\/pre>\n<p>\u0414\u043b\u044f \u0434\u0430\u043d\u043d\u043e\u0439 \u0437\u0430\u0434\u0430\u0447\u0438 \u0441\u0430\u043c\u044b\u043c \u043f\u0440\u043e\u0441\u0442\u044b\u043c \u0440\u0435\u0448\u0435\u043d\u0438\u0435\u043c \u0434\u043b\u044f \u0444\u043e\u0440\u043c\u0438\u0440\u043e\u0432\u0430\u043d\u0438\u044f \u043a\u043b\u044e\u0447\u0430 \u0431\u0443\u0434\u0435\u0442 \u0432\u0437\u044f\u0442\u044c url \u0441\u0442\u0440\u0430\u043d\u0438\u0446\u044b<\/p>\n<p><strong>\u0428\u0430\u0433 7. \u0421\u043e\u0437\u0434\u0430\u0435\u043c \u043a\u043b\u044e\u0447 \u0434\u043b\u044f JSON<\/strong><\/p>\n<pre><code class=\"javascript\">const getJsonKey = (req) => \u00a0 req.path \u00a0 \u00a0 .match(\/\\\/([^\\\/]+)+\/g) \u00a0 \u00a0 .slice(3) \u00a0 \u00a0 .join('')<\/code><\/pre>\n<p>\u041f\u0443\u0442\u044c \u043a JSON \u0444\u0430\u0439\u043b\u0430\u043c \u0432 \u0440\u0430\u043c\u043a\u0430\u0445 \u0444\u0440\u0435\u0439\u043c\u0432\u043e\u0440\u043a\u0430 NextJS \u0438\u043c\u0435\u0435\u0442 \u0432\u0438\u0434 <em>\/_next\/data\/&lt;BUILD_ID>\/your-page-name.json<\/em><\/p>\n<p>\u041f\u043e\u044d\u0442\u043e\u043c\u0443 \u0441\u0430\u043c\u043e\u0439 \u043f\u043e\u043b\u0435\u0437\u043d\u043e\u0439 \u0447\u0430\u0441\u0442\u044c\u044e \u044d\u0442\u043e\u0433\u043e \u043f\u0443\u0442\u0438 \u044f\u0432\u043b\u044f\u0435\u0442\u0441\u044f \u0442\u043e, \u0447\u0442\u043e \u0438\u0434\u0435\u0442 \u043f\u043e\u0441\u043b\u0435 \u0438\u0434\u0435\u043d\u0442\u0438\u0444\u0438\u043a\u0430\u0442\u043e\u0440\u0430 \u0441\u0431\u043e\u0440\u043a\u0438 &#8212; <strong>your-page-name.json<\/strong>, \u043f\u043e\u044d\u0442\u043e\u043c\u0443 \u0435\u0433\u043e \u043c\u044b \u0438 \u0431\u0443\u0434\u0435\u043c \u0431\u0440\u0430\u0442\u044c \u0434\u043b\u044f \u0444\u043e\u0440\u043c\u0438\u0440\u043e\u0432\u0430\u043d\u0438\u044f \u043a\u043b\u044e\u0447\u0430<\/p>\n<p><strong>\u0428\u0430\u0433 8. \u0421\u043e\u0437\u0434\u0430\u0435\u043c \u043a\u044d\u0448 HTML<\/strong><\/p>\n<pre><code class=\"javascript\">async function ssrCache(req, res) {  \u00a0 const key = getSsrKey(req)\u00a0    const cache = await client.get(key)  \u00a0 if ( cache ) { \u00a0 \u00a0 return res.send(cache) \u00a0 }  \u00a0 const data = await app.renderToHTML(req, res, req.path, { ...req.query, ...req.params })  \u00a0 if ( res.statusCode === 200 &amp;&amp; data ) client.set(key, data)  \u00a0 return res.send(data) }<\/code><\/pre>\n<p>\u0414\u043b\u044f \u0440\u0435\u043d\u0434\u0435\u0440\u0438\u043d\u0433\u0430 \u0441\u0442\u0440\u0430\u043d\u0438\u0446 nextJS \u043f\u0440\u0435\u0434\u043e\u0441\u0442\u0430\u0432\u043b\u044f\u0435\u0442 \u043c\u0435\u0442\u043e\u0434 <strong>renderToHTML<\/strong>. \u041f\u043e\u044d\u0442\u043e\u043c\u0443 \u043d\u0430\u0448\u0430 \u0437\u0430\u0434\u0430\u0447\u0430 \u0441\u043e\u0441\u0442\u043e\u0438\u0442 \u0432 \u0442\u043e\u043c, \u0447\u0442\u043e\u0431\u044b:\u00a0<\/p>\n<ol>\n<li>\n<p>\u041f\u043e\u043f\u044b\u0442\u0430\u0442\u044c\u0441\u044f \u043f\u043e\u043b\u0443\u0447\u0438\u0442\u044c \u0437\u043d\u0430\u0447\u0435\u043d\u0438\u0435 \u0438\u0437 \u043a\u044d\u0448\u0430 Redis \u043f\u043e \u043a\u043b\u044e\u0447\u0443.\u00a0<\/p>\n<\/li>\n<li>\n<p>\u0415\u0441\u043b\u0438 \u043a\u044d\u0448 \u0441\u0443\u0449\u0435\u0441\u0442\u0432\u0443\u0435\u0442, \u0442\u043e \u043c\u0433\u043d\u043e\u0432\u0435\u043d\u043d\u043e \u043e\u0442\u0434\u0430\u0442\u044c \u0432 \u0431\u0440\u0430\u0443\u0437\u0435\u0440<\/p>\n<\/li>\n<li>\n<p>\u0415\u0441\u043b\u0438 \u0436\u0435 \u043a\u044d\u0448\u0430 \u043d\u0435\u0442, \u0442\u043e \u043e\u0442\u0440\u0435\u043d\u0434\u0435\u0440\u0438\u0442\u044c \u0441\u0442\u0440\u0430\u043d\u0438\u0446\u0443 \u0441 \u043f\u043e\u043c\u043e\u0449\u044c\u044e \u043c\u0435\u0442\u043e\u0434\u0430 <strong>renderToHTML<\/strong><\/p>\n<\/li>\n<li>\n<p>\u0415\u0441\u043b\u0438 \u0441\u0442\u0440\u0430\u043d\u0438\u0446\u0430 \u0431\u044b\u043b\u0430 \u043e\u0442\u0440\u0435\u043d\u0434\u0435\u0440\u0435\u043d\u0430 \u0443\u0441\u043f\u0435\u0448\u043d\u043e, \u0437\u0430\u043a\u044d\u0448\u0438\u0440\u043e\u0432\u0430\u0442\u044c \u043f\u043e\u043b\u0443\u0447\u0435\u043d\u043d\u044b\u0439 HTML, \u043f\u043e\u0441\u043b\u0435 \u0447\u0435\u0433\u043e \u043e\u0442\u0434\u0430\u0442\u044c \u0432 \u0431\u0440\u0430\u0443\u0437\u0435\u0440<\/p>\n<\/li>\n<\/ol>\n<p><strong>\u0428\u0430\u0433 9. \u0421\u043e\u0437\u0434\u0430\u0435\u043c \u043a\u044d\u0448 JSON<\/strong><\/p>\n<pre><code class=\"javascript\">async function jsonCache(req, res) {  \u00a0 const key = getJsonKey(req)  \u00a0 const cache = await client.get(key)  \u00a0 if ( cache ) {  \u00a0 \u00a0 const headersToWrite = { \u00a0 \u00a0 \u00a0 'content-type': 'application\/json', \u00a0 \u00a0 \u00a0 'content-encoding': 'gzip' \u00a0 \u00a0 }  \u00a0 \u00a0 const buffer = JSON.parse(cache, (k, v) => {  \u00a0 \u00a0 \u00a0 if ( v !== null &amp;&amp; typeof v === 'object' &amp;&amp; 'type' in v &amp;&amp;        v.type === 'Buffer' &amp;&amp;  'data' in v &amp;&amp; Array.isArray(v.data) ) { \u00a0 \u00a0 \u00a0 \u00a0 return Buffer.from(v.data) \u00a0 \u00a0 \u00a0 }  \u00a0 \u00a0 \u00a0 return v \u00a0 \u00a0 })  \u00a0 \u00a0 Object.entries(headersToWrite).forEach(([ key, value ]) => res.setHeader(key, value))  \u00a0 \u00a0 return res.send(buffer)  \u00a0 }  \u00a0 const rawResEnd = res.end \u00a0 const rawResWrite = res.write \u00a0 const chunks = []  \u00a0 const proxyWrite = new Proxy(res.write, {  \u00a0 \u00a0 apply(target, thisArg, args) { \u00a0 \u00a0 \u00a0 const chunk = Buffer.from(args[ 0 ]) \u00a0 \u00a0 \u00a0 chunks.push(chunk) \u00a0 \u00a0 }  \u00a0 })  \u00a0 res.write = proxyWrite  \u00a0 const data = await new Promise(async (resolve) => {  \u00a0 \u00a0 res.end = async (res) => { \u00a0 \u00a0 \u00a0 resolve(res || chunks) \u00a0 \u00a0 }  \u00a0 \u00a0 await app.render(req, res, req.path, { \u00a0 \u00a0 \u00a0 ...req.query, \u00a0 \u00a0 \u00a0 ...req.params \u00a0 \u00a0 })  \u00a0 })  \u00a0 res.write = rawResWrite \u00a0 res.end = rawResEnd  \u00a0 const isChunked = Array.isArray(data) \u00a0 const response = isChunked ?\u00a0 Buffer.concat(data) : (Buffer.from(data)) \u00a0 const serializedResponse = isChunked ? JSON.stringify(response) : (JSON.stringify(await gzip(response)))  \u00a0 if ( res.statusCode === 200 &amp;&amp; data ) client.set(key, serializedResponse)  \u00a0 return res.end(response) }<\/code><\/pre>\n<p>\u0421 JSON \u043a\u044d\u0448\u0435\u043c \u0441\u0438\u0442\u0443\u0430\u0446\u0438\u044f \u043d\u0435\u043c\u043d\u043e\u0433\u043e \u0441\u043b\u043e\u0436\u043d\u0435\u0435. \u0414\u043b\u044f \u0440\u0435\u043d\u0434\u0435\u0440\u0430 JSON \u043c\u044b \u043c\u043e\u0436\u0435\u043c \u0432\u043e\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u044c\u0441\u044f \u043c\u0435\u0442\u043e\u0434\u043e\u043c\u00a0 <strong>render<\/strong>. \u041f\u0440\u043e\u0431\u043b\u0435\u043c\u0430 \u0437\u0430\u043a\u043b\u044e\u0447\u0430\u0435\u0442\u0441\u044f \u0432 \u0442\u043e\u043c, \u0447\u0442\u043e \u044d\u0442\u043e\u0442 \u043c\u0435\u0442\u043e\u0434 \u043d\u0435 \u0432\u043e\u0437\u0432\u0440\u0430\u0449\u0430\u0435\u0442 \u043d\u0438\u043a\u0430\u043a\u043e\u0433\u043e \u0437\u043d\u0430\u0447\u0435\u043d\u0438\u044f, \u0430 \u0441\u0440\u0430\u0437\u0443 \u043e\u0442\u0434\u0430\u0435\u0442 \u0435\u0433\u043e \u0432 \u0431\u0440\u0430\u0443\u0437\u0435\u0440. \u0422\u0430\u043a\u0438\u043c \u043e\u0431\u0440\u0430\u0437\u043e\u043c, \u0434\u043b\u044f \u0440\u0435\u0448\u0435\u043d\u0438\u044f \u0434\u0430\u043d\u043d\u043e\u0439 \u0437\u0430\u0434\u0430\u0447\u0438 \u043d\u0430\u043c \u043d\u0435\u043e\u0431\u0445\u043e\u0434\u0438\u043c\u043e \u043f\u043e\u0434\u043c\u0435\u043d\u0438\u0442\u044c \u0444\u0443\u043d\u043a\u0446\u0438\u043e\u043d\u0430\u043b\u044c\u043d\u043e\u0441\u0442\u044c \u0441\u043b\u0435\u0434\u0443\u044e\u0449\u0438\u0445 \u043c\u0435\u0442\u043e\u0434\u043e\u0432, \u043a\u043e\u0442\u043e\u0440\u044b\u0435 \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u0443\u044e\u0442\u0441\u044f \u0434\u043b\u044f \u0432\u044b\u0434\u0430\u0447\u0438 \u0432 \u0431\u0440\u0430\u0443\u0437\u0435\u0440:<\/p>\n<ol>\n<li>\n<p><strong>res.write <\/strong>&#8212; \u0432\u043c\u0435\u0441\u0442\u043e \u0442\u043e\u0433\u043e, \u0447\u0442\u043e\u0431\u044b \u0432\u044b\u0432\u043e\u0434\u0438\u0442\u044c \u0434\u0430\u043d\u043d\u044b\u0435 \u043c\u044b \u0441\u0434\u0435\u043b\u0430\u0435\u043c \u0442\u0430\u043a, \u0447\u0442\u043e\u0431\u044b \u044d\u0442\u0438 \u0434\u0430\u043d\u043d\u044b\u0435 \u043f\u043e\u043f\u0430\u0434\u0430\u043b\u0438 \u0432 \u043c\u0430\u0441\u0441\u0438\u0432 chunks.<\/p>\n<\/li>\n<li>\n<p><strong>res.end &#8212; <\/strong>\u0432\u043c\u0435\u0441\u0442\u043e \u0442\u043e\u0433\u043e, \u0447\u0442\u043e\u0431\u044b \u0437\u0430\u0432\u0435\u0440\u0448\u0430\u0442\u044c \u043f\u0440\u043e\u0446\u0435\u0441\u0441 \u043e\u0442\u0432\u0435\u0442\u0430 \u0431\u0443\u0434\u0435\u0442 \u0441\u0438\u0433\u043d\u0430\u043b\u0438\u0437\u0438\u0440\u043e\u0432\u0430\u0442\u044c \u043d\u0430\u043c \u043e \u0442\u043e\u043c, \u0447\u0442\u043e \u0432 <strong>chunks<\/strong> \u0443\u0436\u0435 \u043b\u0435\u0436\u0430\u0442 \u0432\u0441\u0435 \u043d\u0435\u043e\u0431\u0445\u043e\u0434\u0438\u043c\u044b\u0435 \u0434\u043b\u044f \u0432\u044b\u0432\u043e\u0434\u0430 \u0434\u0430\u043d\u043d\u044b\u0435, \u0438 \u043c\u044b \u043c\u043e\u0436\u0435\u043c \u043d\u0430\u0447\u0430\u0442\u044c \u0441 \u043d\u0438\u043c \u0440\u0430\u0431\u043e\u0442\u0430\u0442\u044c<\/p>\n<\/li>\n<li>\n<p>\u0422\u0435\u043f\u0435\u0440\u044c \u043c\u044b \u0434\u0435\u043b\u0430\u0435\u043c \u0442\u0443 \u0436\u0435 \u0441\u0430\u043c\u0443\u044e \u043f\u0440\u043e\u0432\u0435\u0440\u043a\u0443 \u043d\u0430 \u043d\u0430\u043b\u0438\u0447\u0438\u0435 \u043a\u044d\u0448\u0430, \u0438 \u0435\u0441\u043b\u0438 \u043e\u043d \u0441\u0443\u0449\u0435\u0441\u0442\u0432\u0443\u0435\u0442, \u0442\u043e \u043f\u0430\u0440\u0441\u0438\u043c JSON \u0432\u00a0 <strong>Buffer<\/strong>, \u0434\u043e\u0431\u0430\u0432\u043b\u044f\u0435\u043c \u0437\u0430\u0433\u043e\u043b\u043e\u0432\u043a\u0438 \u043e \u0442\u0438\u043f\u0435 \u0438 \u043a\u043e\u0434\u0438\u0440\u043e\u0432\u043a\u0435 \u0434\u0430\u043d\u043d\u044b\u0445 \u0438 \u043e\u0442\u0434\u0430\u0435\u043c \u0432 \u0431\u0440\u0430\u0443\u0437\u0435\u0440<\/p>\n<\/li>\n<li>\n<p>\u0415\u0441\u043b\u0438 \u043a\u044d\u0448\u0430 \u043d\u0435\u0442, \u0442\u043e \u0437\u0430\u043f\u043e\u043c\u0438\u043d\u0430\u0435\u043c \u0431\u0430\u0437\u043e\u0432\u044b\u0435 \u0444\u0443\u043d\u043a\u0446\u0438\u0438 <strong>res.write<\/strong> \u0438 <strong>res.end<\/strong> \u0432 \u043f\u0435\u0440\u0435\u043c\u0435\u043d\u043d\u0443\u044e, \u0447\u0442\u043e\u0431\u044b \u043f\u043e\u0442\u043e\u043c \u0438\u043c\u0435\u0442\u044c \u0432\u043e\u0437\u043c\u043e\u0436\u043d\u043e\u0441\u0442\u044c \u0438\u0445 \u0432\u043e\u0441\u0441\u0442\u0430\u043d\u043e\u0432\u0438\u0442\u044c<\/p>\n<\/li>\n<li>\n<p>\u041f\u0435\u0440\u0435\u043e\u043f\u0440\u0435\u0434\u0435\u043b\u044f\u0435\u043c <strong>write<\/strong> \u0438 <strong>end<\/strong> \u043f\u0440\u043e\u043a\u0441\u0438 \u0444\u0443\u043d\u043a\u0446\u0438\u044f\u043c\u0438, \u043a\u043e\u0442\u043e\u0440\u044b\u0435 \u043e\u0431\u0441\u0443\u0434\u0438\u043b\u0438 \u0432 \u043f\u0443\u043d\u043a\u0442\u0430\u0445 1 \u0438 2<\/p>\n<\/li>\n<li>\n<p>\u0420\u0435\u043d\u0434\u0435\u0440\u0438\u043c \u043a\u043e\u043d\u0442\u0435\u043d\u0442 \u0441 \u043f\u043e\u043c\u043e\u0449\u044c\u044e <strong>render<\/strong>, \u043d\u043e \u0431\u043b\u0430\u0433\u043e\u0434\u0430\u0440\u044f \u043d\u0430\u0448\u0438\u043c \u043f\u0440\u043e\u043a\u0441\u0438 \u0444\u0443\u043d\u043a\u0446\u0438\u044f\u043c \u043e\u043d \u043d\u0435 \u0431\u0443\u0434\u0435\u0442 \u043e\u0442\u0434\u0430\u043d \u0431\u0440\u0430\u0443\u0437\u0435\u0440, \u0430 \u0443\u0434\u043e\u0431\u043d\u043e \u043e\u043a\u0430\u0436\u0435\u0442\u0441\u044f \u0432 \u0441\u043f\u0435\u0446\u0438\u0430\u043b\u044c\u043d\u043e \u0441\u043e\u0437\u0434\u0430\u043d\u043d\u043e\u0439 \u043d\u0430\u043c\u0438 \u043f\u0435\u0440\u0435\u043c\u0435\u043d\u043d\u043e\u0439\u00a0<\/p>\n<\/li>\n<li>\n<p>\u0412\u043e\u0441\u0441\u0442\u0430\u043d\u0430\u0432\u043b\u0438\u0432\u0430\u0435\u043c \u0431\u0430\u0437\u043e\u0432\u044b\u0435 \u0432\u0435\u0440\u0441\u0438\u0438 \u0444\u0443\u043d\u043a\u0446\u0438\u0439 <strong>write<\/strong> \u0438 <strong>end<\/strong><\/p>\n<\/li>\n<li>\n<p>\u0414\u0430\u043b\u0435\u0435 \u043c\u043e\u0436\u0435\u0442 \u0431\u044b\u0442\u044c \u0434\u0432\u0430 \u0432\u0430\u0440\u0438\u0430\u043d\u0442\u0430 \u0442\u043e\u0433\u043e, \u043a\u0430\u043a \u043c\u044b \u043c\u043e\u0436\u0435\u043c \u0441\u043e\u0431\u0440\u0430\u0442\u044c \u043e\u0442\u0432\u0435\u0442 &#8212; \u0435\u0441\u043b\u0438 \u043a\u043e\u043d\u0442\u0435\u043d\u0442 \u043d\u0435 \u0447\u0430\u043d\u043a\u0430\u043b\u0441\u044f, \u0442\u043e \u0442\u043e\u0433\u0434\u0430 \u043d\u0430 \u0432\u044b\u0445\u043e\u0434\u0435 \u043c\u044b \u043f\u043e\u043b\u0443\u0447\u0438\u043c \u0441\u0442\u0440\u043e\u043a\u0443(JSON) \u0438 \u0441\u043e\u0437\u0434\u0430\u0434\u0438\u043c \u0431\u0443\u0444\u0435\u0440 \u0438\u0437 \u043d\u0435\u0435, \u0432 \u0438\u043d\u043e\u043c \u0441\u043b\u0443\u0447\u0430\u0435 \u0441\u043e\u0435\u0434\u0438\u043d\u044f\u0435\u043c \u043a\u0443\u0441\u043e\u0447\u043a\u0438, \u043a\u043e\u0442\u043e\u0440\u044b\u0435 \u0441\u043e\u0431\u0440\u0430\u043b\u0438 \u0441 \u043f\u043e\u043c\u043e\u0449\u044c\u044e \u043f\u0440\u043e\u043a\u0441\u0438\u0440\u043e\u0432\u0430\u043d\u0438\u044f \u0444\u0443\u043d\u043a\u0446\u0438\u0438 write \u0432 \u0435\u0434\u0438\u043d\u044b\u0439 \u0431\u0443\u0444\u0435\u0440.\u00a0<\/p>\n<\/li>\n<li>\n<p>\u0415\u0441\u043b\u0438 \u043c\u044b \u043f\u043e\u043b\u0443\u0447\u0438\u043b\u0438 \u0443\u0441\u043f\u0435\u0448\u043d\u044b\u0439 \u043e\u0442\u0432\u0435\u0442, \u0442\u043e \u0441\u0435\u0440\u0438\u0430\u043b\u0438\u0437\u0443\u0435\u043c \u0431\u0443\u0444\u0435\u0440, \u0441\u0436\u0438\u043c\u0430\u0435\u043c (\u0432 \u0441\u043b\u0443\u0447\u0430\u0435 \u043d\u0435\u043e\u0431\u0445\u043e\u0434\u0438\u043c\u043e\u0441\u0442\u0438)\u00a0\u0438 \u0441\u043e\u0445\u0440\u0430\u043d\u044f\u0435\u043c \u0432 \u043a\u044d\u0448\u0435 \u0440\u0435\u0434\u0438\u0441\u0430<\/p>\n<\/li>\n<li>\n<p>\u0417\u0430\u0432\u0435\u0440\u0448\u0430\u0435\u043c \u043e\u0442\u0432\u0435\u0442 \u043c\u0435\u0442\u043e\u0434\u043e\u043c <strong>end<\/strong><\/p>\n<\/li>\n<\/ol>\n<p><strong>\u0428\u0430\u0433 11. \u0421\u043e\u0431\u0438\u0440\u0430\u0435\u043c \u0432\u0441\u0435 \u0432\u043c\u0435\u0441\u0442\u0435\u00a0<\/strong><\/p>\n<p>\u0414\u043b\u044f <strong>SSR<\/strong> \u0441\u0442\u0440\u0430\u043d\u0438\u0446 \u043c\u044b \u043f\u0440\u0438\u043c\u0435\u043d\u044f\u0435\u043c <strong>ssrCache<\/strong>, \u0434\u043b\u044f JSON \u0444\u0430\u0439\u043b\u043e\u0432 &#8212; <strong>jsonCache<\/strong><\/p>\n<pre><code class=\"javascript\">server.get('\/', (req, res) => { \u00a0 return ssrCache(req, res) })  server.get('\/_next\/data\/*', async (req, res) => { \u00a0 return jsonCache(req, res) })  server.get('*', (req, res) => handle(req, res))<\/code><\/pre>\n<p>\u0421\u0441\u044b\u043b\u043a\u0430 \u043d\u0430 \u0440\u0435\u043f\u043e\u0437\u0438\u0442\u043e\u0440\u0438\u0439: <a href=\"https:\/\/github.com\/IAlexanderI1994\/next-redis-article\" rel=\"noopener noreferrer nofollow\">https:\/\/github.com\/IAlexanderI1994\/next-redis-article<\/a><\/p>\n<p>\u0411\u043b\u0430\u0433\u043e\u0434\u0430\u0440\u044e \u0437\u0430 \u043f\u0440\u043e\u0447\u0442\u0435\u043d\u0438\u0435. \u0411\u0443\u0434\u0443 \u0440\u0430\u0434 \u0432\u0430\u0448\u0438\u043c \u0432\u043e\u043f\u0440\u043e\u0441\u0430\u043c \u0438 \u043a\u043e\u043c\u043c\u0435\u043d\u0442\u0430\u0440\u0438\u044f\u043c.<\/p>\n<\/div>\n<\/div>\n<\/div>\n<p><!----><!----><\/div>\n<p><!----><!----><br \/> \u0441\u0441\u044b\u043b\u043a\u0430 \u043d\u0430 \u043e\u0440\u0438\u0433\u0438\u043d\u0430\u043b \u0441\u0442\u0430\u0442\u044c\u0438 <a href=\"https:\/\/habr.com\/ru\/articles\/576874\/\"> https:\/\/habr.com\/ru\/articles\/576874\/<\/a><br \/><\/br><\/br><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[],"tags":[],"class_list":["post-408109","post","type-post","status-publish","format-standard","hentry"],"_links":{"self":[{"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=\/wp\/v2\/posts\/408109","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=408109"}],"version-history":[{"count":0,"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=\/wp\/v2\/posts\/408109\/revisions"}],"wp:attachment":[{"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=408109"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=408109"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=408109"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}