简体   繁体   English

React-snapshot 使用 create-react-app 和 serve 在所有路由上预渲染 index.html

[英]React-snapshot pre-renders index.html on all routes with create-react-app and serve

I am using create-react-app and react-snapshot for pre-rendering.我正在使用 create-react-app 和 react-snapshot 进行预渲染。 Serving the build using serve使用 serve 服务构建

serve -s build -l 80

But strangely for all the routes, only index.js is pre-rendered.但奇怪的是,对于所有路由,只有 index.js 是预渲染的。

Below is the react-snapshot configuration in package.json下面是package.json中的react-snapshot配置

"reactSnapshot": {
    "exclude": [
      "/districts-data",
      "/districts-data?*",
      "/suggest-time",
      "/covid-19-statistics"
    ],
    "snapshotDelay": 300
  }

I have excluded all the paths that include lazy loading.我已经排除了所有包含延迟加载的路径。 I have gone through this answer but I am not catching anything in my service worker just kept it for PWA.我已经完成了这个答案,但我没有在我的服务人员中发现任何东西,只是为 PWA 保留了它。

sw.js

self.addEventListener('fetch', function (event) {

});

self.addEventListener('install', function (event) {

});

self.addEventListener('activate', (event) => { });

After investing a couple of days and deep-diving in the serve code I realize the mistake I was doing is having -s option while running serve script.在投入几天时间并深入研究serve代码后,我意识到我犯的错误是在运行服务脚本时使用了 -s 选项。

serve -s build -l 80

Having -s option default all routes served from index.html.Having -s flag is essential if you are just serving build generated via Create react app else routes other than "/" will return a response 400.使用 -s 选项默认从 index.html 提供的所有路由。如果您只是提供通过 Create react app 生成的构建,则使用 -s 标志是必不可少的,否则“/”以外的路由将返回响应 400。

You can simply use serve build if you have all the routes pre-rendered but if there are some routes for which you don't have pre-rendered HTML available (For my use case it was components using Lazy load) then you can create serve.json in public folder with specifying source and destination.如果您已预渲染所有路由,则可以简单地使用serve build但如果有一些路由您没有预渲染 HTML 可用(对于我的用例,它是使用延迟加载的组件),那么您可以创建 serve .json 在指定源和目标的公用文件夹中。

 {
  "rewrites": [
    { "source": "/districts-data", "destination": "/200.html" },
    { "source": "/districts-data?*", "destination": "/200.html" },
    { "source": "/suggest-time", "destination": "/200.html" },
    { "source": "/covid-19-statistics", "destination": "/200.html" }
  ]
}

You also need to change your serve command to serve -c serve.json build .您还需要将服务命令更改为serve -c serve.json build -c is used to tell Specify custom path to serve.json . -c 用于告诉 Specify custom path to serve.json

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 使用react + react-router-dom获取PHP服务器以为所有路由提供index.html - Get php server to serve index.html for all routes using react + react-router-dom 我可以在没有 npm 启动和 npx create-react-app 的情况下使用 React 运行 index.html 吗? - Can I run index.html with React without npm start and npx create-react-app? Create-React-App 应用程序中 index.html 和 index.js 之间的连接在哪里? - Where's the connection between index.html and index.js in a Create-React-App application? 如何将 create-react-app 与提供 index.html 的后端一起使用? - How to use create-react-app with a backend which serves index.html? reactjs create-react-app在src文件夹中缺少index.html - reactjs create-react-app missing index.html in src folder 从 create-react-app 中的 service worker 缓存中排除 index.html - exclude index.html from service worker cache in create-react-app 如何从服务器正确提供 create-react-app 索引? - How to properly serve the create-react-app index from the server? 如何使用 index.php 而不是 index.html 在 XAMP 上运行由 create-react-app 创建的 ReactApp? - How to run ReactApp created by create-react-app on XAMP using index.php rather than index.html? 我正在使用create-react-app,需要在没有服务器的情况下直接在浏览器中打开build / index.html - I'm using create-react-app and I need to open build/index.html in browser directly without a server 为生产/开发创建不同的React应用public / index.html - Create react app different public/index.html for production/dev
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM