Nuxt 101

Nuxt: the framework to scale up
universal Vue apps.

Adriร  Fontcuberta

@afontcu_

Software Engineer at Holaluz (๐Ÿ‘‹๐Ÿ’ก)

Adriร  Fontcuberta

@afontcu_

Frontend Developer at Holaluz (๐Ÿ‘‹๐Ÿ’ก)

Adriร  Fontcuberta

@afontcu_

UI Engineer at Holaluz (๐Ÿ‘‹๐Ÿ’ก)

Adriร  Fontcuberta

@afontcu_

Webmaster at Holaluz (๐Ÿ‘‹๐Ÿ’ก)

Nuxt 101

Nuxt: the framework to scale up
universal Vue apps.

Wait, what?

A framework built on top of a framework?

Vuenpm i vue
Vue CLInpm i -g @vue/cli
Nuxtnpm i -g create-nuxt-app

Nuxt or Vue CLI?

Universal mode

Reason #1 to use Nuxt

SPA - Single Page App (CSR)

SSR - Server Side Rendering

SGA - Static Generated App (Prerender)

What does "build on top of Vue" mean?

<nuxt-link> replaces <router-link>

<nuxt-child> replaces <router-view>

<nuxt> replaces <router-view> (in Layouts)

...

Magic folders

Reason #2 to use Nuxt

Convention over configuration

Pages

pages/
  page.vue        // app.com/page
  users/       
    index.vue     // app.com/users
    _id.vue       // app.com/users/{id}

Pages

pages/
  posts/
    index.vue     // app.com/posts
    latest.vue    // app.com/posts/latest
    _id/
      index.vue   // app.com/posts/{id}
      details.vue // app.com/posts/{id}/details
export default {
  // Called every time before loading the component
  async asyncData ({ params }) {
    const someStuff = await axios.get(`https://api.com/${params.id}`)

    // Returns an object that will be merged with `data`
    return { someStuff }
  },

  head () { },

  validate () { },
  ...
}
export default {
  // Called every time before loading the component
  // Fills the store before rendering
  async fetch ({ store, params }) {
    const { data } = await axios.get(`https://api.com/${params.id}`)

    await store.dispatch('WOW_SUCH_ACTION')
    store.commit('AMAZE_COMMIT', data)
  }
}

Layouts

layouts/
  default.vue
  full-screen.vue
// layout/default.vue

<template>
  <div>
    <app-header />
    <nuxt />
  </div>
</template>
// layout/full-screen.vue

<template>
  <div class="full-height">
    <nuxt />
  </div>
</template>
// random page

export default {
  layout: 'full-screen',
}

Store

store/
  user.js
  language.js
  posts/
    index.js  <-- Loads the other files
    state.js
    getters.js
    mutations.js
    actions.js 

Middleware

// middleware/auth.js
// it becomes the 'auth' middleware

export default function ({ store, redirect }) {
  if (store.state.user.loggedIn) {
    return redirect('/')
  }
}
// some page (or layout, or nuxt.config.js...)

export default {
  middleware: [ 'auth' ],
}

Modules

// nuxt.config.js

{
  modules: [
    ['@nuxtjs/google-analytics', {
      id: 'UA-12345-1',
    }],
  ],
}

Other cool stuff

Collocating routes and files

pages/
  contracts/
    index.vue
    components/
    store/
    tests/
    services/              
pages/
  contracts/
    index.vue
    components/   <- I
    store/        <- DON'T
    tests/        <- WANT
    services/     <- ROUTES
.nuxtignore

(v2.5 o nuxt-edge)

# ignore layout foo.vue
layouts/foo.vue

# ignore layout files whose name ends with -ignore.vue
layouts/*-ignore.vue

# ignore files inside /custom folders
pages/**/components/*
pages/**/store/*
pages/**/services/*

# ignore store files match *.test.*
store/*.test.*
// nuxt.config.js

export default {
  ignore: [
    '**/components/**',
    '**/store/**',
    '**/service/**',
    '**/test/**',
  ],
}

Route prefetching

Demo

Nuxt + globally accessible Sass

npm install -D sass-loader node-sass

npm install -D @nuxtjs/style-resources
// nuxt.config.js

{
  css: [
    '~assets/main'
  ],

  modules: [
    '@nuxtjs/style-resources',
  ],

  styleResources: {
    scss: [
      '~assets/autoload.scss',
    ],
  },
}

Recap

Nuxt is a framework built on top of Vue.

It simplifies SSR/Prerendering tremendously.

It is quite opinionated. Is it good? Is it bad?

"Convention over configuration" approach.

Has its own ecosystem (plugins, modules, Discord...).

There's a lot more!

TypeScript support

Page loading transitions

SEO HTML <head>

Custom modules + commands

...

Thanks! ๐Ÿ‘‹


Twitter: @afontcu_

Slides: https://nuxt-101.now.sh

npx afontcu

๐Ÿ˜ btw, we might be hiring! ๐Ÿ˜

are you interested? DM me @afontcu_