Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

157
Views
Do the http requests from within a ServiceWorker not come form the scope set when registering?

I am trying to register a service worker like so:

navigator.serviceWorker.register("/static/sw.js", {scope: "/blogs"});

With the goal to make requests to fill the cache during the install step like so:

cache.addAll([
  "/main.css",
  "/logo.png"
]);

the requests would be made to and be saved to the cache as:

<website origin>/blogs/main.css
<website origin>/blogs/logo.png

However, while the scope of the registration seems to be correct (the ServiceWorker in the Application tab of the browser dev tools shows the correct path) the requests made don't include the /blogs part.

In this scenario I cant hardcode the /blogs part of the request to cache.addAll because the value will change depending on deployment stage. The SW wont know the value that will be set to scope, but the window will.

I thought the requests that come from a ServiceWorker will have to go thru the scope set on registration but that doesn't seem to be the case.

If I omit the / in addAll then the requests are made like so:

<website origin>/static/main.css
<website origin>/static/logo.png

since /static is where the Service Worker resides and, currently, I would rather not move it to /blogs if I don't have to.

about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

You can access this scope value from your ServiceWorker by checking the self.registration.scope value.
From there all you have to do is to convert your assets's relative URLs to absolute ones by using this value.

cache.addAll([
  "./main.css",
  "./logo.png"
] // make them relative to the SW's scope
  .map((url) => new URL(url, registration.scope))
);
about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!