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.

Puppeteer and ServiceWorkers offline mode

See original GitHub issue

Steps to reproduce

Tell us about your environment:

  • Puppeteer version: 1.3.0
  • Platform / OS version: CentOS 7 x64
  • URLs (if applicable): -
  • Node.js version: 8.11.1

What steps will reproduce the problem?

Please include code that reproduces the issue.

  1. Initialise empty project with puppeteer
  2. Create test file with following content
const puppeteer = require('puppeteer');
(async () => {
  const browser = await puppeteer.launch({ args: ['--no-sandbox']});
  const page = await browser.newPage();
  page.on('console', msg => console.log('PAGE LOG:', msg.text()));
  await page.goto('https://googlechrome.github.io/samples/service-worker/custom-offline-page/', { waitUntil: 'networkidle0' });
  console.log('Setting app offline');
  await page.setOfflineMode(true);
  await page.waitFor(1*2000);
  await page.reload();
  await page.waitFor(1*2000);
  await page.screenshot({path: 'test.png'});
});
  1. Check the resulting screenshot, the page was reloaded but the offline mode was not applied.

What is the expected result?

The resulting screenshot will contain offline page as in classic example https://googlechrome.github.io/samples/service-worker/custom-offline-page/

What happens instead?

Seems like the offline mode is not applied and the page is loaded regularly.

StackOverflow question for reference: Service worker installation in Headless Chrome (via puppeteer)

Issue Analytics

  • State:closed
  • Created 5 years ago
  • Reactions:5
  • Comments:15 (7 by maintainers)

github_iconTop GitHub Comments

8reactions
mdiblasiocommented, Dec 6, 2018

Is there any update on how to run offline tests that also disable the network for service workers?

4reactions
ebidelcommented, Apr 30, 2018

Yea this is weird. I’d expect this example to work as you’ve outlined.

The main page gets cached offline (expected) but it appears the SW isn’t intercepting the network requests, and therefore, not serving the fallback content.

Slightly modified repro.

const puppeteer = require('puppeteer');

const url = 'https://googlechrome.github.io/samples/service-worker/custom-offline-page/';

(async() => {
const browser = await puppeteer.launch({
  headless: false,
});

const page = await browser.newPage();
page.on('console', msg => console.log('PAGE LOG:', msg.text()));
await page.goto(url, {waitUntil: 'networkidle0'});
await page.evaluate('navigator.serviceWorker.ready');

console.log('Going offline');
await page.setOfflineMode(true);

 // Does === true for the main page but the fallback content isn't being served.
page.on('response', r => console.log(r.fromServiceWorker()));

await page.reload({waitUntil: 'networkidle0'});

// await browser.close();
})();
Read more comments on GitHub >

github_iconTop Results From Across the Web

Service worker not activated in Puppeteer for localhost domain
The service worker I'm talking about here is from a chrome extension (background script). Because it is the background script from a chrome ......
Read more >
Making PWAs work offline with Service workers
In this article we took a simple look at how you can make your PWA work offline with service workers. Be sure to...
Read more >
What is a service worker ? | Browser - Data and Co
https://github.com/puppeteer/examples/blob/master/verify_sw_caching.js - Verify all the resources you expect are being cached by a service worker for offline.
Read more >
Service Worker - ITNEXT
Read writing about Service Worker in ITNEXT. ... Go to the profile of Kjartan Rekdal Müller ... Interacting with Service Workers using Puppeteer....
Read more >
Use Service Workers to manage network requests and push ...
You can use the install event to pre-cache any important and long-lived files, such as CSS files, JavaScript files, logo images, offline pages, ......
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