pwa-module icon indicating copy to clipboard operation
pwa-module copied to clipboard

offlinePage not route offline requests to the specified path

Open beneschjozsef opened this issue 5 years ago • 10 comments

I tried with a new build after set up this in nuxt.config.js:

modules: [
    [
      "@nuxtjs/pwa",
      {
        manifest: {
          start_url: "/",
        },
        workbox: {
          offlinePage: "/offline.html",
        },
      },
    ],
  ],

I put the offline html in the static directory. It is working properly offline, but not redirect to the offline.html when the page is offline. Please help.

beneschjozsef avatar Dec 08 '20 10:12 beneschjozsef

Same issue here, offline.html is getting precached but offline routes are not getting redirected to /offline.html

nuxt.config.js

...
workbox: {
	dev: process.env.NODE_ENV === 'development',
	cleanupOutdatedCaches: true,
	offlinePage: '/offline.html',
	offlineAssets: ['/images/offline.png'],
	runtimeCaching: [
		{
			urlPattern: '/images/.*',
			handler: 'cacheFirst',
			strategyOptions: {
				cacheName: 'images',
				cacheExpiration: {
					maxEntries: 20,
					maxAgeSeconds: 3600,
				},
			},
		},
		{
			urlPattern: '/api/.*',
			handler: 'networkOnly',
		},
	],
}
...

harish0507 avatar Mar 09 '21 11:03 harish0507

Same here, any advice anyone?

jezmck avatar Apr 19 '21 10:04 jezmck

Does anyone have any recommendations on how to tackle this?

beatriznbarroso avatar Apr 26 '21 16:04 beatriznbarroso

Same issue using "@nuxtjs/pwa": "^3.3.4" and "nuxt": "^2.15.3".

nuxt.config.js

pwa: {
    manifest: {
      short_name: '...',
      name: '....',
      description: '....',
      lang: 'en',
      background_color: '#658361',
      theme_color: '#658361',
      start_url: "/?standalone=true",
    },
    workbox: {
      dev: true,
      enabled: true,
      offlineStrategy: 'StaleWhileRevalidate',
      offlinePage: '/offline.html',
      offlineAssets: [
        '/icon.png',
        '/favicon.png',
      ],
      clientsClaim: false
    }
  },

tdxius avatar Jun 21 '21 20:06 tdxius

Any progress on that? I think this package isn't maintained any more?

zatorck avatar Aug 22 '21 21:08 zatorck

Hi,

Same here. I had issues with making the module precache all routes (#306) so I thought well I'm gonna use the offline page instead. But that doesn't work either :(

cdefy avatar Nov 04 '21 06:11 cdefy

Unfortunately I can confirm this issue.

klickparkdominik avatar May 06 '22 11:05 klickparkdominik

any one solve this?

honarmandpooria avatar May 12 '22 09:05 honarmandpooria

I found workaround solution for this:

Adding custom service worker:

// nuxt.config.js
pwa: { 
  workbox: {
    importScripts: ['/offline-sw.js'],
  },
}

Then add offline service worker into 'static' folder

// offline-sw.js
/*
Copyright 2015, 2019 Google Inc. All Rights Reserved.
 Licensed under the Apache License, Version 2.0 (the "License");
 you may not use this file except in compliance with the License.
 You may obtain a copy of the License at
 http://www.apache.org/licenses/LICENSE-2.0
 Unless required by applicable law or agreed to in writing, software
 distributed under the License is distributed on an "AS IS" BASIS,
 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 See the License for the specific language governing permissions and
 limitations under the License.
*/

// Incrementing OFFLINE_VERSION will kick off the install event and force
// previously cached resources to be updated from the network.
const OFFLINE_VERSION = 1
const CACHE_NAME = 'offline'
// Customize this with a different URL if needed.
const OFFLINE_URL = 'offline.html'

self.addEventListener('install', (event) => {
  event.waitUntil(
    (async () => {
      const cache = await caches.open(CACHE_NAME)
      // Setting {cache: 'reload'} in the new request will ensure that the response
      // isn't fulfilled from the HTTP cache; i.e., it will be from the network.
      await cache.add(new Request(OFFLINE_URL, { cache: 'reload' }))
    })()
  )
})

self.addEventListener('activate', (event) => {
  event.waitUntil(
    (async () => {
      // Enable navigation preload if it's supported.
      // See https://developers.google.com/web/updates/2017/02/navigation-preload
      if ('navigationPreload' in self.registration) {
        await self.registration.navigationPreload.enable()
      }
    })()
  )

  // Tell the active service worker to take control of the page immediately.
  self.clients.claim()
})

self.addEventListener('fetch', (event) => {
  // We only want to call event.respondWith() if this is a navigation request
  // for an HTML page.
  if (event.request.mode === 'navigate') {
    event.respondWith(
      (async () => {
        try {
          // First, try to use the navigation preload response if it's supported.
          const preloadResponse = await event.preloadResponse
          if (preloadResponse) {
            return preloadResponse
          }

          const networkResponse = await fetch(event.request)
          return networkResponse
        } catch (error) {
          // catch is only triggered if an exception is thrown, which is likely
          // due to a network error.
          // If fetch() returns a valid HTTP response with a response code in
          // the 4xx or 5xx range, the catch() will NOT be called.
          console.log('Fetch failed; returning offline page instead.', error)

          const cache = await caches.open(CACHE_NAME)
          const cachedResponse = await cache.match(OFFLINE_URL)
          return cachedResponse
        }
      })()
    )
  }

  // If our if() condition is false, then this fetch handler won't intercept the
  // request. If there are any other fetch handlers registered, they will get a
  // chance to call event.respondWith(). If no fetch handlers call
  // event.respondWith(), the request will be handled by the browser as if there
  // were no service worker involvement.
})

That worked for me :)

artrayd avatar May 29 '22 09:05 artrayd

+1

rttmax avatar Sep 28 '22 13:09 rttmax