Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I cache other routes for offline browsing?

I am setting up a Progressive Web App supporting offline browsing.

I have already set up offline browsing for my main route ('domainsample.com/') and it responds 200 even if offline.

But when I navigate to other routes ('domainsample.com/about') I receive a No Internet Page error.

Here is a sample I deployed in Heroku the URL: https://pwa-hehe.herokuapp.com

I used Vue CLI 3 to set up the project and Node.js and Express.js to run my dist folder in the server.

// 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, {});
like image 585
frost kazuma Avatar asked Oct 02 '19 08:10

frost kazuma


People also ask

Does Google Maps cache maps for offline use?

While Google doesn't make caching maps for offline use a very obvious option, it is there and we can show you how to use it. Launch the Google Maps app from the Home screen of your iPhone or iPad.

How do I enable offline caching on OK maps?

Now in the Search bar type in Ok Maps and hit Search. You'll see a screen with the Google icon showing that it's processing the area for offline caching. If you receive an error message that the area couldn't be cached because it's too large, try zooming in to a smaller area.

How to use offline files in Windows 7 to cache Offline Files?

How to Use Offline Files in Windows to Cache Your Networked Files Offline 1 Setting Up Offline Files. If you’re new to networking, be sure to check out our guide to networking Windows 7 with XP or Vista, or the guide on how to ... 2 Manually Initiating a Sync. ... 3 Scheduling Sync Jobs. ... 4 Resolving Conflicts. ... 5 Adding Some Security. ...

How do I save a route offline?

To save a route offline, select the menu item "Download" in the lower bar on the detail page of the route. In addition to individual routes, you can also save entire sections of the map offline.


2 Answers

There's a simpler solution that doesn't involve using InjectManifest.

Simply add this to your vue.config.js file:

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

It will automatically add the necessary code to your service worker:

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

I had the same problem and I found the solution here: https://github.com/vuejs/vue-cli/issues/717#issuecomment-382079361

like image 192
clem Avatar answered Oct 01 '22 20:10

clem


To anyone who is also using the vue/cli-pligin-pwa and vue-cli 3, I have solved this problem.

The vue/cli-pligin-pwa does its job in caching your js files for offline use of your web app.

but since you're using a Single Page App, you'll have to set a some kind of fallback.

The request for the page e.g.(sample.com/about) will be a navigation request and it will serve the cached page for /index.html

Source: https://developers.google.com/web/tools/workbox/modules/workbox-routing#how_to_register_a_navigation_route

So what I did is, I made a custom service worker by entering this line of code in my vue.config.js file

// vue.config.js

module.exports = {
    pwa: {
        // configure the workbox plugin
        workboxPluginMode: 'InjectManifest',
        workboxOptions: {
            swSrc: 'public/service-worker.js'
        }
    }
}

and now, create a service-worker.js in your public folder directory, and write up the the generated line of codes from vue/cli-pligin-pwa and add the line : workbox.routing.registerNavigationRoute('/index.html');

//service-worker.js

self.__precacheManifest = [].concat(self.__precacheManifest || []);
workbox.precaching.suppressWarnings();
workbox.precaching.precacheAndRoute(self.__precacheManifest, {});

workbox.routing.registerNavigationRoute('/index.html');

// install new service worker when ok, then reload page.
self.addEventListener("message", msg => {
    if (msg.data.action == 'skipWaiting') {
        self.skipWaiting()
    }
})
like image 35
frost kazuma Avatar answered Oct 01 '22 21:10

frost kazuma