</>
Skip to content
Vue lessons (42/43)

Vue — SSR

What is SSR?

Server-side rendering renders Vue components on the server, sending HTML to the browser.

Benefits

  • Better SEO
  • Faster initial load
  • Social media previews
  • Accessibility

Nuxt.js

npx nuxi init my-nuxt-app
cd my-nuxt-app
npm install
npm run dev

Page component

<script setup>
const { data } = await useFetch('/api/users')
</script>

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

Data fetching

<script setup>
// Runs on server and client
const { data } = await useFetch('/api/posts')

// Only on server
const { data } = await useAsyncData('posts', () =>
  $fetch('/api/posts')
)
</script>

Layouts

<!-- layouts/default.vue -->
<template>
  <div>
    <header>Navigation</header>
    <slot />
    <footer>Footer</footer>
  </div>
</template>

Middleware

// middleware/auth.js
export default defineNuxtRouteMiddleware((to, from) => {
  if (!isLoggedIn()) {
    return navigateTo('/login')
  }
})

SEO

<script setup>
useHead({
  title: 'My Page',
  meta: [
    { name: 'description', content: 'Page description' }
  ]
})
</script>

Static generation

# Generate static site
npm run generate

# Output in .output/public

Mini Practice

  1. Create a Nuxt app
  2. Add server-side data fetching
  3. Use layouts and middleware
  4. Optimize for SEO

Up Next

Continue with Performance - Optimization tips.

Related Topics

Frequently Asked Questions about SSR

What is SSR in Vue?

SSR is a fundamental concept in Vue. This lesson explains it step by step with clear examples, making it easy for beginners to understand.

How do I learn SSR?

Start by reading the explanation above, then try the code examples. Practice by modifying the examples and experimenting with different values. Hands-on practice is the best way to learn SSR.

Why is SSR important in Vue?

SSR is essential for Vue development. Understanding this concept will help you write better code and solve real-world problems more effectively.