├── README.md ├── .gitignore ├── package.json ├── app.js ├── puppeteer-pool.js ├── spider.js └── LICENSE /README.md: -------------------------------------------------------------------------------- 1 | # vue seo puppetter方案 2 | -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | .DS_Store 2 | Thumbs.db 3 | db.json 4 | *.log 5 | node_modules/ 6 | public/ 7 | .deploy*/ 8 | user_agent.txt -------------------------------------------------------------------------------- /package.json: -------------------------------------------------------------------------------- 1 | { 2 | "dependencies": { 3 | "express": "^4.17.1", 4 | "fd-op": "^1.0.0", 5 | "puppeteer": "^21.7.0" 6 | } 7 | } 8 | -------------------------------------------------------------------------------- /app.js: -------------------------------------------------------------------------------- 1 | const express = require('express') 2 | const app = express() 3 | const spider = require('./spider.js') 4 | 5 | app.get('*', async (req, res, next) => { 6 | // 部署到服务器的完整URL 7 | const url = 'https://www.ling0523.cn' + req.originalUrl 8 | console.log('请求的完整URL:' + url) 9 | const content = await spider(url).catch(error => { 10 | console.log(error) 11 | res.send('获取html内容失败') 12 | return 13 | }) 14 | res.send(content) 15 | }) 16 | 17 | app.listen(3000, () => { 18 | console.log('预渲染服务已启动!') 19 | }) 20 | -------------------------------------------------------------------------------- /puppeteer-pool.js: -------------------------------------------------------------------------------- 1 | const puppeteer = require('puppeteer') 2 | const MAX_WSE = 2 //启动几个浏览器 3 | let WSE_LIST = [] //存储browserWSEndpoint列表 4 | 5 | ;(async () => { 6 | for (let i = 0; i < MAX_WSE; i++) { 7 | const browser = await puppeteer.launch({ 8 | headless: true, 9 | args: ['--disable-gpu', '--disable-dev-shm-usage', '--disable-setuid-sandbox', '--no-first-run', '--no-sandbox', '--no-zygote', '--single-process'] 10 | }) 11 | const browserWSEndpoint = await browser.wsEndpoint() 12 | WSE_LIST.push(browserWSEndpoint) 13 | } 14 | })() 15 | 16 | module.exports = WSE_LIST 17 | -------------------------------------------------------------------------------- /spider.js: -------------------------------------------------------------------------------- 1 | const puppeteer = require('puppeteer') 2 | const WSE_LIST = require('./puppeteer-pool.js') 3 | const spider = async url => { 4 | const tmp = Math.floor(Math.random() * WSE_LIST.length) 5 | const browserWSEndpoint = WSE_LIST[tmp] 6 | const browser = await puppeteer.connect({ 7 | browserWSEndpoint 8 | }) 9 | const page = await browser.newPage() 10 | await page.goto(url, { 11 | timeout: 0, //连接超时时间,单位ms 12 | waitUntil: 'networkidle0' //网络空闲说明已加载完毕 13 | }) 14 | const html = await page.evaluate(() => { 15 | return document.getElementsByTagName('html')[0]?.outerHTML 16 | }) 17 | await page.close() 18 | return html 19 | } 20 | module.exports = spider 21 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | MIT License 2 | 3 | Copyright (c) 2024 so-better 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. 22 | --------------------------------------------------------------------------------