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.

ServiceWorker doesn't catch routes in Firefox only

See original GitHub issue

Describe the bug

MSW properly mock routes in Chrome and Safari, but doesn’t in Firefox.

Environment

  • msw: 0.19.3
  • nodejs: 12.14.0
  • npm: 6.14.2
  • firefox: 77.0.1

To Reproduce

Here’s my mocks.js file:

import { setupWorker, rest } from 'msw';

const worker = setupWorker(
  rest.get('http://localhost:3000/api/user/:id/', (req, res, ctx) => {
    const { id } = req.params;

    return res(
      ctx.status(202, 'Mocked'),
      ctx.json({
        id: id,
      })
    );
  })
);

worker.start();

The request is made with Axios within a Vue.js generated with Vue CLI and using TypeScript. It basically looks like this:

this.axios.defaults.baseURL = 'http://localhost:3000/api';
this.axios.defaults.headers.common['Accept'] = 'application/json';
// ...
this.axios.get(`/user/${id}`);

I tried to put some logs in the ServiceWorker and it seems to not catch this request at all while seeing others such as assets.

I’m testing in HTTP but I have checked the parameter to enable SW in HTTP in the devtools. I have also cleared the SW after every change in about:debugging to be sure it was up-to-date.

Expected behavior

The request should be mocked in Firefox just like it does in Chrome and Safari.

Screenshots

Firefox console:

Capture d’écran 2020-06-17 à 10 16 22

Chrome console:

Capture d’écran 2020-06-17 à 10 16 42

Issue Analytics

  • State:closed
  • Created 3 years ago
  • Reactions:3
  • Comments:10 (5 by maintainers)

github_iconTop GitHub Comments

4reactions
LeBenLeBencommented, Jun 17, 2020

I was able to make Firefox mock the request by running fetch('http://localhost:3000/api/user/1') directly in the browser console.

I checked both requests to find differences, and basically the one that fails is an XMLHttpRequest whereas the one that works is done with Fetch.

This leaded me to find that XHR within SW are not supported in Firefox apparently, which match with my logs not outputting the XHR request at all from the SW.

I guess we’ll have to either use Fetch or Chrome then 😅

2reactions
LeBenLeBencommented, Jul 6, 2021

@Thane2376 It’s a browser limitation, no workaround is possible, you should either use Fetch or another browser.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Using Service Workers - Web APIs | MDN
In Firefox, Service Worker APIs are hidden and cannot be used when the user is in private browsing mode, or when history is...
Read more >
1663125 - Service Worker registrations are not loaded at startup
Bug 1663125 - Fix registered service workers missing after restart and ... The problem is not only that after closing Firefox the Service...
Read more >
Pinned tabs that use ServiceWorkers and do not skipWaiting ...
The bug impacted page loads that happened when the target process type wasn't already running, which would be the case for non-fission-enabled Firefox...
Read more >
Off-origin requests are not handled by service worker if 3rd ...
This doesn't make sense to me. So this patch: 1. Blocks all service worker interception if the policy is REJECT 2. Blocks 3rd...
Read more >
Show all registered service workers in about:debugging
1) Clicking on "Debug" should open a toolbox on the one live service worker. 2) A service worker is finishing up, and a...
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