Skip to content

Nuxt 3 Demo App with Prerender and SSR

Introduction

The quest for an optimal web application involves numerous factors, such as performance, user experience, and search engine optimization (SEO). Nuxt 3 provides excellent features out-of-the-box that can help you tackle these aspects efficiently.

This article will guide you in creating a Nuxt 3 application using Server Side Rendering (SSR) and pre-rendering capabilities. We'll craft an application that offers a seamless user experience, superior performance, and improved SEO.

Server Side Rendering in Nuxt 3

Enhancing User Experience and SEO with SSR

In the world of web development, Server Side Rendering (SSR) has gained significant traction. It refers to the server rendering the webpage upon each request rather than the client. With SSR, your web page arrives fully formed at the client's doorstep, reducing the time for the first painting.

The advantage of SSR extends beyond just performance and user experience. It's also a darling of search engine crawlers, which find it easier to index your website with fully formed page content at the time of their crawl. In simple terms, it's like providing a tour guide to lead the search engine around your website, ensuring they get all the spots!

The Power of SSR in Nuxt 3

Nuxt 3 has built-in support for SSR by default. It allows you to optimize your application by loading the data before rendering the page. To take advantage of SSR, you simply build your application as you usually would. In this case, using the fetch() method to load some.

<script setup>
import { ref } from 'vue'

const data = ref(null)

async function fetchData() {
  const response = await fetch('https://api.example.com/data')
  const jsonData = await response.json()
  data.value = jsonData
}

fetchData()
</script>

<template>
  <div>
    <h1>Data fetched from the server</h1>
    <pre>{{ data }}</pre>
  </div>
</template>

In this example, the fetchData function fetches the data before rendering the page. The data then gets displayed within the <pre> tag.

Sidenote, if you wanted to disable SSR in your Nuxt application, you would do so in your nuxt.config.ts file:

export default defineNuxtConfig({
  ssr: false # this usually defaults to true
})

More details on how this works can be found in the rendering modes section in the official Nuxt 3 documentation.

Pre-rendering in Nuxt 3

Amplifying Application Performance with Pre-rendering

While SSR has its advantages, another superhero in our story is pre-rendering. Pre-rendering refers to the process of generating the static HTML pages of a site in advance. It's like preparing a feast before the guests arrive so you can serve them promptly.

Like SSR, pre-rendering enhances SEO, as search engines can index fully rendered pages. Especially for content-heavy applications, pre-rendering can give a turbo boost to performance, leading to better user engagement and retention.

Leverage Pre-rendering with Nuxt 3

Nuxt 3 supports SSR and Static Site Generation (SSG), including pre-rendering. To enable prerendering, you need to export your routes in nuxt.config.ts:

export default defineNuxtConfig({
  nitro: {
    prerender: {
      routes: ['/route1', '/route2', '/route3']
    }
  }
})

These routes will be pre-rendered during the build phase, allowing for swift navigation. Let's update our application to pre-render the users' list:

<script setup>
import { ref } from 'vue'

const users = ref([])

async function fetchUsers() {
  const response = await fetch('https://api.example.com/users')
  const userJson = await response.json()
  users.value = userJson
}

fetchUsers()
</script>

<template>
  <div>
    <h1>Users</h1>
    <ul>
      <li v-for="user in users" :key="user.id">
        {{ user.name }}
      </li>
    </ul>
  </div>
</template>

In the nuxt.config.ts, include the users' route for pre-rendering:

export default defineNuxtConfig({
  nitro: {
    prerender: {
      routes: ['/users']
    }
  }
})

Going beyond with Nuxt 3

While SSR and pre-rendering are significant for performance and SEO, Nuxt 3 offers more advanced features like hot module replacement, Vue 3 integration, improved error handling, and more. Nuxt 3 also has different kinds of rendering modes for different applications; in our SSR case, we used the default Universal rendering mode. Leveraging these features allows you to develop robust, high-performance, and user-friendly applications.

Conclusion

In this article, we delved into the concepts of SSR and pre-rendering in Nuxt 3. We explored how these techniques contribute to enhancing performance, user experience, and SEO. You can create exceptional web applications by integrating Vue 3's capabilities with Nuxt 3's robust features.

A well-structured, performant, and SEO-friendly application not only attract users but also retains them. Embrace the power of Nuxt 3 and Vue 3 in your development journey. Happy coding!