【问题标题】:Serve static html files in PWA if service worker is configured in CRA如果在 CRA 中配置了 service worker,则在 PWA 中提供静态 html 文件
【发布时间】:2021-06-03 06:03:32
【问题描述】:
我想提供静态 html 文件,例如PWA 中的 privacy.html、terms.html(使用 Create-React-App 构建)并配置了 service worker。在本地主机上,我可以访问像
这样的文件
http://locahost://terms.html
http://localhost://privacy.html
但是当提供构建(server -s build)或部署到主机时,我得到一个空白页面。我尝试实施多种解决方案来访问静态文件,但无法解决此问题。
【问题讨论】:
标签:
javascript
html
reactjs
progressive-web-apps
service-worker
【解决方案1】:
当您在 CRA 中构建应用程序时,它会为您创建一个 build 目录并将其构建的文件放入其中,为了将 privacy.html 放入构建目录中,您可以手动将privacy.html 在构建目录中,或者使用这样的命令:
cp current/path/to/privacy.html build/privacy.html
更好的方法是构建它的脚本并将其添加到您的package.json 文件中并在当前构建脚本的末尾调用它,这样,每次您构建应用程序时,它都会自动为您执行此操作:
package.json
"build-static-html-page": "cp current/path/to/privacy.html build/privacy.html",
"build": "CURRENT_BUILD_SCRIPT",
"build-project": "build build-static-html-page"