Building Static Website with NuxtJS

Building Static Website with NuxtJS

Written by Gopakumar Gopalan,
on Thursday, February 13, 2020

Static sites are now regaining popularity. It is super fast loading as there is not processing on server. Static sites are all SEO ready. The coming back of static sites is made possible by Workflow Pipeline Services and CI/CD platforms.

There are many static site generators from well known Jekyll and Hugo to new Gatsby, NextJS and NuxtJS. Here we are using NuxtJS to create a static website. NuxtJS is a framework based on VueJS and you can do Prerendering (Creating static pages on compilation) or Server Side Render. We are using the Prerendering feature generate static web site.

As a first step we need to crate a new NuxtJs project. In your terminal from your projects directory run the command below:

// Using NPM

npx create-nuxt-app my-static-site

// Using Yarn

yarn create nuxt-app my-static-site 

Now you will be prompted to give a name, description etc to your project. Follow the steps below.

  • Choose programming language as JavaScript
  • Choose the package manager of your choice
  • Choose UI framework Bootstrap Vue from the list
  • Choose custom server framework None from list
  • Skip choosing Next.js modules
  • Skip adding listing tools
  • Choose test framework None from list
  • Choose rendering mode Universal (SSR) from list

Now we have a NuxtJS project ready. Go to project directory.

cd my-static-site

Now you need to run the development server by running

// Using NPM

npm run dev

// Using Yarn

yarn dev

You will get a development server running at http://localhost:3000/ . Now you need to edit and create some files.  First edit layouts/default.vue and replace with the content of the gist below.

Now you need to edit pages/index.vue and remove every thing and paste the code in the gist below.

Add a new file inside pages directory named about.vue and add contents of this gist.

In the pages directory create a file named services.vue and add contents of this gist.

All your pages need to be created as .vue files in pages directory. So we will create one more file there named contact-us.vue and put contents of the gist below.

Now all our pages are ready. You may check the site at http://localhost:3000/ . It's time to generate the static files. End the development server by pressing Ctrl+C in terminal.

Before you generate the static files you need to add line in the nuxt.config.js file under build section to extract the css styles to seperate files.

  build: {
    extractCSS: true,  // Add this line
    /*
    ** You can extend webpack config here
    */
    extend(config, ctx) {
    }
  }

And run the command below to create static files.

// Using NPM

npm run generate

// Using Yarn

yarn generate

Now we have upload ready static site in the ./dist directory. You just need to remove the README.md file and upload! That's all !

You may use any build pipeline to automatically run the generate command and deploy the files to the server. The easy way is to do it with Netlify and GitHub.

Static sites are now regaining popularity. It is super fast loading as there is not processing on server. Static sites are all SEO ready. The coming back of static sites is made possible by Workflow Pipeline Services and CI/CD platforms.

There are many static site generators from well known Jekyll and Hugo to new Gatsby, NextJS and NuxtJS. Here we are using NuxtJS to create a static website. NuxtJS is a framework based on VueJS and you can do Prerendering (Creating static pages on compilation) or Server Side Render. We are using the Prerendering feature generate static web site.

As a first step we need to crate a new NuxtJs project. In your terminal from your projects directory run the command below:

// Using NPM

npx create-nuxt-app my-static-site

// Using Yarn

yarn create nuxt-app my-static-site 

Now you will be prompted to give a name, description etc to your project. Follow the steps below.

  • Choose programming language as JavaScript
  • Choose the package manager of your choice
  • Choose UI framework Bootstrap Vue from the list
  • Choose custom server framework None from list
  • Skip choosing Next.js modules
  • Skip adding listing tools
  • Choose test framework None from list
  • Choose rendering mode Universal (SSR) from list

Now we have a NuxtJS project ready. Go to project directory.

cd my-static-site

Now you need to run the development server by running

// Using NPM

npm run dev

// Using Yarn

yarn dev

You will get a development server running at http://localhost:3000/ . Now you need to edit and create some files.  First edit layouts/default.vue and replace with the content of the gist below.

Now you need to edit pages/index.vue and remove every thing and paste the code in the gist below.

Add a new file inside pages directory named about.vue and add contents of this gist.

In the pages directory create a file named services.vue and add contents of this gist.

All your pages need to be created as .vue files in pages directory. So we will create one more file there named contact-us.vue and put contents of the gist below.

Now all our pages are ready. You may check the site at http://localhost:3000/ . It's time to generate the static files. End the development server by pressing Ctrl+C in terminal.

Before you generate the static files you need to add line in the nuxt.config.js file under build section to extract the css styles to seperate files.

  build: {
    extractCSS: true,  // Add this line
    /*
    ** You can extend webpack config here
    */
    extend(config, ctx) {
    }
  }

And run the command below to create static files.

// Using NPM

npm run generate

// Using Yarn

yarn generate

Now we have upload ready static site in the ./dist directory. You just need to remove the README.md file and upload! That's all !

You may use any build pipeline to automatically run the generate command and deploy the files to the server. The easy way is to do it with Netlify and GitHub.

Related Tags :

> @GKWrites > Building Static Website with NuxtJS