• Stars
    star
    641
  • Rank 67,720 (Top 2 %)
  • Language
    TypeScript
  • License
    MIT License
  • Created about 5 years ago
  • Updated 3 months ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

Vercel Builder for Nuxt

vercel-builder

Nuxt Vercel Builder

npm version npm downloads packagephobia Github actions status Codecov Dependencies Standard JS

โš ๏ธ This is a legacy builder and only works for Nuxt 2. We'd strongly recommend using Nuxt Bridge or Nuxt 3, which use the latest Vercel features instead.


@nuxtjs/vercel-builder allows you ship a fast, production-ready Nuxt 2 application that scales automatically on Vercel when using SSR rendering.

When to use it

This package is only made for Nuxt 2 SSR applications - and you probably do not need to use it.

๐Ÿ‘‰ If you want to deploy a statically generated Nuxt 2 application instead, Vercel is a zero configuration provider. Check this guide from Vercel for more information.

๐Ÿ‘‰ If you want to deploy a Nuxt Bridge or Nuxt 3 app, Vercel deployment will work with zero configuration. Check this guide for more information.

โš ๏ธ We would advise you migrate your app to Nuxt 3, which features a much superior integration with Vercel using their latest Build API.

How it works

This Vercel builder takes a Nuxt application defined by a nuxt.config.js (or .ts) entrypoint and deploys it as a serverless function in a Vercel environment.

It features built-in caching of node_modules and the global yarn cache (even when dependencies change) and a multi-stage build for fast and small deployments.

Setup

All you need is a Nuxt application and a Vercel account.

Then, simply create a vercel.json file at the root of your project:

{
  "version": 2,
  "builds": [
    {
      "src": "nuxt.config.js",
      "use": "@nuxtjs/vercel-builder"
    }
  ]
}

NOTE: When installing your dependencies, Vercel will use the same package manager that is used in the project. Using yarn is highly recommended due to its autoclean functionality, which can decrease lambda size.

Examples

See Basic example for a more complete deployable example, including an example of how to set up vercel dev support.

See Deploying two Nuxt apps side-by-side for details on deploying two Nuxt apps in one monorepo.

Configuration

serverFiles

  • Type: Array

If you need to include files in the server lambda that are not built by webpack (or within static/), such as a local module or serverMiddleware, you may specify them with this option. Each item can be a glob pattern.

Example

{
  "builds": [
    {
      "src": "nuxt.config.js",
      "use": "@nuxtjs/vercel-builder",
      "config": {
        "serverFiles": ["server-middleware/**"]
      }
    }
  ]
}

internalServer

  • Type: Boolean
  • Default: false

If you have defined serverMiddleware in your nuxt.config, this builder will automatically enable an internal server within the lambda so you can access your own endpoints via http://localhost:3000. (This does not affect how you call your endpoints from client-side.)

If you need to enable or disable the internal server manually (for example, if you are adding server middleware via a module), just set internalServer within the builder options.

generateStaticRoutes

  • Type: Boolean
  • Default: false

To pre-render routes during the build using nuxt generate set this to true. Routes that are not generated will fallback to the server lambda. You will need to specify the routes to be generated in your nuxt.config.

Example

{
  "builds": [
    {
      "src": "nuxt.config.js",
      "use": "@nuxtjs/vercel-builder",
      "config": {
        "generateStaticRoutes": true
      }
    }
  ]
}

tscOptions

  • Type: Object

If you need to pass TypeScript compiler options to override your tsconfig.json, you can pass them here. See the TypeScript documentation for valid options.

Example

{
  "src": "nuxt.config.ts",
  "use": "@nuxtjs/vercel-builder",
  "config": {
    "tscOptions": {
      "sourceMap": false
    }
  }
}

You can also include a tsconfig.vercel.json file alongside your tsconfig.json file. The compilerOptions from those files, along with any tscOptions passed through vercel.json, will be merged and the resulting options used to compile your nuxt.config.ts, local modules and serverMiddleware.

memory

  • Type: Number

Pass this option if you need to customize the default memory limit of the serverless function that renders your pages.

maxDuration

  • Type: Number

Pass this option if you need to customize the max duration of the serverless function that renders your pages.

Environment variables

env

If you are accessing environment variables via env within your Nuxt build, then they will be baked in at build time. This means that if you update the variables in the Vercel dashboard, you will need to trigger a deployment again for the changes to take effect. You must include these variables in build.env in your vercel.json (see below).

runtimeConfig

If you are using Nuxt 2.13+, it is recommended to use the new runtimeConfig options instead.

Exposing variables

There are two environments where you may need to expose environment variables within vercel.json. They are env (for runtime variables) and build.env (for build-time variables, which may not be required for runtimeConfig). See Vercel documentation. For example:

{
  "env": {
    "MY_VARIABLE": true
  },
  "build": {
    "env": {
      "MY_VARIABLE": true
    }
  }
}

Finally, note that if you want to access Vercel's system environment variables, you may want ensure that system environment variables are automatically exposed.

Usage with Typescript

vercel-builder supports TypeScript runtime compilation. It adds in a pre-compilation step as part of building the lambda for files not compiled by Webpack, such as nuxt.config.ts, local modules and serverMiddleware.

References to original TS files in strings outside of modules or serverMiddleware may therefore cause unexpected errors.

Don't forget to update your Nuxt config filename in your vercel.json file.

Technical details

Monorepos

Just enable the "Include source files outside of the Root Directory in the Build Step" option in the Root Directory section within the project settings.

Vercel monorepo config

Private npm modules

To install private npm modules, define NPM_AUTH_TOKEN or NPM_TOKEN as a build environment variable in vercel.json.

Alternatively, you can inline your entire .npmrc file in a NPM_RC environment variable.

Node.js version

The newest available Node.js version is automatically selected. If your packages depend on a particular major release Node.js version, you can specify the version in your package.json - see Vercel documentation.

vercel-build script support

This builder will run a given custom build step if you have added a vercel-build key under scripts in package.json.

Deploying additional serverless functions

You canย also deploy additional serverless functions alongside your Nuxt application.

serverMiddleware

Create an api folder at the root of your project, and then create a file in it, for example hello.js:

import express from 'express'
import bodyParser from 'body-parser'

const app = express()
app.use(bodyParser.json())

// It is important that the full path is specified here
app.post('/api/hello', function(req, res) {
  const { info } = req.body
  console.log(info)
  res
    .status(200)
    .json({ info })
    .end()
})

export default app

Setup the Vercel config

In your vercel.json, add your additional endpoints:

{
  "version": 2,
  "routes": [
    {
      "src": "/api/hello",
      "dest": "/api/hello.js"
    }
  ],
  "builds": [
    {
      "src": "api/**/*.js",
      "use": "@vercel/node"
    },
    {
      "src": "nuxt.config.ts",
      "use": "@nuxtjs/vercel-builder",
      "config": {
        "serverFiles": ["api/**"]
      }
    }
  ]
}

Add it to the Nuxt config

If you want to interact with this API whilst developing your Nuxt app, you can add it to your serverMiddleware conditionally.

export default {
  serverMiddleware:
    process.env.NODE_ENV === 'production' ? [] : ['~/api/hello.js'],
}

And that's it! You can now go to http://locahost:3000/api/hello and see the result! In production the endpoint will be handled with Vercel, but locally Nuxt will manage it for you.

License

MIT License

Documentation and builder inspired by Next.js by Vercel

Copyright (c) Nuxt Community

More Repositories

1

nuxt

The Intuitive Vue Framework.
TypeScript
51,559
star
2

framework

Old repo of Nuxt 3 framework, now on nuxt/nuxt
10,721
star
3

awesome

A curated list of awesome things related to Nuxt.js
5,014
star
4

vue-meta

Manage HTML metadata in Vue.js components with SSR support
JavaScript
4,064
star
5

create-nuxt-app

Create Nuxt.js App in seconds.
JavaScript
3,451
star
6

ui

A UI Library for Modern Web Apps, powered by Vue & Tailwind CSS.
Vue
3,316
star
7

content

The file-based CMS for your Nuxt application, powered by Markdown and Vue components.
TypeScript
2,976
star
8

devtools

Unleash Nuxt Developer Experience
Vue
2,681
star
9

website-v2

Nuxt 2 Documentation Website
Vue
2,241
star
10

movies

๐Ÿฟ A TMDB client built with Nuxt 3
Vue
1,818
star
11

vite

โšก Vite Experience with Nuxt 2
TypeScript
1,385
star
12

image

Plug-and-play image optimization for Nuxt applications.
TypeScript
1,247
star
13

hackernews

HackerNews clone built with Nuxt.
Vue
1,197
star
14

components

Scan and auto import components for Nuxt.js 2.13+
TypeScript
879
star
15

modules

Discover the Nuxt modules to add any CMS, Database, UI, Auth and integrations into your Vue application.
TypeScript
871
star
16

example-auth0

A simple example that shows how to use Nuxt.js with Auth0.
Vue
713
star
17

typescript

TypeScript Support for Nuxt 2
TypeScript
567
star
18

docs

Old Documentation of Nuxt.js ๐Ÿ’š - not in use anymore
539
star
19

learn.nuxt.com

[Work in Progress] An interactive tutorial and playground for Nuxt
Vue
492
star
20

starter

Create a new Nuxt project, module, layer or start from a theme with our collection of starters.
382
star
21

eslint-config

ESlint config used for Nuxt
JavaScript
346
star
22

fonts

Plug-and-play web font optimization and configuration for Nuxt apps.
TypeScript
345
star
23

test-utils

๐Ÿงช Test utilities for Nuxt
TypeScript
273
star
24

assets

๐ŸŽจ Unified Assets and Templates for Nuxt
HTML
267
star
25

bridge

๐ŸŒ‰ Experience Nuxt 3 features on existing Nuxt 2 projects
TypeScript
263
star
26

nuxt.com

The Nuxt website, made with Nuxt.
Vue
225
star
27

nuxt.new

Create a new Nuxt project from your address bar.
Vue
225
star
28

http

Universal HTTP Module for Nuxt.js
JavaScript
221
star
29

cli

โšก๏ธ Nuxt Generation CLI Experience.
TypeScript
207
star
30

module-builder

Complete solution to build Nuxt Modules.
TypeScript
202
star
31

nitro-demo

nuxt nitro preview
Vue
187
star
32

telemetry

Nuxt Telemetry
TypeScript
185
star
33

press

[Deprecated] Minimalist Markdown Publishing for Nuxt.js
JavaScript
184
star
34

todomvc

Nuxt.js TodoMVC Example
Vue
146
star
35

examples

Deployed Nuxt examples
TypeScript
134
star
36

eslint-plugin-nuxt

ESLint plugin for Nuxt.js [WIP]
JavaScript
129
star
37

nuxters

Vue
124
star
38

rfcs

RFCs for changes to Nuxt.js
97
star
39

loading-screen

Loading Screen Module for Nuxt.js
JavaScript
90
star
40

scripts

Plug-and-play script optimization for Nuxt applications. (Public Preview)
TypeScript
65
star
41

cli-draft

WIP: CLI for Nuxt.js projects
62
star
42

postcss8

Opt-in to postcss 8 in Nuxt 2 apps.
TypeScript
58
star
43

actions-yarn

Github Actions for yarn
Shell
47
star
44

codesandbox-nuxt

Starter template for CodeSandBox.io
Vue
43
star
45

blueprints

Module for Nuxt.js to create distributable micro-apps
JavaScript
41
star
46

benchmarks

๐Ÿ’จ Nuxt.js SSR performance Benchmarks
JavaScript
24
star
47

renovate-config-nuxt

Nuxt.js presets for Renovate tool.
18
star
48

nuxt-services-experimental

[WIP] Around realtime services for Nuxt 3
JavaScript
17
star
49

nuxt3-stubs

JavaScript
13
star
50

static

Fast Static Builder
TypeScript
12
star
51

governance

Nuxt Project Governance
12
star
52

babel-preset-app

PSA: this repo has been moved into nuxt.js/nuxt --> https://github.com/nuxt/nuxt.js/tree/dev/packages/babel-preset-app
JavaScript
10
star
53

nuxt-redirects

Nuxt.js redirects app for Netlify
9
star
54

.github

Default community health files for nuxt repositories
4
star