首页 > 解决方案 > 登录前和登录后用于 SEO 的 React SSR(服务器端渲染)创建 React 应用程序(CRA)。如何在一个项目文件夹中执行此操作?

问题描述

我有一个问题,假设登录前和登录后有两种情况:

在登录 网站之前将使用下一个 js 或其他方式进行 SEO。并且有几个页面,如博客、博客详细信息、关于、联系方式和产品等。

登录 普通功能后,如发布,使用 CRA(创建 React 应用程序)设置发布详细信息。

我的问题是我们如何在登录 SSR 之前和登录之后设置项目。登录后我不想要 SSR,因为每个特性和功能在服务器上都有很多负载。有什么解决办法吗?

我很少有关于同构的文章,例如https://hackernoon.com/get-an-isomorphic-web-app-up-and-running-in-5-minutes-72da028c15dd

示例入门套件项目: https ://github.com/xiaoyunyang/isomorphic-router-demo

我们可以在一个项目设置中同时创建 SSR 和 Create react app 吗?

标签: reactjsreduxreact-reduxcreate-react-appserver-side-rendering

解决方案


如果您使用的是 express 服务器,则可以使用ssr-for-bots中间件将 ssr 仅应用于某些路由。

const ssrForBots = require("ssr-for-bots");

const defaultOptions = {
  prerender: [], // Array containing the user-agents that will trigger the ssr service | uses Regex
  exclude: ["/routesAfterLogin/"], // Array containing paths and/or extentions that will be excluded from being prerendered by the ssr service | uses Regex
  useCache: true, // Variable that determins if we will use page caching or not
  cacheRefreshRate: 86400, // Seconds of which the cache will be kept alive, pass 0 or negative value for infinite lifespan default 24 hours
};

app.use(ssrForBots(defaultOptions)); 

这样登录前的路径将是SSR,登录后的路径(假设它们都以/app开头)将被排除在外


推荐阅读