Edgio
Edgio

Vue.js

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

Example

System Requirements

Sign up for Edgio

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

Install the Edgio CLI

If you have not already done so, install the Edgio CLI.

With npm:

Bash
1npm i -g @edgio/cli

With yarn:

Bash
1yarn global add @edgio/cli

Create a new Vue.js app

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

Bash
1npm install -g @vue/cli @vue/cli-service-global
2vue create hello-world

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

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

You can verify your app works by running it locally:

Bash
1cd hello-world
2npm run serve

Configuring your Vue.js app for Edgio

Initialize your project

In the root directory of your project run edgio init:

Bash
1edgio init

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

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

Adding Edgio Service Worker

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

Bash
1npm i register-service-worker workbox-webpack-plugin

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

JavaScript
1import { skipWaiting, clientsClaim } from 'workbox-core'
2import { precacheAndRoute } from 'workbox-precaching'
3import { Prefetcher } from '@edgio/prefetch/sw'
4
5skipWaiting()
6clientsClaim()
7precacheAndRoute(self.__WB_MANIFEST || [])
8
9new Prefetcher().route()

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

JavaScript
1/* eslint-disable no-console */
2
3import { register } from 'register-service-worker'
4
5if (process.env.NODE_ENV === 'production') {
6 register(`${process.env.BASE_URL}service-worker.js`, {
7 ready() {
8 console.log(
9 'App is being served from cache by a service worker.\n' +
10 'For more details, visit https://goo.gl/AFskqB',
11 )
12 },
13 registered() {
14 console.log('Service worker has been registered.')
15 },
16 cached() {
17 console.log('Content has been cached for offline use.')
18 },
19 updatefound() {
20 console.log('New content is downloading.')
21 },
22 updated() {
23 console.log('New content is available; please refresh.')
24 },
25 offline() {
26 console.log('No internet connection found. App is running in offline mode.')
27 },
28 error(error) {
29 console.error('Error during service worker registration:', error)
30 },
31 })
32}

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

JavaScript
1import { createApp } from 'vue'
2import App from './App.vue'
3import './registerServiceWorker'
4
5createApp(App).mount('#app')

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

JavaScript
1const { InjectManifest } = require('workbox-webpack-plugin')
2
3const config = {}
4
5if (process.env.NODE_ENV === 'production') {
6 config['configureWebpack'] = {
7 plugins: [
8 new InjectManifest({
9 swSrc: './service-worker.js',
10 }),
11 ],
12 }
13}
14
15module.exports = config

Configure the routes

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

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

JavaScript
1const { Router } = require('@edgio/core/router')
2
3module.exports = new Router()
4 // Prevent search engine bot(s) from indexing
5 // Read more on: https://docs.edg.io/guides/cookbook#blocking-search-engine-crawlers
6 .noIndexPermalink()
7 // Send requests to static assets in the build output folder `dist`
8 .static('dist')
9
10 // Send everything else to the App Shell
11 .fallback(({ appShell }) => {
12 appShell('dist/index.html')
13 })

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 CDN-as-code 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 Edgio

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

Bash
1npm run build

Test your app with the Sites on your local machine by running the following command in your project’s root directory:

Bash
1edgio 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:

Bash
1npm run build

Deploy your app to the Sites by running the following command in your project’s root directory:

Bash
1edgio deploy

Refer to the Deployments 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 Edgio. Refer to the Nuxt guide for more information.