3

Hi I have simple service worker, but my Range header is not being send even thought i can log it on request object.

self.addEventListener('fetch', async function(event) {
    if (event.request.headers.get("range")) {
      const response = await fetch(event.request.clone());

      return  event.respondWith(this.getPartialResponse(event.request, response));
    }

    return  event.respondWith(fetch(event.request));
}

async getPartialResponse(req, res) {
  const pos = Number(/^bytes\=(\d+)\-$/g.exec(req.headers.get("range"))[1]);
  const ab = await res.arrayBuffer();
  const headers = new Headers(res.headers);

  headers.append("Content-Range", `bytes ${pos}-${ab.byteLength - 1}/${ab.byteLength}`);
  headers.append("Content-Length", ab.byteLength - pos + 1);

  return new Response(ab.slice(pos), {
    status: 206,
    statusText: "Partial Content",
    headers
  });
}

Here you can see request one catched by service worker and the second one send to the api where you can notice the Range header is missing. Why ? My browser: Chrome/59.0.3071.104 enter image description here enter image description here

1 Answer 1

2

caveat, I don't know what a range header is, so bear with me. I know some headers are not available to the service worker for security reasons. Basically you cannot tamper with the request and response. For example you do not have access to Cache-Control in response objects from 3rd party domains, that way you cannot adjust the HTTP caching logic. This might be your issue, but I can't say for sure. Look up rules concerning opaque request & responses.

3
  • as you can see on my screens i am requesting video from the same origin
    – vardius
    Commented Jun 19, 2017 at 21:20
  • Ok, did a little more research on the range header, good thing too, I am about to create a podcast PWA :) I think if you review the demo code in this Chrome project you will find some guidance -> github.com/GoogleChrome/sample-media-pwa They reference a ranged-response library to help.
    – Chris Love
    Commented Jun 20, 2017 at 2:00
  • 1
    Thanks, I will take a closer look at this, so far I can see they create Range request to cache.
    – vardius
    Commented Jun 20, 2017 at 2:12

Not the answer you're looking for? Browse other questions tagged or ask your own question.