Closed Bug 1897200 Opened 1 year ago Closed 1 year ago

Service Worker fetch event request.body is undefined

Categories

(Core :: DOM: Service Workers, defect)

Firefox 125
defect

Tracking

()

RESOLVED DUPLICATE of bug 1387483

People

(Reporter: jcubic, Unassigned)

Details

User Agent: Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/124.0.0.0 Safari/537.36

Steps to reproduce:

I run this code:

sw.js

self.addEventListener('fetch', async (event) => {
  const req = event.request;
  const url = new URL(req.url);
  if (url.pathname === '/api/login/' && req.method === 'POST') {
    const headers = {
      "Content-Type": "text/plain"
    };
    console.log({body: req.body});
    event.respondWith(new Response('Hello!', { headers }));
  }
});

self.addEventListener('activate', (event) => {
  event.waitUntil(clients.claim());
});

and index.html:

<!DOCTYPE html>
<html>
<body>
<button id="button">run</button>
<script>
if ('serviceWorker' in navigator) {
    const scope = location.pathname.replace(/\/[^\/]+$/, '/');
    navigator.serviceWorker.register('sw.js', { scope })
             .then(function(reg) {
                 reg.addEventListener('updatefound', function() {
                     const installingWorker = reg.installing;
                     console.log('A new service worker is being installed:',
                                 installingWorker);
                 });
                 // registration worked
                 console.log('Registration succeeded. Scope is ' + reg.scope);
             }).catch(function(error) {
                 // registration failed
                 console.log('Registration failed with ' + error);
             });
}
button.addEventListener('click', async () => {
    console.log(await login('user', 'pass'));
});

function login(username, password) {
    return fetch('/api/login/', {
        method: 'post',
        headers: {
            'Accept': 'application/json',
            'Content-Type': 'application/json'
        },
        credentials: 'include',
        body: JSON.stringify({
            username,
            password
        })
    }).then(res => res.text());
}
</script>
</body>

Actual results:

When clicking the button and inspecting the Service Worker in remote debugger. It logs:

{body: undefined}

Expected results:

I expect to see ReadableStream like on MDN:

https://developer.mozilla.org/en-US/docs/Web/API/Request

https://developer.mozilla.org/en-US/docs/Web/API/FetchEvent

This is how it works in Google Chrome and this is how it's documented on MDN.

Let me know if you want me to upload the files to the server.

I just inspected the req object and it has a field bodyUsed: false

The Bugbug bot thinks this bug should belong to the 'Core::DOM: Service Workers' component, and is moving the bug to that component. Please correct in case you think the bot is wrong.

Component: Untriaged → DOM: Service Workers
Product: Firefox → Core

bodyUsed is only to indicate that the stream was already read. But in Firefox the stream is not present.

Status: UNCONFIRMED → RESOLVED
Closed: 1 year ago
Duplicate of bug: 1387483
Resolution: --- → DUPLICATE
You need to log in before you can comment on or make changes to this bug.