Code Monkey home page Code Monkey logo

serverless-nuxt's Introduction

Serverless Nuxt Plugin

Nuxt on AWS(Lambda + S3) with Serverless Framework.

Downloads Version License
dependencies Status devDependencies Status
NPM

Installation

If you don't have a Nuxt project already, let's create it first. If you have already installed a Nuxt project, you can skip the following.

Nuxt Official : Installation

Install the plug-in in the project directory.

cd my-nuxt-project
npm i serverless-nuxt

Important

Lambda has a maximum file size of 256 MB. (cf. AWS Lambda Limits) You will need to delete the package files you need during the build process. There is a good package for this.

npm i nuxt-start
npm i nuxt -D

We will touch a total of 3 files. Very easy.

  • serverless.yml
  • handler.js
  • nuxt.config.js

Once you've used the Serverless framework, it's easier. It is helpful to read the Manual once.

serverless.yml

Add the serverless.yml file. The my-nuxt-project part is set to your project name. Use the Cloud Formations to upload assets(.nuxt/dist/client) files to the AWS S3 repository. If you have an S3 repository already created, remove Cloud Formation information(resources section).

Write your plugin settings in the custom.nuxt field. The version(custom.nuxt.version) is used as a prefix when uploading assets files to S3. For more options, please see here.

service:
  name: my-nuxt-project

plugins:
  - serverless-nuxt/plugin

resources:
  Resources:
    AssetsBucket:
      Type: AWS::S3::Bucket
      Properties:
        BucketName: ${self:custom.nuxt.bucketName}
        CorsConfiguration:
          CorsRules:
            - AllowedMethods:
              - GET
              - HEAD
              AllowedOrigins:
              - "*"

provider:
  name: aws
  stage: ${opt:stage, 'dev'}
  runtime: nodejs8.10
  environment:
    NODE_ENV: ${file(.env.${self:provider.stage}.yml):NODE_ENV}

custom:
  nuxt:
    version: v0.0.1-alpha
    bucketName: my-nuxt-project-${self:provider.stage}

functions:
  nuxt:
    handler: handler.render
    events:
      - http: ANY /
      - http: ANY /{proxy+}

You can modify the existing Nuxt configuration file (nuxt.config.js) to fit the following format:

const pkg = require("./package.json")

// "export default" => "module.exports ="
module.exports = {
  mode: "universal",
  head: {
    title: pkg.name,
    meta: [
      { charset: "utf-8" },
      { name: "viewport", content: "width=device-width, initial-scale=1" },
      { hid: "description", name: "description", content: pkg.description },
    ],
    link: [
      { rel: "icon", type: "image/x-icon", href: "/favicon.ico" },
    ],
  },
  build: {
    // The "publicPath" value is automatically generated by the plug-in
  },
}

Finally, the handler(handler.js) is written as:

const { createNuxtApp } = require("serverless-nuxt")
const config = require("./nuxt.config.js")

module.exports.render = createNuxtApp(config)

Proceed as follows: The serverless plug-in builds automatically at deployment time.

Never use the npm run build command.

sls deploy --stage dev

Options

custom:
  nuxt:
    version: v1.0.0-alpha
    bucketName:
    assetsPath:
Name Description Default
version (required) version
bucketName (required) AWS S3 Bucket Name for static files
cdnPath CDN Path null
assetsPath ".nuxt/dist/client"

References

License

MIT

serverless-nuxt's People

Watchers

James Cloos avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.