vue.js - 如何缓存其他路线以供离线浏览?

标签 vue.js vue-router progressive-web-apps offline-caching vue-cli-3

我正在设置一个支持离线浏览的渐进式网络应用程序。

我已经为我的主路由('domainsample.com/')设置了离线浏览,即使离线它也会响应 200。

但是当我导航到其他路由('domainsample.com/about')时,我收到No Internet Page 错误。

这是我在 Heroku 中部署的示例,URL:https://pwa-hehe.herokuapp.com

我使用 Vue CLI 3 设置项目,并使用 Node.js 和 Express.js 来运行服务器中的 dist 文件夹。

// server.js
const express = require('express')
const path = require('path')
const history = require('connect-history-api-fallback')

const app = express()

const staticFileMiddleware = express.static(path.join(__dirname + '/dist'))

app.use(staticFileMiddleware)

app.use(history({
    disableDotRule: true,
    verbose: true
}))

app.use(staticFileMiddleware)

app.get('/', function (req, res) {
    res.render(path.join(__dirname + '/dist/'))
})

var server = app.listen(process.env.PORT || 3000, function () {
    var port = server.address().port
    console.log("App now running on port", port)
})
// manifest.json
{
  "name": "pwa-offline",
  "short_name": "pwa-offline",
  "icons": [
    {
      "src": "./img/icons/android-chrome-192x192.png",
      "sizes": "192x192",
      "type": "image/png"
    },
    {
      "src": "./img/icons/android-chrome-512x512.png",
      "sizes": "512x512",
      "type": "image/png"
    }
  ],
  "start_url": "./index.html",
  "display": "standalone",
  "background_color": "#000000",
  "theme_color": "#4DBA87"
}

// service-worker.js

/**
 * Welcome to your Workbox-powered service worker!
 *
 * You'll need to register this file in your web app and you should
 * disable HTTP caching for this file too.
 * 
 *
 * The rest of the code is auto-generated. Please don't update this file
 * directly; instead, make changes to your Workbox build configuration
 * and re-run your build process.
 * 
 */

importScripts("https://storage.googleapis.com/workbox-cdn/releases/3.6.3/workbox-sw.js");

importScripts(
  "/precache-manifest.d3f1ce5d8331bddc555348f44cfba9d8.js"
);

workbox.core.setCacheNameDetails({prefix: "pwa-offline"});

/**
 * The workboxSW.precacheAndRoute() method efficiently caches and responds to
 * requests for URLs in the manifest.
 * 
 */
self.__precacheManifest = [].concat(self.__precacheManifest || []);
workbox.precaching.suppressWarnings();
workbox.precaching.precacheAndRoute(self.__precacheManifest, {});

最佳答案

有一个更简单的解决方案,不涉及使用 InjectManifest

只需将其添加到您的 vue.config.js 文件中即可:

pwa: {
  workboxOptions: {
    navigateFallback: 'index.html'
  }
}

它会自动向您的 Service Worker 添加必要的代码:

workbox.routing.registerNavigationRoute(workbox.precaching.getCacheKeyForURL("index.html"));

我遇到了同样的问题,我在这里找到了解决方案:https://github.com/vuejs/vue-cli/issues/717#issuecomment-382079361

关于vue.js - 如何缓存其他路线以供离线浏览?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58198148/

相关文章:

vue.js bootstrap-vue 动态更改警报变体

javascript - 在 Vue 中显示 JS 对象值

javascript - 使用 PWA 服务 worker 以离线模式保存/提供 Web 服务

ios - Angular 6 中的 window.navigator.standalone 丢失了?

validation - 表单验证的最佳实践

javascript - 是否可以从 VueJS 中的路由获取完整的 url(包括来源)?

vue-router - 在地址栏中隐藏查询参数-VueJS

vue.js - 无法弄清楚 vue-router 是如何工作的

javascript - 如何在加载某些异步数据(在 Vuex 存储中)之前防止任何路由?

service-worker - 如何在我的 PWA 关闭时触发离线后台功能