Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

344
Visualizações
How to preload js workers and wasm scripts for offline use (pwa)?

I would like to force the browser to preload a JavaScript worker as well as a WebAssembly script. When loaded, I have a ServiceWorker that puts these scripts in the CacheStorage.

For the images, I use the following <link> tag and this works well:

<link rel="prefetch" href="/img/foo.png" as="image" type="image/png" importance="low" />

So I tried the same things for my wasm and js scripts:

<link rel="prefetch" href="/wasm/bar.wasm" type="application/wasm" importance="low" />
<link rel="prefetch" href="/js/baz.worker.js" as="script" type="text/javascript" importance="low" />

However, it looks like the Browser (Chrome) does not load these scripts with the prefetch rel. So I tried to use rel="preload".

But I don't know what to fill for the as property for the wasm file and for the js worker file, I get the following warning: The resource http://localhost:3000/wasm/bar.wasm was preloaded using link preload but not used within a few seconds from the window's load event. Please make sure it has an appropriate `as` value and it is preloaded intentionally..

If the user executes an action that requires these files, the browser loads them well and then we can use them when offline.

These scripts are not needed before the load event. I could send a fetch request when the browser emits the load event? But I think it is better to let the browser manages such things with a link tag? In my case, it is quite likely that the user will need this script (90%+), so a preload would make sense, but theoretically, I want to make a prefetch of the content only.

How would you recommend preloading these files for offline use (pwa)?

about 4 years ago · Juan Pablo Isaza
1 Respostas
Responde à pergunta

0

It seems that sending an HTTP request with the fetch API puts the script in CacheStorage just fine.

 /** * Preload resources for offline pwa use */ window.addEventListener('load', () => { fetch('/js/baz.worker.js'); fetch('/wasm/bar.wasm'); });

Sending these requests after the load event does not affect the page load time.

To maintain the priority hint, it is possible to set the importance parameter to a low level, as shown below:

 fetch('/wasm/bar.wasm', { importance: "low" });

(warning: limited browser support when this answer was posted)

about 4 years ago · Juan Pablo Isaza Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda