Pricing Blog

Service worker

  • ssssadsadasd-1413264251660927109

    ssssadsadasd

    9 days ago

    So, I have a js file in supabase and I enter the url in the Service Worker filed ([see here](https://editor.nordcraft.com/projects/seno12/branches/start/components/test-worker?canvas-width=800&canvas-height=800&mode=design&rightpanel=attributes&selection=nodes.QjNuJkBABo_p0DypGl4XP&leftpanel=Files)).

    however it throws an error: Uncaught ReferenceError: importScripts is not defined
    at serviceWorker.js?v=85904c8:3:1


    AI tells me to add a script element with attribute content = if ('serviceWorker' in navigator) { window.addEventListener('load', () => { navigator.serviceWorker.register('/serviceWorker.js') .then(registration => { console.log('Service Worker registered successfully.'); }) .catch(error => { console.error('Service Worker registration failed:', error); }); }); }
    but this does not solve the issue.

    anyone knows how to fix this? thanks
  • ssssadsadasd-1413445030064164945

    ssssadsadasd

    8 days ago

    so what I do is:
    1. remove the script element completely
    2. in page -> head assects add tag=script, content=the code above
    3. now it works and it tells me: Service Worker registered successfully:
    4. yet the Uncaught ReferenceError: importScripts is not defined still appears. AI tells me it could be that "development tool or framework is automatically injecting that <script> tag into your HTML", since in the page's html the type=module for the servicer worker appears automatically once you add the Service Worker link.


    AI tells me:

    @Andreas Møller not sure whether this makes sense. should the <script type="module" src="/serviceWorker.js"> be visible in the html automatically? thanks
    1413445029673963530-image.png
  • whitep4nth3r-1413446121979641956

    salma

    8 days ago

    Service workers are only available on paid plans and looks like your project is on a free plan?
  • ssssadsadasd-1413446742791426050

    ssssadsadasd

    8 days ago

    @salma that is just a test project that I added just as an example.
    the image above is from my paid project (where I could make it work)
  • whitep4nth3r-1413446829844070452

    salma

    8 days ago

    ah, difficult to debug without seeing the actual project
  • whitep4nth3r-1413448095299014767

    salma

    8 days ago

    however it does sound like in your service worker you're attempting to use a variable called importScripts without defining it
  • whitep4nth3r-1413448405400813619

    salma

    8 days ago

    yes this is the line in the service worker

    importScripts('https://storage.googleapis.com/workbox-cdn/releases/5.1.2/workbox-sw.js');

    importScripts is a function that is not defined
  • so it looks like this code was taken from somewhere where the importScripts function was defined before it was used
  • ssssadsadasd-1413448602088378488

    ssssadsadasd

    8 days ago

    @salma I just copy pasted it from here https://www.pwabuilder.com/
  • whitep4nth3r-1413448637765390346

    salma

    8 days ago

    if you need that script for the service worker, you could load that as a module via a script tag on the page
  • copying and pasting code without understanding what it does rarely goes well 🙂
  • you'll actually want to probably import that file into the actual service worker, rather than in a script tag actually
  • You can do this stuff natively though I think, without needing the workbox package?
  • ssssadsadasd-1413449034374447174

    ssssadsadasd

    8 days ago

    yeah I know haha I just thought since this is sth standardized it works in any case
  • whitep4nth3r-1413449162011185204

    salma

    8 days ago

    I've never heard of workbox so I'm not sure how standard it is 😅
  • ssssadsadasd-1413449273235869786

    ssssadsadasd

    8 days ago

    I mean the code from pwabuilder for the service worker haha
  • whitep4nth3r-1413449283713241088

    salma

    8 days ago

    oh it's a google thing 😅
  • you could ask the AI how to import that file directly without using the importScripts function
  • ssssadsadasd-1413487832210735140

    ssssadsadasd

    8 days ago

    tried but not sure how to do that
  • ssssadsadasd-1413545404305375353

    ssssadsadasd

    8 days ago

    for some strange reason if I remove the link to the serviceWorker.js the error dissappears and I get Service Worker registered successfully:
  • whitep4nth3r-1413554132752662709

    salma

    8 days ago

    This is very odd. Might need @Erik Beuschau to take a look next week.
  • erikbeus-1414532605562323125

    Erik Beuschau

    5 days ago

    I'm happy to investigate your issues here @ssssadsadasd
    Could I ask you to briefly explain your issue again and provide a link to a page where it's not working as expected? 🤞
    👍1
  • ssssadsadasd-1414543759286468678

    ssssadsadasd

    5 days ago

    thanks @Erik Beuschau just sent you a dm 👍