---
title: Enabling Service Worker with Vue 2 and Vue 3
description: Step-by-step setup for adding a Service Worker to Vue 2 and Vue 3 apps created with the Vue CLI, worked out by testing different configurations.
url: https://rishi.app/blog/enabling-service-worker-with-vue-2-and-vue-3
author: Rishi Raj Jain
published_at: 2021-08-09T21:15:04.692Z
updated_at: 2026-10-14T22:26:37.602Z
---

# Enabling Service Worker with Vue 2 and Vue 3

While Vue users [await release of Workbox 5](https://github.com/vuejs/vue-cli/issues/5119), I crawled the entire internet for solutions to integrating Service Worker with Vue 2 and Vue 3. By actually trying different configurations, I've finally laid out the exact steps for you to rightly configure Service Worker with your Vue app.

1. To add service worker (app created with **vue create project-name**), run the following in your project:

```bash
npm install register-service-worker workbox-webpack-plugin
```

2. Create **service-worker.js** at the root of your project with the following:

```js
// Insert your service worker code here
```

3. To register the service worker, first create **registerServiceWorker.js** in the **src** folder:

```js
/* eslint-disable no-console */
import { register } from 'register-service-worker'

if (process.env.NODE_ENV === 'production') {
  register(`${process.env.BASE_URL}service-worker.js`, {
    ready () {
      console.log(
        'App is being served from cache by a service worker.\n' +
        'For more details, visit https://goo.gl/AFskqB'
      )
    },
    registered () {
      console.log('Service worker has been registered.')
    },
    cached () {
      console.log('Content has been cached for offline use.')
    },
    updatefound () {
      console.log('New content is downloading.')
    },
    updated () {
      console.log('New content is available; please refresh.')
    },
    offline () {
      console.log('No internet connection found. App is running in offline mode.')
    },
    error (error) {
      console.error('Error during service worker registration:', error)
    }
  })
}
```

4. and to include the service worker in the app, edit **main.js** (in the **src** folder) as follows:

```js
import App from './App.vue'
import { createApp } from 'vue'

import './registerServiceWorker'

createApp(App).mount('#app')
```

5. Now, create **vue.config.js** at the root of your project with the following config:
```js
const { InjectManifest } = require('workbox-webpack-plugin')

const config = {}

if (process.env.NODE_ENV === 'production') {
  config['configureWebpack'] = {
    plugins: [
      new InjectManifest({
        swSrc: './service-worker.js',
      }),
    ],
  }
}

module.exports = config
```

You're good to go ✨, now create a build of your app (**npm run build**) and celebrate! 🎉

---

This is the Markdown version of https://rishi.app/blog/enabling-service-worker-with-vue-2-and-vue-3. Every page on this site is listed with a summary at https://rishi.app/sitemap.md.
