[英]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.