最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Workbox - SPA - fallback to index.html - Stack Overflow

programmeradmin2浏览0评论

I'm using workbox with webpack to generate a service worker.

With the following code in webpack.config.js:

new WorkboxPlugin.InjectManifest({
  swSrc: "./src/sw.js"
}),

a service worker is generated nicely.

In ./src/sw.js, I have:

workbox.precaching.precacheAndRoute(self.__precacheManifest || []);

And all of my assets are precached nicely.

However, I have a single page application and I noticed that when refreshing the page while offline from a non-homepage route, the service worker doesn't respond. For example, when refreshing /page1 while offline doesn't work, but refreshing / does work.

How can I configure workbox to use a runtime strategy that uses /index.html as a fallback for HTML requests?

Note

Doing something like this:

new WorkboxPlugin.InjectManifest({
  swSrc: "./src/sw.js",
  navigationFallback: "/index.html"
})

does not work since navigationFallback is not a valid option in it's above usage.

{ message: '"navigationFallback" is not a supported parameter.'

I'm using workbox with webpack to generate a service worker.

With the following code in webpack.config.js:

new WorkboxPlugin.InjectManifest({
  swSrc: "./src/sw.js"
}),

a service worker is generated nicely.

In ./src/sw.js, I have:

workbox.precaching.precacheAndRoute(self.__precacheManifest || []);

And all of my assets are precached nicely.

However, I have a single page application and I noticed that when refreshing the page while offline from a non-homepage route, the service worker doesn't respond. For example, when refreshing /page1 while offline doesn't work, but refreshing / does work.

How can I configure workbox to use a runtime strategy that uses /index.html as a fallback for HTML requests?

Note

Doing something like this:

new WorkboxPlugin.InjectManifest({
  swSrc: "./src/sw.js",
  navigationFallback: "/index.html"
})

does not work since navigationFallback is not a valid option in it's above usage.

{ message: '"navigationFallback" is not a supported parameter.'
Share Improve this question edited May 26, 2018 at 3:19 Raphael Rafatpanah asked May 26, 2018 at 2:57 Raphael RafatpanahRaphael Rafatpanah 20k28 gold badges104 silver badges174 bronze badges 1
  • register a route in the version of SW that u inject the manifest into. the regex can match the html's u wish to cover and the target can be 'index.html' . review docs for WB.registerRoute() – Robert Rowntree Commented May 26, 2018 at 3:38
Add a comment  | 

2 Answers 2

Reset to default 14

Luckily, workbox has made this an easy solve.

If your site is a single page app, you can use a NavigationRoute to return a specific response for all navigation requests.

workbox.routing.registerNavigationRoute('/single-page-app.html');

In my case:

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

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

For workbox v4 or below, use workbox.routing.registerNavigationRoute (see another answer).

The recipe is changed in v5:

import {precacheAndRoute, createHandlerBoundToURL} from 'workbox-precaching'
import {NavigationRoute, registerRoute} from 'workbox-routing'


precacheAndRoute(self.__WB_MANIFEST)
registerRoute(new NavigationRoute(createHandlerBoundToURL('/index.html')))

Note: self.__WB_MANIFEST (renamed from self.__precacheManifest) is provided by WorkboxPlugin.InjectManifest.

发布评论

评论列表(0)

  1. 暂无评论