/* 静态预渲染:把 LandingPage 渲染为静态 HTML 注入 dist/index.html 的 #root, 让 Google / Bing / ChatGPT / Perplexity 等不执行 JS 的爬虫也能读到真实正文。 同时生成 robots.txt 与 sitemap.xml。 */ const esbuild = require('esbuild'); const React = require('react'); const ReactDOMServer = require('react-dom/server'); const fs = require('fs'); const path = require('path'); const FRONT = '/app/frontend'; const OUT = path.join(FRONT, 'dist'); // 1) 用 esbuild 把组件打包成 CJS(react/react-dom/react-router-dom 保持 external,复用运行时同一实例) esbuild.buildSync({ entryPoints: [path.join(FRONT, 'prerender-entry.tsx')], bundle: true, format: 'cjs', platform: 'node', outfile: path.join(FRONT, 'prerender-bundle.cjs'), alias: { '@': path.join(FRONT, 'src') }, loader: { '.css': 'empty', '.tsx': 'tsx', '.ts': 'ts' }, jsx: 'automatic', external: ['react', 'react-dom', 'react-router-dom'], logLevel: 'info', }); // 2) 服务端渲染为静态标记 const mod = require(path.join(FRONT, 'prerender-bundle.cjs')); const ssrHtml = ReactDOMServer.renderToStaticMarkup(React.createElement(mod.App)); console.log('[prerender] ssrHtml length =', ssrHtml.length); // 3) 注入 dist/index.html 的 #root let html = fs.readFileSync(path.join(OUT, 'index.html'), 'utf8'); const rootOpen = '
'; if (html.includes(rootOpen)) { html = html.replace(rootOpen, ''); } // 4) 注入 SEO/GEO
元数据(head 中的 JSON-LD 在 JS 挂载后仍保留;#root 内由组件渲染的 JSON-LD 供非JS爬虫读取) const title = '七虎AI · 个人体验 AI、办公提效与 AI 创收的一站式 PaaS 平台'; const desc = '七虎AI 是面向个人与职场用户的 AI PaaS 平台:低价订阅无限 Token、垂直办公模板、可交易技能市场、AI 创作试炼场与低成本养模型,让每个人用 AI 把事做成。'; const jsonLd = { '@context': 'https://schema.org', '@type': 'SoftwareApplication', name: '七虎AI', applicationCategory: 'BusinessApplication', operatingSystem: 'Web', offers: { '@type': 'Offer', price: '0', priceCurrency: 'CNY' }, aggregateRating: { '@type': 'AggregateRating', ratingValue: '4.8', reviewCount: '1000' }, url: 'https://ai.qihuyun.com/', }; const headInject = [ `
`,
` `,
` `,
` `,
` `,
` `,
` `,
` `,
].join('\n');
html = html.replace('', headInject + '\n ');
fs.writeFileSync(path.join(OUT, 'index.html'), html);
// 5) robots.txt
fs.writeFileSync(path.join(OUT, 'robots.txt'),
'User-agent: *\nAllow: /\n\nSitemap: https://ai.qihuyun.com/sitemap.xml\n');
// 6) sitemap.xml(当前公开可索引页仅首页;其余为登录后页面,按需在后续迭代补充)
const urls = ['https://ai.qihuyun.com/'];
const sitemap = '\n' +
'