【发布时间】:2022-01-18 00:04:05
【问题描述】:
我的 Meteor/React 应用程序应该呈现一个静态页面,除了具有响应式 UI 的响应式页面。静态包在浏览器中显示后甚至不需要使用 React 魔法“水合”。尽管使用 React 组件,服务器上的服务器端渲染将是动态的。
我得到了它的工作,但我不确定这是否是预期的官方方式。
文件 import/client/routes.js
...
<Route path="/reactive/pages/:id" component={ReactiveComponent} />
<Route path="/static_url" />
...
文件 server/main.jsx
...
onPageLoad((sink) => {
if (sink.request.path === '/static_url) {
sink.renderIntoElementById('app', renderToString(
<StaticPage />,
));
}
});
...
文件 client/main.js
...
import { Routes } from '../imports/client/routes';
Meteor.startup(() => {
...
if (window.location.pathname !== '/offer_pdf') {
render(Routes, document.getElementById('app'));
}
});
...
尤其是在渲染依赖于 URI 时,对我来说似乎有点 hacky。是否存在更优雅的解决方案?
【问题讨论】:
-
我也这样做。它有效,但我不确定这是最好的方法。
标签: reactjs meteor server-side-rendering