question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Fallback .html extension matters to fallback setup?

See original GitHub issue

I’m following the google developer guide to build a PWA with a fallback page.

my SW:

import { registerRoute, NavigationRoute } from 'workbox-routing';
import * as navigationPreload from 'workbox-navigation-preload';
import { NetworkFirst, StaleWhileRevalidate, CacheFirst } from 'workbox-strategies';
// Used for filtering matches based on status code, header, or both
import { CacheableResponsePlugin } from 'workbox-cacheable-response';
// Used to limit entries in cache, remove entries after a certain period of time
import { ExpirationPlugin } from 'workbox-expiration';
import { precacheAndRoute } from 'workbox-precaching';

// Add offline fallback cache when installing
const FALLBACK_CACHE_NAME = 'offline-fallbacks';
// This assumes /404.html is a URL for your self-contained
// (no external images or styles) offline page.
const FALLBACK_HTML_URL = '/404';
// Build fallback asset array
// let offlineCaches = placeholderImage.images.map((img) => `${img.path}`)
// offlineCaches.push(FALLBACK_HTML_URL)
// Populate the cache with the offline HTML page when the
// service worker is installed.
self.addEventListener('install', async (event) => {
    event.waitUntil(
        caches.open(FALLBACK_CACHE_NAME)
        .then((cache) => cache.addAll([FALLBACK_HTML_URL, placeholderImage.src]))
    );
});

const navNetworkfirstHandler = new NetworkFirst({
    cacheName: 'cached-navigations',
})

const navigationHandler = async (params) => {
    try {
        // Attempt a network request.
        return await navNetworkfirstHandler.handle(params);
    }
    catch (error) {
        // If it fails, return the cached HTML.
        return caches.match(FALLBACK_HTML_URL, {
            cacheName: FALLBACK_CACHE_NAME,
        });
    }
};

// Cache page navigations (html) with a Network First strategy
registerRoute(
    new NavigationRoute(navigationHandler)
);

But the fallback page always respond with ERR_INTERNET_CONNECTION.

After changing /404 to /404.html and serving the page with the extension, the issue was gone.

Out of curiosity, I would like to know why URL without extension would not work as a fallback path. Any insights?

PS. Sorry if it’s a bad place to ask questions…

Issue Analytics

  • State:closed
  • Created 3 years ago
  • Comments:6 (2 by maintainers)

github_iconTop GitHub Comments

1reaction
jeffposnickcommented, Mar 16, 2021

Oh, so looking more closely, the issue is that your response was the result of a redirect, and there’s a service worker security restriction that prevents redirected responses from being used to fulfill navigation requests.

I’m assuming /404 redirects to /404/. What I’d suggest doing is to change things so that /404/ is used as the fallback instead:

const FALLBACK_HTML_URL = '/404/';
0reactions
DotIN13commented, Mar 17, 2021

That fully dspell my doubts, thanks a lot for your kind explanation!

Read more comments on GitHub >

github_iconTop Results From Across the Web

fallback - CSS: Cascading Style Sheets - MDN Web Docs
When the range descriptor is specified for a counter style, the fallback style will be used to represent values that fall outside the...
Read more >
HTML fallback images on error
We use the onerror to set the error to null and set the src of the image to a fallback. In our case,...
Read more >
How to fallback to local stylesheet (not script) if CDN fails
So my question is: How do I ensure a stylesheet is loaded locally if a CDN fails ? html · jquery · css...
Read more >
HTML only image fallback
Below an image is embedded in an 'object' tag. If that image is not loaded correctly (e.g. inexistent image) the 'img' inside it...
Read more >
Azure — How To Configure fallback routes for Angular ...
You need to know a lot of things as prerequisites if you want to understand the Azure Static web apps service. First, you...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found