sveltejs / sapper

The next small thing in web development, powered by Svelte
https://sapper.svelte.dev
MIT License
7k stars 434 forks source link

Service worker files to cache don't include base path #1692

Closed finnbear closed 3 years ago

finnbear commented 3 years ago

Describe the bug The paths passed to the service worker for caching do not include the basepath

Logs Here is the compiled service worker for my project, despite the fact that I specified a two-directory-deep base path following the instructions in the documentation. As you can see, many of the paths in the service worker start with /client rather than /my/basepath/client.

!function(){"use strict";const e=1610090779060,t=`cache${e}`,c=["/client/client.4a48bc03.js","/client/inject_styles.5607aec6.js","/client/index.a638974d.js","/client/integer.23b05fd4.js","/client/Select.f63838d9.js","/client/Slider.cdb985c1.js","/client/matrix.4cf47436.js","/client/cache.f20914d9.js","/client/float.8d03a59d.js","/client/heap.50efc23e.js"].concat(["/service-worker-index.html","/favicon.png","/global.css","/manifest.json"]),n=new Set(c);self.addEventListener("install",(e=>{e.waitUntil(caches.open(t).then((e=>e.addAll(c))).then((()=>{self.skipWaiting()})))})),self.addEventListener("activate",(e=>{e.waitUntil(caches.keys().then((async e=>{for(const c of e)c!==t&&await caches.delete(c);self.clients.claim()})))})),self.addEventListener("fetch",(t=>{if("GET"!==t.request.method||t.request.headers.has("range"))return;const c=new URL(t.request.url),s=c.protocol.startsWith("http"),a=c.hostname===self.location.hostname&&c.port!==self.location.port,i=c.host===self.location.host&&n.has(c.pathname),l="only-if-cached"===t.request.cache&&!i;!s||a||l||t.respondWith((async()=>i&&await caches.match(t.request)||async function(t){const c=await caches.open(`offline${e}`);try{const e=await fetch(t);return c.put(t,e.clone()),e}catch(e){const n=await c.match(t);if(n)return n;throw e}}(t.request))())}))}();

To Reproduce

  1. Create a sapper app
  2. Add a basepath, following the documentations instructions to add it to both the server and the argument to sapper export
  3. Load the site
  4. Look for failed caching requests to incorrect urls

Expected behavior Service worker should be passed paths that conform to the basepath.

Severity My site works fine, but nothing is cached properly by the service worker.

benmccann commented 3 years ago

This was also reported in https://github.com/sveltejs/sapper/issues/1538 and https://github.com/sveltejs/sapper/issues/1441

I'm not quite sure what the best fix is. See the description in https://github.com/sveltejs/sapper/issues/1545#issue-705849178. I'm leaning towards prepending the base path. Though I'm not actively working on Sapper at the moment. But if you'd like to send a PR I'll review it

finnbear commented 3 years ago

Those do look like the same issue. I guess we can close this as duplicate.