所以这是一个相当新的话题,React Server Components 最近发布了,和 SSR/Next.js 相比,它对 SEO 有什么影响?
由于组件是在请求时动态呈现在服务器中的,它并不像 Next.js 这样的 SSR 那样静态,如果我使用它,搜索引擎会无法索引这些组件吗?
可以找到一个演示 here
我们可以在 api.server.js
中看到,
async function renderReactTree(res, props) {
await waitForWebpack();
const manifest = readFileSync(
path.resolve(__dirname, '../build/react-client-manifest.json'),
'utf8'
);
const moduleMap = JSON.parse(manifest);
pipeToNodeWritable(React.createElement(ReactApp, props), res, moduleMap);
}
function sendResponse(req, res, redirectToId) {
const location = JSON.parse(req.query.location);
if (redirectToId) {
location.selectedId = redirectToId;
}
res.set('X-Location', JSON.stringify(location));
renderReactTree(res, {
selectedId: location.selectedId,
isEditing: location.isEditing,
searchText: location.searchText,
});
}
我知道这有助于减少客户端设备的工作量,因为组件在服务器上呈现并发送到客户端,并且可以使用存储在服务器中的 secret 来呈现组件,因为我们可以将其作为 Prop 传递而不是我们将 secret 发送给客户。但如果 SEO 很重要,SSR 是否优于 React 服务器组件?
最佳答案
服务器组件是 互补渲染成 HTML,而不是替代方案。 计划是两者兼得。
服务器组件未发布。本着分享我们研究的精神,发布的是早期技术预览。此预览不包括 HTML 渲染器。 api.server.js
您提到的演示中的文件包含对此的评论:
const html = readFileSync(
path.resolve(__dirname, '../build/index.html'),
'utf8'
);
// Note: this is sending an empty HTML shell, like a client-side-only app.
// However, the intended solution (which isn't built out yet) is to read
// from the Server endpoint and turn its response into an HTML stream.
res.send(html);
到服务器组件正式发布时,将有一个流式 HTML 渲染器用于第一次渲染。它还没有建成。
关于javascript - React 服务器组件在 SEO 上的性能,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/65488167/