2023-09-14 15:54:29 +08:00
|
|
|
import puppeteer from 'puppeteer';
|
|
|
|
import redisClient from './redis/index.js';
|
|
|
|
|
2023-09-27 17:54:24 +08:00
|
|
|
const MAX_WSE = 4; //启动几个浏览器
|
|
|
|
let WSE_LIST = []; //存储browserWSEndpoint列表
|
|
|
|
|
|
|
|
init()
|
|
|
|
|
2023-09-14 15:54:29 +08:00
|
|
|
async function ssr(url) {
|
|
|
|
const REDIS_KEY = `ssr:${url}`;
|
|
|
|
const CACHE_TIME = 600; // 10 分钟缓存
|
|
|
|
const CACHE_HTML = await redisClient.get(REDIS_KEY);
|
|
|
|
|
|
|
|
if (CACHE_HTML) {
|
|
|
|
return { html: CACHE_HTML, ttRenderMs: 0 };
|
|
|
|
}
|
|
|
|
|
|
|
|
const start = Date.now();
|
|
|
|
|
2023-09-27 17:54:24 +08:00
|
|
|
let tmp = Math.floor(Math.random()* MAX_WSE);
|
|
|
|
let browserWSEndpoint = WSE_LIST[tmp];
|
|
|
|
const browser = await puppeteer.connect({browserWSEndpoint});
|
|
|
|
|
2023-09-14 15:54:29 +08:00
|
|
|
const page = await browser.newPage();
|
|
|
|
try {
|
|
|
|
// networkidle0 waits for the network to be idle (no requests for 500ms).
|
|
|
|
await page.goto(url, {waitUntil: 'networkidle0'});
|
2023-09-27 17:54:24 +08:00
|
|
|
await page.waitForSelector('#root', { timeout: 30000 }); // ensure #posts exists in the DOM.
|
2023-09-14 15:54:29 +08:00
|
|
|
} catch (err) {
|
|
|
|
console.error(err);
|
2023-09-27 17:54:24 +08:00
|
|
|
// throw new Error('page.goto/waitForSelector timed out.');
|
2023-09-14 15:54:29 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
const html = await page.content(); // serialized HTML of page DOM.
|
2023-09-27 17:54:24 +08:00
|
|
|
await page.close();
|
2023-09-14 15:54:29 +08:00
|
|
|
|
|
|
|
const ttRenderMs = Date.now() - start;
|
|
|
|
console.info(`Puppeteer rendered page: ${url} in: ${ttRenderMs}ms`);
|
|
|
|
|
|
|
|
redisClient.set(REDIS_KEY, html, 'EX', CACHE_TIME); // cache rendered page.
|
|
|
|
|
|
|
|
return {html, ttRenderMs};
|
|
|
|
}
|
|
|
|
|
2023-09-27 17:54:24 +08:00
|
|
|
function init(){
|
|
|
|
(async () => {
|
|
|
|
for(var i=0;i<MAX_WSE;i++){
|
|
|
|
const browser = await puppeteer.launch({headless:'new',
|
|
|
|
args: [
|
|
|
|
'--disable-gpu',
|
|
|
|
'--disable-dev-shm-usage',
|
|
|
|
'--disable-setuid-sandbox',
|
|
|
|
'--no-first-run',
|
|
|
|
'--no-sandbox',
|
|
|
|
'--no-zygote',
|
|
|
|
'--single-process'
|
|
|
|
]});
|
|
|
|
let browserWSEndpoint = await browser.wsEndpoint();
|
|
|
|
WSE_LIST[i] = browserWSEndpoint;
|
|
|
|
}
|
|
|
|
console.log(WSE_LIST);
|
|
|
|
})();
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2023-09-14 15:54:29 +08:00
|
|
|
export {ssr as default};
|