1

无论预缓存的数据如何,我每次离线时都需要显示一个自定义离线页面。

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

我尝试更改NavigationFallback为,offline.html但每次我在离线时重新加载页面时,它都会显示index.html页面并且不显示offline.html

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

包.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"

}

4

1 回答 1

0

如果您指的是navigateFallback预缓存数据的属性,这里是有关如何使用它的文档:

sw-precache

navigateFallback [String] 设置 HTML 文档以用作在 sw-precache 缓存中找不到的 URL 的后备。此后备 URL 需要通过缓存,staticFileGlobs否则dynamicUrlToDependencies将无法正常工作。

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

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

这在与使用History API执行客户端 URL 路由的 Web 应用程序一起使用时会派上用场。它允许客户端生成的任意 URL 映射到备用缓存的 HTML 条目。理想情况下,此回退条目应用作能够根据请求 URL 在客户端加载适当资源的“应用程序外壳”。

注意:这不打算用于将失败的导航路由到通用的“离线后备”页面。无论浏览器在线还是离线,都使用 navigateFallback 页面。如果要实现“离线回退”,则使用类似于此示例的方法更合适。

默认: ””

于 2018-10-08T07:36:21.077 回答