Configuration and Deployment
🎯 Introduction
By the end of this article, you will be able to configure Nuxt and choose a rendering mode and deployment approach for your project. You will learn common nuxt.config.ts options, how to use runtimeConfig, and the differences between SSR, SSG, and CSR.
🧱 What Is nuxt.config.ts?
nuxt.config.ts is the main configuration file for a Nuxt project. It controls modules, runtime settings, build behavior, the development server, and more.
A basic configuration looks like this:
export default defineNuxtConfig({
devtools: { enabled: true },
modules: ['@pinia/nuxt'],
css: ['~/assets/css/main.css'],
app: {
head: {
title: 'My Nuxt Blog',
meta: [{ name: 'description', content: 'A technical blog built with Nuxt' }],
},
},
});
~/ is a built-in Nuxt alias for the source directory. Use it to reference files in directories such as assets/ and components/.⚙️ Common Configuration Options
css: Global Styles
Add stylesheets to the css array to apply them across all pages:
export default defineNuxtConfig({
css: ['~/assets/css/main.css'],
});
app.head: Global Head Metadata
Configure site metadata, including the title, description, favicon, and SEO information:
export default defineNuxtConfig({
app: {
head: {
title: 'My Blog',
meta: [{ name: 'description', content: 'Sharing frontend development knowledge' }],
link: [{ rel: 'icon', href: '/favicon.ico' }],
},
},
});
modules: Extending Nuxt
Nuxt modules extend the framework. Integrations such as Pinia, Content, and Image are added as modules:
export default defineNuxtConfig({
modules: ['@pinia/nuxt', '@nuxt/content'],
});
🔐 runtimeConfig: Runtime Configuration
Use Nuxt's runtimeConfig for settings you do not want to hard-code, such as API URLs and secrets.
export default defineNuxtConfig({
runtimeConfig: {
// Private to the server; not exposed to the client
apiSecret: 'xxxxxx',
// Accessible to the client; must be inside public
public: {
apiBase: 'https://api.example.com',
},
},
});
Use it in a component:
<script setup>
const config = useRuntimeConfig();
console.log(config.public.apiBase); // Accessible on both the client and server
console.log(config.apiSecret); // Accessible only on the server
</script>
public, because it is exposed to the client.🌐 Three Rendering Modes
Nuxt supports three rendering modes. Choose one based on your application's requirements:
| Mode | Description | Suitable use cases |
|---|---|---|
| SSR | Server-side rendering generates HTML for each request | Frequently changing content, SEO, and authenticated users |
| SSG | Generates static HTML at build time | Infrequently changing content, blogs, and documentation |
| CSR | Renders entirely on the client | Admin dashboards and internal tools with little need for SEO |
🚀 Deployment Options
SSR Deployment
For a Node.js SSR deployment, build the project with:
npm run build
After the build completes, run:
node .output/server/index.mjs
npm run build creates .output/ with both server and client assets. Deploy this directory to a server that supports Node.js.SSG Static Deployment
For a fully static site, run:
npm run generate
This generates static HTML and assets that you can upload to a CDN or a static hosting platform such as GitHub Pages or Vercel. The static output directory is .output/public/.
generate, configure nitro.prerender.routes for dynamic routes that might otherwise be missed.❓ Questions and Answers
Q1: How do runtimeConfig and app.config differ?
Use runtimeConfig for environment-dependent settings, such as API URLs and secrets. Use app.config for public application settings, such as theme names and version numbers.
Q2: How do I handle dynamic routes with SSG?
List the dynamic paths to prerender in nitro.prerender.routes in nuxt.config.ts.
Q3: Can I deploy Nuxt to static hosting?
Yes. Run npm run generate and upload the generated .output/public/ directory to your static hosting platform.
Q4: Is SSR or SSG better for SEO?
Both produce HTML and work well for SEO. SSR suits frequently changing content, while SSG suits stable content.
🧪 Practice
Complete the following configuration tasks:
- Set the global site title to “My Nuxt Site” in
nuxt.config.ts. - Add
runtimeConfig.public.apiBasewith the valuehttps://jsonplaceholder.typicode.com. - Replace the hard-coded API URL in
app/pages/index.vuewithuseRuntimeConfig().public.apiBase. - Run both
npm run buildandnpm run generateand compare the server build in.output/with the static output in.output/public/.
export default defineNuxtConfig({
// Configure the global title here
runtimeConfig: {
public: {
// Configure apiBase here
},
},
});
🎉 You have completed the Nuxt.js introductory course! You now understand directory conventions, routing, data fetching, state management, and deployment configuration, and are ready to build your own Nuxt projects.
