【发布时间】:2023-03-09 18:30:30
【问题描述】:
我正在设置一个支持离线浏览的 Progressive Web App。
我已经为我的主要路由('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, {});
【问题讨论】:
-
你在哪里定义的离线策略?我只看到你的网络清单,但没有看到 Service Worker 的代码。
-
嗨,编辑了这个问题,我只使用 Vue CLI 3 自动生成的 service-worker.js,我相信我应该自己做?
-
请问是否有任何答案有效,或者您是否找到了解决方案?
标签: vue.js vue-router progressive-web-apps offline-caching vue-cli-3