具有create-react-app和workbox-webpack-plugin的自定义脱机后备页面

时间:2018-10-07 13:47:45

标签: reactjs service-worker progressive-web-apps workbox-webpack-plugin

无论何时预缓存数据,每次离线时我都需要显示一个定制脱机页面。

我正在使用create-react-appworkbox-webpack-plugin

我尝试将NavigationFallback更改为offline.html,但是每次脱机时重新加载页面时,它都会显示index.html页面,而不显示offline.html

有人可以帮我添加自定义的离线后备页面吗?

package.json

below is my package.json screenshot
"dependencies": {
    "@babel/core": "7.1.0",
    "@fortawesome/fontawesome-svg-core": "^1.2.4",
    "@fortawesome/free-solid-svg-icons": "^5.3.1",
    "@fortawesome/react-fontawesome": "^0.1.3",
    "@svgr/webpack": "2.4.1",
"availity-reactstrap-validation": "^2.2.1",
"aws-amplify": "^1.1.6",
"axios": "^0.18.0",
"axios-mock-adapter": "^1.15.0",
"babel-core": "7.0.0-bridge.0",
"babel-eslint": "9.0.0",
"babel-jest": "23.6.0",
"babel-loader": "8.0.4",
"babel-plugin-named-asset-import": "^0.2.2",
"babel-preset-react-app": "^5.0.3",
"bfj": "6.1.1",
"bootstrap": "^4.1.3",
"case-sensitive-paths-webpack-plugin": "2.1.2",
"chalk": "2.4.1",
"classnames": "^2.2.6",
"css-loader": "1.0.0",
"dotenv": "6.0.0",
"dotenv-expand": "4.2.0",
"eslint": "5.6.0",
"eslint-config-react-app": "^3.0.3",
"eslint-loader": "2.1.1",
"eslint-plugin-flowtype": "2.50.1",
"eslint-plugin-import": "2.14.0",
"eslint-plugin-jsx-a11y": "6.1.1",
"eslint-plugin-react": "7.11.1",
"file-loader": "2.0.0",
"firebase": "^5.5.3",
"font-awesome": "^4.7.0",
"fs-extra": "7.0.0",
"html-webpack-plugin": "4.0.0-alpha.2",
"identity-obj-proxy": "3.0.0",
"jest": "23.6.0",
"jest-pnp-resolver": "1.0.1",
"jest-resolve": "23.6.0",
"mini-css-extract-plugin": "0.4.3",
"moment": "^2.22.2",
"object-assign": "^4.1.1",
"offline-js": "^0.7.19",
"optimize-css-assets-webpack-plugin": "5.0.1",
"pnp-webpack-plugin": "1.1.0",
"postcss-flexbugs-fixes": "4.1.0",
"postcss-loader": "3.0.0",
"postcss-preset-env": "6.0.6",
"postcss-safe-parser": "4.0.1",
"prop-types": "^15.6.2",
"qrcode.react": "^0.8.0",
"react": "^16.5.2",
"react-app-polyfill": "^0.1.3",
"react-dev-utils": "^6.0.4",
"react-dom": "^16.5.2",
"react-multistep": "^3.2.3",
"react-overlay-loader": "0.0.3",
"react-plx": "^1.3.9",
"react-redux": "^5.0.7",
"react-router-config": "^1.0.0-beta.4",
"react-router-dom": "^4.3.1",
"react-router-redux": "^5.0.0-alpha.9",
"react-seo": "^1.0.11",
"react-share": "^2.3.1",
"react-telephone-input": "^4.73.2",
"react-toastify": "^4.3.2",
"reactstrap": "^6.4.0",
"redux": "^4.0.0",
"redux-devtools-extension": "^2.13.5",
"redux-logger": "^3.0.6",
"redux-thunk": "^2.3.0",
"resolve": "1.8.1",
"sass-loader": "7.1.0",
"serve": "^10.0.2",
"style-loader": "0.23.0",
"terser-webpack-plugin": "1.1.0",
"url-loader": "1.1.1",
"webpack": "4.19.1",
"webpack-dev-server": "3.1.9",
"webpack-manifest-plugin": "2.0.4",
"workbox-webpack-plugin": "3.6.2"

}

1 个答案:

答案 0 :(得分:0)

如果您要引用navigateFallback属性来预缓存数据,则以下是有关如何使用它的文档:

sw-precache

  

navigateFallback [String]设置HTML文档以用作后备   在sw-precache缓存中找不到URL。此后备网址需要   通过staticFileGlobsdynamicUrlToDependencies进行缓存   它不起作用。

// via staticFileGlobs
staticFileGlobs: ['/shell.html']
navigateFallback: '/shell.html'

// via dynamicUrlToDependencies
dynamicUrlToDependencies: {
  '/shell': ['/shell.hbs']
},
navigateFallback: '/shell'
     

与执行以下操作的Web应用程序一起使用时,这非常方便   使用History API进行客户端URL路由。它允许任意   客户端生成以映射到后备缓存HTML条目的URL。   理想情况下,此后备条目应作为“应用程序外壳”   能够基于客户端加载适当的资源   请求网址。

     

注意:这不是用来将失败的导航路由到   通用的“离线后备”页面。使用了navigationFallback页面   浏览器是在线还是离线。如果您想实施   “离线后备”,然后使用类似于此example的方法   更合适。

     

默认:“”