Vue.js

This guide shows you how to deploy a Vue.js application on Layer0.

Example

Sign up for Layer0

Deploying requires an account on Layer0. Sign up here for free.

Install the Layer0 CLI

If you have not already done so, install the Layer0 CLI

Terminal
npm i -g @layer0/cli

System Requirements

Create a new Vue.js app

If you don’t already have a Vue.js app, create one by using the Vue CLI:

Terminal
npm install -g @vue/cli @vue/cli-service-global
vue create hello-world

When running vue create select Vue 2 or Vue 3 as a preset,

Terminal
Vue CLI v4.5.13
? Please pick a preset: (Use arrow keys)
❯ Default ([Vue 2] babel, eslint)
  Default (Vue 3) ([Vue 3] babel, eslint)
  Manually select features

You can verify your app works by running it locally:

Terminal
cd hello-world
npm run serve

Configuring your Vue.js app for Layer0

Initialize your project

In the root directory of your project run 0 init:

Terminal
0 init

This will automatically update your package.json and add all of the required Layer0 dependencies and files to your project. These include:

  • The @layer0/core package - Allows you to declare routes and deploy your application on Layer0
  • The @layer0/prefetch package - Allows you to configure a service worker to prefetch and cache pages to improve browsing speed
  • layer0.config.js - A configuration file for Layer0
  • routes.js - A default routes file that sends all requests to Vue.js.

Adding Layer0 Service Worker

To add service worker to your Vue app, run the following in the root folder of your project:

Terminal
npm i register-service-worker workbox-webpack-plugin

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

JavaScript
import { skipWaiting, clientsClaim } from 'workbox-core'
import { precacheAndRoute } from 'workbox-precaching'
import { Prefetcher } from '@layer0/prefetch/sw'

skipWaiting()
clientsClaim()
precacheAndRoute(self.__WB_MANIFEST || [])

new Prefetcher().route()

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

JavaScript
/* 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)
    },
  })
}

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

Diff
import { createApp } from 'vue'
import App from './App.vue'
+ import './registerServiceWorker'

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

Now, create vue.config.js at the root of your project with the following config:

JavaScript
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

Configure the routes

Next you’ll need to configure Layer0 routing in the routes.js file.

For the Vue hello-world template, replace the routes.js file that was created during 0 init with the following:

JavaScript
const { Router } = require('@layer0/core/router')

module.exports = new Router()
  // Send requests to static assets in the build output folder `dist`
  .static('dist')

  // Send everything else to the App Shell
  .fallback(({ appShell }) => {
    appShell('dist/index.html')
  })

The example above assumes you’re using Vue as a single page app. It routes the static assets (JavaScript, CSS, and Images) in the production build folder dist and maps all other requests to the app shell in dist/index.html.

Refer to the Routing guide for the full syntax of the routes.js file and how to configure it for your use case.

Run the Vue.js app locally on Layer0

Create a production build of your app by running the following in your project’s root directory:

Terminal
npm run build

Run Layer0 on your local machine:

Terminal
0 dev

Load the site: http://127.0.0.1:3000 !

Deploying

Create a production build of your app by running the following in your project’s root directory:

Terminal
npm run build

Next, deploy the build to Layer0 by running the 0 deploy command:

Terminal
0 deploy

Refer to the Deploying guide for more information on the deploy command and its options.

Server Side Rendering

For server side rendered Vue.js apps we recommend using the Nuxt.js framework which is supported on Layer0. Refer to the Nuxt guide for more information.