Skip to main content
Vue.js for Beginners to Advanced
CHAPTER 29 Beginner

Deploying Vue Applications

Updated: May 18, 2026
5 min read

# CHAPTER 29

Deploying Vue Applications

1. Chapter Introduction

Building a Vue app is half the work — deploying it so the world can access it completes the journey. Vite makes production builds optimized by default. Modern platforms like Vercel and Netlify deploy in minutes from GitHub.

2. Learning Objectives

  • Build Vue apps for production with Vite.
  • Configure environment variables per environment.
  • Deploy to Vercel, Netlify, and GitHub Pages.
  • Configure server redirects for Vue Router history mode.
  • Analyze and optimize bundle size.

3. Production Build

bash
12345
npm run build
# Outputs optimized files to /dist folder

npm run preview
# Preview the production build locally before deploying
text
1234567
/dist
├── index.html
├── assets/
│   ├── index-[hash].js      ← Main bundle
│   ├── vendor-[hash].js     ← node_modules (split automatically)
│   ├── about-[hash].js      ← Lazy-loaded chunks
│   └── index-[hash].css
vite.config.js
123456789101112131415161718192021222324
import { defineConfig } from 'vite'
import vue from '@vitejs/plugin-vue'
import { visualizer } from 'rollup-plugin-visualizer'

export default defineConfig({
  plugins: [
    vue(),
    visualizer({ open: true })  // Bundle analyzer
  ],
  build: {
    outDir: 'dist',
    sourcemap: false,  // true for easier debugging in production
    minify: 'esbuild',
    rollupOptions: {
      output: {
        manualChunks: {
          vendor: ['vue', 'vue-router', 'pinia'],
          ui: ['@headlessui/vue', 'lucide-vue-next']
        }
      }
    },
    chunkSizeWarningLimit: 500  // Warn if chunk > 500KB
  }
})

4. Environment Variables

bash
1234567891011
# .env (all environments)
VITE_APP_NAME=MyVueApp

# .env.development (dev server only)
VITE_API_BASE_URL=http://localhost:3000

# .env.production (npm run build)
VITE_API_BASE_URL=https://api.myapp.com

# .env.local (local overrides — NOT committed to git)
VITE_API_KEY=my-secret-key
javascript
123456
// Accessing env vars in Vue (must start with VITE_)
const apiUrl = import.meta.env.VITE_API_BASE_URL
const appName = import.meta.env.VITE_APP_NAME
const isDev = import.meta.env.DEV      // true in development
const isProd = import.meta.env.PROD    // true in production
const mode = import.meta.env.MODE      // 'development' | 'production'

5. Deploy to Vercel

bash
123456789101112
# Method 1: Vercel CLI
npm install -g vercel
vercel        # Follow prompts — auto-detects Vue/Vite
vercel --prod # Deploy to production

# Method 2: GitHub Integration (Recommended)
# 1. Push code to GitHub
# 2. Go to vercel.com → New Project → Import from GitHub
# 3. Framework: Vite
# 4. Build command: npm run build
# 5. Output directory: dist
# 6. Click Deploy → Get URL instantly
vercel.json
123
{
  "rewrites": [{ "source": "/(.*)", "destination": "/index.html" }]
}

6. Deploy to Netlify

bash
12345678910
# Method 1: Netlify CLI
npm install -g netlify-cli
netlify deploy --dir=dist --prod

# Method 2: Drag & Drop
# Build locally: npm run build
# Drag /dist folder to netlify.com/drop

# Method 3: GitHub integration (auto-deploy on push)
# netlify.com → New site from Git → GitHub → Select repo
netlify.toml
12345678
[build]
  command = "npm run build"
  publish = "dist"

[[redirects]]
  from = "/*"
  to = "/index.html"
  status = 200

7. Deploy to GitHub Pages

bash
1
npm install -D gh-pages
vite.config.js
123
export default defineConfig({
  base: '/your-repo-name/',  // Required for GitHub Pages subdirectory
})
package.json
123456
{
  "scripts": {
    "build": "vite build",
    "deploy": "npm run build && gh-pages -d dist"
  }
}
bash
1
npm run deploy  # Deploys to https://username.github.io/repo-name/

8. Server Configuration for History Mode

nginx
123456789101112131415161718
# Nginx (for self-hosted VPS)
server {
    listen 80;
    server_name myapp.com www.myapp.com;
    root /var/www/html/dist;
    index index.html;

    # Redirect ALL requests to index.html for SPA routing
    location / {
        try_files $uri $uri/ /index.html;
    }

    # Cache static assets aggressively
    location /assets {
        expires 1y;
        add_header Cache-Control "public, immutable";
    }
}
apache
123456789
# Apache (.htaccess)
<IfModule mod_rewrite.c>
  RewriteEngine On
  RewriteBase /
  RewriteRule ^index\.html$ - [L]
  RewriteCond %{REQUEST_FILENAME} !-f
  RewriteCond %{REQUEST_FILENAME} !-d
  RewriteRule . /index.html [L]
</IfModule>

9. Common Mistakes

  • Not configuring server redirects: Vue Router history mode requires ALL unknown URLs to return index.html. Without this, refreshing any page other than / gives a 404.
  • Committing .env.local to git: This file contains secrets. Add .env.local to .gitignore.

10. MCQs

Question 1

Vite production build output is in?

Question 2

Environment variables in Vite must start with?

Question 3

Why do deployed Vue SPAs need server redirect?

Question 4

import.meta.env.PROD is?

Question 5

.env.local should be?

Question 6

Vercel auto-detects Vue/Vite project because?

Question 7

manualChunks in Vite config?

Question 8

base: '/repo-name/' in vite.config.js is needed for?

Question 9

npm run preview does?

Question 10

netlify.toml configures?

11. Interview Questions

  • Q: Why do Vue Router history-mode apps need server configuration?
  • Q: How do environment variables work in a Vite-based Vue project?

12. Summary

Deploying Vue with Vite is streamlined — npm run build generates an optimized /dist folder. Vercel and Netlify deploy in minutes from GitHub with zero config. The critical server-side redirect rule /* → index.html is essential for Vue Router history mode to work correctly in production.

13. Next Chapter Recommendation

In Chapter 30: Final Projects and Real-World Applications, we build 6 complete Vue projects: ecommerce, chat, admin dashboard, social media, blog, and task management.

Finish this Chapter

Save your progress on your learning path and prepare for coding interview challenges.

Discussion

Join the discussion

Log in or create a free account to participate.

Sort: ·