Which folder to deploy
nuxt generate (or nuxt build --prerender) writes the site to
.output/public/. It also creates dist/ as a link to that folder, which
some hosts follow and some upload as an empty link. Point the host, the
artifact upload or the zip at .output/public/ itself.
nuxt build without prerendering produces a Node server in .output/server/.
Uploading .output/public/ from that build gives assets with no HTML, and
every page 404s.
On Nuxt 2 the command is the same but the output is dist/, the setting is
target: 'static', and the base path is router.base.
You opened .output/public/index.html from disk
Look at the source of any generated page:
<link rel="stylesheet" href="/_nuxt/entry.Dh3k.css" />
<script type="module" src="/_nuxt/BfY2a9.js" crossorigin></script>
On a web server /_nuxt/ is under the site root. On a file:/// page it is
under the root of your disk, so every stylesheet and script fails with
ERR_FILE_NOT_FOUND. Two more things stop it even if you fix the paths: the
scripts are ES modules, which browsers won't load from a file:// origin, and
client-side navigation fetches _payload.json files, which
a file:// page can't fetch.
ssr: true (default): the prerendered text shows. Component styles that
Nuxt inlined may apply; global CSS doesn't. Nothing is interactive.
ssr: false: index.html is an empty <div id="__nuxt"> and stays
blank.
There is no relative-path mode to switch on; app.baseURL: './' is not
supported. Serve the folder over HTTP:
npx serve .output/public
GitHub Pages: _nuxt 404s and app.baseURL
Two separate things break a Nuxt site on GitHub Pages.
Jekyll eats every underscore. When Pages publishes from a branch it runs
Jekyll, which skips files and folders starting with _. That removes
_nuxt/ (every script and stylesheet) and each page's _payload.json (the
data client-side navigation loads). Either add an empty file that the build
copies across:
touch public/.nojekyll
or build with the Nitro preset for Pages, which writes .nojekyll and a
404.html for you:
NITRO_PRESET=github_pages npx nuxt generate
Deploying with the actions/deploy-pages workflow skips Jekyll, so the first
fix only matters for branch deploys.
A project site lives below the root. https://user.github.io/repo/ asks
for https://user.github.io/_nuxt/… and gets a 404. Set the base URL at build
time:
NUXT_APP_BASE_URL=/repo/ npx nuxt generate
or in config:
// nuxt.config.ts
export default defineNuxtConfig({
app: { baseURL: "/repo/" },
});
Unlike Next.js and SvelteKit, Nuxt's base URL keeps its trailing slash. It
prefixes <NuxtLink> routes and the _nuxt assets. It does not prefix
paths you wrote by hand:
| Written as |
With baseURL: "/repo/" it requests |
Fix |
<a href="/about"> |
/about (404) |
<NuxtLink to="/about"> |
<img src="/logo.png"> |
/logo.png (404) |
Put it in assets/ and import, or prefix it |
CSS url(/fonts/x.woff2) |
/fonts/x.woff2 (404) |
url(~/assets/fonts/x.woff2) |
fetch("/data.json") |
/data.json (404) |
$fetch(`${useRuntimeConfig().app.baseURL}data.json`) |
With a custom domain the site is at / again, so drop the base URL or every
asset 404s.
Routes the crawler missed
The home page and anything it links to are generated. A page nothing links to
isn't, and it 404s on a static host.
The prerenderer starts at / and follows links. Product pages loaded from an
API, posts only reachable through search, and routes behind a button click
are never found. List them:
// nuxt.config.ts
export default defineNuxtConfig({
nitro: {
prerender: {
crawlLinks: true,
routes: ["/blog/new-post", "/sitemap.xml"],
},
},
routeRules: {
"/docs/**": { prerender: true },
},
});
For a long list, fetch it in a prerender:routes hook in nuxt.config.ts and
add each route to ctx.routes. Check what was generated by listing
.output/public/ after the build.
With ssr: false every route uses the same shell, so the host has to
answer unknown paths with it. Nuxt writes 200.html and 404.html for this.
Netlify and Surge pick up 200.html; GitHub Pages serves 404.html; nginx
needs try_files $uri $uri/ /200.html;; S3 and CloudFront need the error
document set to 200.html with a 200 response.
Nitro writes prerendered pages as about/index.html, so refreshing a
prerendered route works on every static host without extra config.
Hydration mismatch
[Vue warn]: Hydration completed but contains mismatches.
The HTML written at build time differs from the first render in the browser,
so Vue patches the page. Usually that only flickers. It blanks the page when
the patch throws, or when the difference is a whole component. Common causes
on a generated site:
- Values that differ between build and visit:
new Date(),
Math.random(), the visitor's locale or time zone. Compute them in
onMounted, or wrap the component in <ClientOnly>.
- Browser-only APIs during setup:
window, localStorage or document
read at the top of <script setup> throw during prerendering. Guard with
if (import.meta.client) or move them into onMounted.
- Invalid HTML nesting: a
<div> inside a <p>, or a <tr> without
<tbody>. The browser repairs the markup before Vue hydrates, so the DOM no
longer matches.
The console names the component in development; rebuild with
sourcemap: { client: true } to get real file names in production.
Stale chunks after a deploy
TypeError: Failed to fetch dynamically imported module:
https://example.com/_nuxt/Cq7xY2.js
A visitor's tab (or a CDN) still holds HTML that imports hashed chunks the new
build replaced. Nuxt catches this by default and reloads the page on the next
navigation (experimental.emitRouteChunkError: 'automatic'). If it keeps
happening, the HTML is cached: serve HTML with Cache-Control: no-cache and
_nuxt/ with a long immutable cache.
If the Network tab shows the chunk came back as HTML with a 200, the path is
wrong instead: a host with an SPA fallback answers a missing file with the
shell. See the baseURL section.
Server routes and config
A generated site is files. These need a running server:
server/api/ routes. A useFetch("/api/products") that runs during
prerendering is called once at build time, and the result is saved in the
page payload. The same call made only in the browser, in onMounted or on a
click, requests an endpoint that doesn't exist. Point it at an external API,
or fetch the data at build time.
runtimeConfig. On a server, NUXT_PUBLIC_API_BASE overrides
runtimeConfig.public.apiBase at request time. On a static host nothing
runs at request time, so the value from the build is final. Set the
variable in the CI job that runs nuxt generate; setting it in the host's
dashboard afterwards changes nothing.
- Server middleware,
useCookie set on the server, redirects in
routeRules need a server or the host's own redirect rules.
Stop sending the output folder
Every file-based fix above has a ceiling: the person you send
.output/public/ to still needs a local server, because every asset path
starts at /_nuxt/. A build preview works best as a URL.
Comma takes the folder or a zip of it and returns one link. Assets upload
alongside the HTML with their references rewritten, so the _nuxt,
baseURL and file:// problems don't apply, and reviewers comment on the
exact element they mean instead of sending a screenshot.
Share a Nuxt static site has the steps.
Two honest caveats. Reports run in a sandboxed iframe with scripts enabled but
no same-origin access, so code that reads localStorage or cookies without a
try/catch throws there. And leave app.baseURL at its default for a
preview build; it bakes in a path the preview isn't served from.
Publish a build preview →
Related