Skip to content
Tutorials Center
My learning

Topics

Lesson 31 of 32 ยท Practice and review

Nuxt Interview Questions and Answers

30 Nuxt interview questions with short, accurate answers on rendering, hydration, data fetching, Nitro, middleware and SSR state, plus runnable stand-ins for the tricky ones.

A Nuxt interview is rarely about reciting the API. It checks whether you understand what runs where and when: on the server during the first request, in the browser after hydration, or in the Nitro engine that sits behind both. Most of the hard questions come back to that one idea. The thirty questions below are grouped by topic, and each answer is short enough to say out loud in under a minute.

How to use this page:

  • Answer first, then read. Say your answer in your own words, then compare it with the bold phrases, which are the points interviewers listen for.
  • Follow the links. Every answer points to the lesson that explains the idea in depth.
  • Expect follow-ups. "Why?" and "what breaks if...?" are the normal next questions, so some answers include the usual one.
  • Know the version. This page targets Nuxt 4, where application code lives in app/ and server/ stays at the project root. Nuxt 3 projects without an app/ folder behave the same way; only the paths differ.

The real framework cannot run on this site, so the snippets in this lesson are read-only. Two runnable stand-ins prove the trickiest answers. They are small plain JavaScript models of the idea, not Nuxt itself, and each one says so.

Where does the code run?

Server render First request, HTML is built here.
Browser Hydrates, then handles navigation.
Nitro Server routes, caching, deployment.

Most Nuxt questions are really "which of these three?"

1 / 2

Fundamentals

1. What is Nuxt and what does it add to Vue?

Nuxt is a full-stack meta-framework built on Vue. Plain Vue gives you components and reactivity; Nuxt adds file-based routing, server-side rendering, auto-imports, data-fetching composables, a server engine (Nitro), a module system and deployment presets. You mostly follow folder conventions instead of wiring things by hand. See what Nuxt is.

2. Explain the main folders of a Nuxt project.

In Nuxt 4, app/ holds the front end (pages/, components/, composables/, layouts/, middleware/, plugins/, app.vue), server/ holds API routes and server code, public/ holds files served as-is, and nuxt.config.ts configures everything. The generated .nuxt/ folder is build output, and you never edit it. See project structure.

3. What are auto-imports and what is their downside?

Nuxt scans components/, composables/ and utils/ (and the Vue and Nuxt built-ins) and imports what you use at build time, so you write useFetch or <UserCard /> without an import. The cost is less obvious origins: a name can come from anywhere, two files can collide on a name, and code outside Nuxt's scan, such as a plain script, does not get them. You can always import explicitly. See auto-imports.

4. How does file-based routing work?

Every file in pages/ becomes a route: pages/index.vue is /, pages/about.vue is /about, and a folder adds a path segment. A file with the same name as a folder becomes the parent of the nested routes inside it and must render <NuxtPage />. Nuxt generates the Vue Router table from the file tree. See file-based routing.

5. How do dynamic routes and catch-alls look?

Square brackets: [id].vue matches one segment (/users/42), [[id]].vue makes it optional, and [...slug].vue matches any number of segments. Read the value with useRoute().params. A catch-all is the right tool for CMS paths, and you should check params before using them. See dynamic routes.

6. What is app.vue and when do you need NuxtPage?

app.vue is the root component. If your project has a pages/ folder, app.vue should render <NuxtPage /> (usually inside <NuxtLayout>), which shows the matched page. Without a pages/ folder, Nuxt skips the router entirely and app.vue is the whole app, which suits a one-screen site.

Rendering and hydration

7. What is universal rendering?

On the first request the server renders the page to HTML so the browser and crawlers get real content immediately. Then the browser downloads the JavaScript, hydrates the HTML (attaches reactivity and listeners) and from then on it behaves as a single-page application: navigation happens on the client. This is the default mode. See rendering modes.

8. What is hydration?

Hydration is Vue taking the HTML the server produced and adopting the existing DOM instead of building it again. It walks the component tree, matches each virtual node to its real node, and attaches event listeners and state. The page is visible before hydration but not interactive until it finishes. See hydration and SSR pitfalls.

9. What causes a hydration mismatch?

The server HTML and the client's first render disagree. Typical causes are Date.now(), Math.random(), values read from window, localStorage or navigator during setup, locale or time-zone formatting that differs between server and browser, and invalid HTML that the browser repairs (a <div> inside a <p>). Vue warns in development, and in the worst case the DOM ends up wrong.

10. How do you fix a hydration mismatch?

Make the first client render equal the server render. Move browser-only values into onMounted (which never runs on the server), wrap browser-only widgets in <ClientOnly> with a fallback, use useState or useCookie to share a value computed on the server, and use useId() for ids that must match. Do not hide the warning; remove the difference.

11. When would you turn SSR off?

When the page needs no SEO and depends heavily on browser APIs, such as an admin tool behind a login, you can set ssr: false globally (an SPA) or just for some paths with routeRules: { '/admin/**': { ssr: false } }. You trade faster first content and crawlable HTML for simplicity. A hybrid setup is common.

12. What is the difference between SSR, SSG and ISR/SWR?

SSR renders on every request. SSG (nuxt generate, or prerender: true) renders at build time into static files. SWR serves a cached render and revalidates in the background after a time; ISR is similar but the cache is managed by the host (Vercel and Netlify have support). You choose per route with routeRules. See rendering modes.

13. What are routeRules?

A map from path patterns to per-route behavior, set in nuxt.config.ts: prerender, ssr, swr, isr, cache, redirect, headers and cors. It lets one app mix static marketing pages, cached blog pages and dynamic dashboards. When patterns overlap, the more specific one wins. See the cheat sheet.

TypeScript
export default defineNuxtConfig({
  routeRules: {
    '/': { prerender: true },
    '/blog/**': { swr: 600 },
    '/admin/**': { ssr: false },
    '/old-docs/**': { redirect: '/docs' },
  },
})

14. What is the cost of hydrating a big page, and how do you cut it?

Hydration runs JavaScript for the whole tree. Reduce it by splitting rarely used components into lazy components (prefix the name with Lazy) and using the lazy hydration strategies such as hydrate-on-visible, by shipping less data in the payload, and by keeping heavy libraries out of the first render. See performance.

Data fetching

15. useFetch, useAsyncData and $fetch: when do you use each?

useFetch(url) is a shortcut for useAsyncData plus $fetch with an automatic key; use it for a plain request. useAsyncData(key, fn) wraps any async function (a CMS SDK, several requests combined). $fetch is just the HTTP client; use it in event handlers (a submit button) and in server code, not at the top level of setup. See data fetching.

16. Why not call $fetch directly in setup?

During SSR it runs on the server, then runs again in the browser during hydration, so you fetch twice and may flash different content. useFetch and useAsyncData store the server result in the payload and reuse it on the client. The stand-in below shows the counting.

17. What does the payload do?

Nuxt serializes the data returned by useAsyncData, useFetch and useState into the HTML (in a <script> tag with the id __NUXT_DATA__). When the app hydrates, the composables find their key there and skip the request. It also means everything you return becomes part of the page size, so return only the fields you need. See the data fetching lesson.

Node.js Stand-in: server payload versus a second request

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with node:readline or process.stdin), it gets the next line from this box.

The real payload is a compact serialized format, not plain JSON, but the idea is the same: the server fetches once and the browser reads the answer from the page.

18. What is the key in useAsyncData for?

The key identifies the request in the payload and in the shared cache. Two calls with the same key share data, a refresh with that key updates both, and the client finds the server's answer by it. useFetch builds the key from the URL and options. Give distinct keys to different data, and put dynamic values (an id) into the key.

19. How do lazy, server: false and pending differ?

lazy: true does not block navigation: the page renders at once and pending is true until data arrives. server: false skips the fetch during SSR and runs it only in the browser (the content is missing from the HTML, which hurts SEO). pending and status describe the current request; always render a loading and an error state.

20. How do you refetch when something changes?

Call refresh() from the composable, pass watch: [source] so it re-runs when a ref changes, or use refreshNuxtData('key') to refresh from anywhere. For a reactive URL, pass a getter or computed (() => '/api/items?page=' + page.value), not a plain string.

Nitro and the server

21. What is Nitro?

Nitro is the server engine inside Nuxt. It bundles everything in server/ into one output, handles routing with the h3 HTTP toolkit, provides caching, storage and a $fetch that calls your own routes without a network hop on the server, and targets many platforms through presets (Node, static, serverless, edge). Nuxt and Nitro are built together. See server routes.

22. How do you create an API endpoint?

Add a file under server/api/ and export defineEventHandler. The file name sets the path and an optional suffix sets the method: server/api/posts/[id].get.ts answers GET /api/posts/:id. Return a value and Nitro sends it as JSON; throw createError({ statusCode: 404 }) for errors.

TypeScript
export default defineEventHandler(async (event) => {
  const id = getRouterParam(event, 'id')
  const post = await findPost(id)
  if (!post) {
    throw createError({ statusCode: 404, statusMessage: 'Post not found' })
  }
  return post
})

23. What is the difference between server/api and server/routes?

server/api/ files are served under the /api prefix; server/routes/ files are served at the path you write, which suits things like /sitemap.xml or /health. The handler code is the same.

24. How do runtime config and environment variables work?

runtimeConfig in nuxt.config.ts declares values, and top-level keys are server-only while keys under public are also sent to the browser. At runtime you override them with environment variables named NUXT_ plus the key in upper snake case (NUXT_API_SECRET, NUXT_PUBLIC_API_BASE) without rebuilding. Read them with useRuntimeConfig(). Never put a secret under public. See runtime config.

25. How do you cache a slow endpoint?

Wrap it with defineCachedEventHandler(handler, { maxAge: 60 }), or set routeRules: { '/api/stats': { cache: { maxAge: 60 } } }. Nitro stores the response in its storage layer (memory by default, other drivers configurable). Remember that a cached handler is shared by all users, so it must not depend on who is asking. See server middleware and utils.

Middleware, state and security

26. How does route middleware differ from server middleware?

Route middleware (app/middleware/) runs while navigating between pages, on the server for the first load and in the browser afterwards. It uses defineNuxtRouteMiddleware and can navigateTo or abortNavigation. Server middleware (server/middleware/) runs on every HTTP request before routes, including APIs and assets, and should not return a value unless it wants to end the request. See navigation and middleware.

27. How do you protect a page and an API?

Add a route middleware that checks the session and redirects (return navigateTo('/login')), then attach it with definePageMeta({ middleware: 'auth' }). This is only a convenience: someone can call your API directly, so the server route must check the session itself and throw a 401 or 403. Never rely on a page guard for security. See forms and auth flows.

28. What is useState and why not a module-level ref?

useState(key, init) is SSR-friendly shared state: it is scoped to the current request on the server, serialized into the payload and restored on the client. A ref declared at module level lives as long as the server process, so every visitor shares it: one user's data can leak into another user's page. The stand-in shows the leak. See state management.

JS Stand-in: shared module state versus per-request state

Press Run (or Ctrl + Enter) to see the console output here.

No output.

In the real framework the server keeps one process for many requests, which is exactly why the first block counts up while the second starts at 1 every time.

29. How do you handle cookies and request headers during SSR?

Use useCookie('token'), which reads the cookie from the request on the server and from document.cookie in the browser and keeps both in sync. To forward the visitor's headers to an internal API call, use useRequestHeaders(['cookie']). Inside server routes use getCookie(event, 'name') and setCookie.

30. What are the most common security mistakes?

Putting secrets in runtimeConfig.public, rendering untrusted HTML with v-html, trusting a page middleware instead of checking on the server, skipping validation of readBody and getQuery, forgetting rate limits on login routes, and returning whole database rows (password hashes included) from an API. See security and common mistakes.

Check your understanding

Quick check ยท 5 questions

of 5 answered You scored / 5 ยท perfect!

  1. 1. A page calls $fetch('/api/items') at the top of setup. What is the most likely problem?

    Top-level $fetch is not deduplicated: it runs during server rendering and again during hydration. useFetch and useAsyncData store the server result in the payload and reuse it.
  2. 2. Which cause of a hydration mismatch is correct?

    The server and the browser render at different moments, so the two values differ. Move such values into onMounted or wrap them in ClientOnly.
  3. 3. Why is a module-level ref risky in an SSR app?

    The server process handles many requests, so state outside a request is shared. Use useState, which is scoped to the request.
  4. 4. A route middleware redirects guests away from /account. Is the /api/account endpoint now protected?

    Route middleware only runs for page navigation. Anyone can call the API directly, so the handler must verify the session and answer 401 or 403.
  5. 5. How do you give one part of the site cached rendering and another a static page?

    routeRules maps path patterns to behavior such as prerender, swr and ssr, so one app can mix modes per route.

Summary

  • Most Nuxt questions come down to where code runs: server render, browser after hydration, or Nitro.
  • Hydration needs the first client render to equal the server HTML; browser-only values belong in onMounted or <ClientOnly>.
  • useFetch and useAsyncData reuse the server's answer through the payload; a bare $fetch in setup fetches twice.
  • useState is request-scoped, serialized state; module-level refs leak between visitors.
  • routeRules picks prerender, SWR, cache or SPA behavior per path.
  • Route middleware is a convenience for navigation; always check authorization inside server routes.

Next, keep the one-page reference handy: the Nuxt cheat sheet.