Movatterモバイル変換


[0]ホーム

URL:


Skip to content
DEV Community
Log in Create account

DEV Community

Julien Calixte
Julien Calixte

Posted on • Edited on

     

Easy Web Worker integration in VueJS

The main thread

Web workers are super handy to make heavy computation and not blocking the main thread. So the user doesn't have his webpage blocked by too much JavaScript working behind.

Example in VueJS

We can see how to use them easily in VueJS. Start by installing comlink-loader. Wait, what is Comlink? And comlink-loader?

Comlink is a library that simplify the way we call web workers, it will be as simple as calling an async function in our code. This lib was created bySurma.

comlink-loader is simply the webpack loader that we will use to tranform files*.worker.js into web workers.

So here we go:

yarn add-D comlink-loader
Enter fullscreen modeExit fullscreen mode

Now, here's the trick to add the loader in your vuejs configuration. It is explainedhere.

module.exports={configureWebpack:(config)=>{config.module.rules=[{test:/\.worker\.js$/i,use:[{loader:'comlink-loader',options:{singleton:true}}]},...config.module.rules]}}
Enter fullscreen modeExit fullscreen mode

Finally we can use it this way:

  1. Create a file that ends with.worker.js and export async functions,
// utils.worker.jsexportconsthelloWorld=async(params)=>{// heavy computing goes here}
Enter fullscreen modeExit fullscreen mode
  1. Import them in vue files.
import{helloWorld}from'../workers/utils.worker'exportdefault{name:'HelloWorld',data:()=>({msg:''}),asyncmounted(){this.msg=awaithelloWorld()}}
Enter fullscreen modeExit fullscreen mode

Remember, it will always be async methods, so make sure to await them.

Set and done!

You can have a look at the quick example I've made to illustrate how web workers can impact user experience here:vue-worker-example.netlify.app. See if the button is clickable when the page is computing with and without background tasks.

Thanks for reading!

Top comments(4)

Subscribe
pic
Create template

Templates let you quickly answer FAQs or store snippets for re-use.

Dismiss
CollapseExpand
 
vicentedl profile image
vicente-dl
  • Joined

Great article that helped me a lot! one question though, how would you make a message back to the main thread? I have been trying to do MyWorker.onmessage but it gives me an error. Return would not be suitable for my app.

Thanks!

CollapseExpand
 
jcalixte profile image
Julien Calixte
Engineering manager - frontend & mobile developper.Love building offline first PWAs.🇧🇷🇫🇷🇪🇺
• Edited on• Edited

Hi Vicente! Thank you!
Do you want to receive a value from your service worker in you main thread? I think comlink hides the onmessage thing so you can return a value from a method in the web worker. Take a look at thisexample 🙂
Just remember, methods in web worker will always return a promise.

CollapseExpand
 
roynes profile image
Saitama
Normal Dev?
  • Joined

Hello!

Will this work without webpack? By just using the default mode?

CollapseExpand
 
jcalixte profile image
Julien Calixte
Engineering manager - frontend & mobile developper.Love building offline first PWAs.🇧🇷🇫🇷🇪🇺

Hi!
I didn't tested but I guess it'll break after webpack bundles the app as the imported filename will not have the same name. I may be wrong

Are you sure you want to hide this comment? It will become hidden in your post, but will still be visible via the comment'spermalink.

For further actions, you may consider blocking this person and/orreporting abuse

Engineering manager - frontend & mobile developper.Love building offline first PWAs.🇧🇷🇫🇷🇪🇺
  • Location
    Paris
  • Work
    BAM
  • Joined

More fromJulien Calixte

DEV Community

We're a place where coders share, stay up-to-date and grow their careers.

Log in Create account

[8]ページ先頭

©2009-2025 Movatter.jp