Service Worker breaking 301 redirects
<p>I'm using a service worker on a WordPress site and it's messing up the redirections from <a href="https://example.com/page" rel="noreferrer">https://example.com/page</a> to <a href="https://example.com/page/" rel="noreferrer">https://example.com/page/</a>.</p>

<p>After the first load, going to the URL without the trailing slash Blink browsers say "This site can't be reached" and Firefox says "Corrupted content error".</p>

<p>Based on my reading of <a href="https://medium.com/@boopathi/service-workers-gotchas-44bec65eab3f#.hf3r4pbcs" rel="noreferrer">https://medium.com/@boopathi/service-workers-gotchas-44bec65eab3f#.hf3r4pbcs</a> and <a href="https://stackoverflow.com/questions/35420980/how-to-alter-the-headers-of-a-request">How to alter the headers of a Request?</a> I <em>think</em> I have to detect when a response is 3xx and set the redirect mode to manual.</p>

<p>However nothing I've tried based on my research has worked. How do I fix this?</p>

<p>Current service worker file:</p>

<pre><code>var cacheName = 'v14';

var urlsToCache = [
  // list of URLs to precache
];

self.addEventListener('install', event =&gt; {

  function onInstall(event) {
    return caches.open(cacheName)
      .then(cache =&gt; cache.addAll(urlsToCache));
  }

  event.waitUntil(
    onInstall(event)
      .then(() =&gt; self.skipWaiting())
  );

});

self.addEventListener('activate', event =&gt; {

  function onActivate (event) {
    return caches.keys()
      .then(cacheKeys =&gt; {
        var oldCacheKeys = cacheKeys.filter(key =&gt; key.indexOf(cacheName) !== 0);
        var deletePromises = oldCacheKeys.map(oldKey =&gt; caches.delete(oldKey));
        return Promise.all(deletePromises);
      })
  }

  event.waitUntil(
    onActivate(event)
      .then(() =&gt; self.clients.claim ())
  );
});

self.addEventListener('fetch', event =&gt; {

  function onFetch (event) {
    // Let's not interfere with requests for stuff that doesn't need to be cached
    // or could prevent access to admin if it is
    if (event.request.url.match(/wp-admin/) || event.request.url.match(/wp-login/) || event.request.url.match(/preview=true/) || event.request.url.match(/wp-includes/) || event.request.url.match(/plugins/) || event.request.url.match(/google-analytics/) || event.request.url.match(/gravatar\.com/) || event.request.url.match(/login/) || event.request.url.match(/admin/) || event.request.method !== 'GET') {
      return;
    }

    // Determine type of asset
    var request = event.request,
        acceptHeader = request.headers.get('Accept'),
        resourceType = 'static';

    if(acceptHeader.indexOf('text/html') !== -1) {
      resourceType = 'content';
    } else if(acceptHeader.indexOf('image') !== -1) {
      resourceType = 'image';
    }

    // Network first for HTML and images
    if(resourceType === 'content') {
      event.respondWith(fetch(request.url, {
        method: request.method,
        headers: request.headers,
        mode: 'same-origin', // need to set this properly
        credentials: request.credentials,
        redirect: 'manual'
      })
        .then(response =&gt; addToCache(request, response)) // read through caching
        .catch(() =&gt; fetchFromCache(event))
        .catch(() =&gt; offlineResponse(resourceType))
      )
    }

    // Cache first for static assets
    else if(resourceType === 'static' || resourceType === 'image') {
      event.respondWith(fetchFromCache(event)
        .catch(() =&gt; fetch(request))
        .then(response =&gt; addToCache(request, response))
        .catch(() =&gt; offlineResponse(resourceType))
      )
    }
  }

  onFetch(event);

});

function addToCache(request, response) {

  if(response.ok) { // only 200s
    var copy = response.clone(); // Because responses can only be used once
    caches.open(cacheName)
      .then(cache =&gt; {
        cache.put(request, copy);
      });

    return response;
  }

}

function fetchFromCache (event) {

  return caches.match(event.request)
    .then(response =&gt; {
      if(!response) {
        // A synchronous error that will kick off the catch handler
        throw Error('${event.request.url} not found in cache');
      }
    return response;
  });

}

function offlineResponse (resourceType) {

  if(resourceType === 'content') {
    return caches.match('/offline/');
  }
  return undefined;

}
</code></pre>