Building a Web App Using Nuxt.js and Firebase

Ben Mayer
Ben Mayer
Dec 26, 2020 · 6 min read
Image for post
Image for post
Combine Nuxt.js and Firebase

Why I decided to build a blog from scratch using Nuxt.js and hosted it by deploying to Firebase Functions

I created this website from scratch. Instead of the usual Wordpress site or any existing headless CMS, I chose to build it from the ground up (with a little help from other exiting projects) using Nuxt.js as the foundation. Nuxt.js, promised being an intuitive, yet proweful framework for building Vue.js applications. After trying it out for about half an hour, Nuxt.js and Vue.js felt most intuitive and fun, compared to its alternatives Angular or React, especially in terms rendering content from the server (SSR), which was a crucial reason to pick Nuxt.js

Getting started

First, you will have to create a Firebase account and set up a firebase project, following these steps in the firebase setup guide. Make sure you upgrade from the Free “Spark” plan to the Pay as you go “Blaze” plan, as you’ll need this to deploy your app later.

$ mkdir <my-firebase-nuxt-project>
$ cd <my-firebase-nuxt-project>

Install Firebase

Install Firebase CLI — if you don’t have it installed already — log in and initiate firebase:

$ firebase login
$ firebase init
  1. Choose the project you set up in the first step, or create new one, and give it an ID and name. This will be your project in the Google Firebase Console.
  2. Choose Javascript
  3. Skip ESLint
  4. Don’t install dependencies now, as we’ll do that later
  5. Choose ‘public’ as your default public folder. (This is where the static files go later)
  6. Don’t configure as single page app (don’t rewrite all urls to the index.html files)
  7. Skip automatic builds and deploys with Github
Image for post
Image for post
Image for post
Image for post

Create Nuxt.js app

These next steps are base on the regular Nuxt.js installation from the Nuxt.js installtion guide.

$ yarn create nuxt-app src
$ cd src  
$ yarn build
Image for post
Image for post

Connecting Nuxt to Firebase

Now that we have Firebase and Nuxt.js set up, let’s connect the two. In ./functions/node.js add the following code:

const { Nuxt } = require('nuxt-start');const nuxtConfig = require('../src/nuxt.config.js');const config = {
...nuxtConfig,
dev: false,
debug: false
};
const nuxt = new Nuxt(config);exports.ssrapp = functions.https.onRequest(async (req, res) => {
await nuxt.ready();
nuxt.render(req, res);
});
$ cd ../functions
$ yarn install
$ yarn add nuxt-start
{
"functions": {
"source": "functions"
},
"hosting": {
"public": "public",
"ignore": [
"firebase.json",
"**/.*",
"**/node_modules/**"
],
"rewrites": [
{
"source": "**",
"function": "ssrapp"
}
]
}
}
$ mkdir -p functions/.nuxt/dist/
$ cp -r src/.nuxt/dist/server functions/.nuxt/dist/
$ rm ./public/*
$ mkdir -p public/_nuxt
$ cp -r src/.nuxt/dist/client/ public/_nuxt && cp -a src/static/. public/
$ yarn serve --only functions,hosting
$ yarn deploy

The Startup

Medium's largest active publication, followed by +775K people. Follow to join our community.

Ben Mayer

Written by

Ben Mayer

Lead Product Manager at Urban Sports Club. Bass player and love climbing.

The Startup

Medium's largest active publication, followed by +775K people. Follow to join our community.

Ben Mayer

Written by

Ben Mayer

Lead Product Manager at Urban Sports Club. Bass player and love climbing.

The Startup

Medium's largest active publication, followed by +775K people. Follow to join our community.

Medium is an open platform where 170 million readers come to find insightful and dynamic thinking. Here, expert and undiscovered voices alike dive into the heart of any topic and bring new ideas to the surface. Learn more

Follow the writers, publications, and topics that matter to you, and you’ll see them on your homepage and in your inbox. Explore

If you have a story to tell, knowledge to share, or a perspective to offer — welcome home. It’s easy and free to post your thinking on any topic. Write on Medium

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store