Skip to content
Source
Sign inSign up
TanStack/router

sitemap not being served from nitro build #6587

Openisc30(GitHub profile, opens in a new tab) opened this issue 11 comments

Which project does this relate to?

Start

Describe the bug

When generating a sitemap with nitro, the production server throws a 404 on /sitemap.xml.

Your Example Website or App

.

Steps to Reproduce the Bug or Issue

vite.config.js

nitro({ preset: 'node-server' }),
tanstackStart({
    prerender: {
      enabled: true,
      crawlLinks: false,
    },
    sitemap: {
      enabled: true,
      host: 'https://example.com',
    },

Expected behavior

npm run start serves the built application and /sitemap.xml works

Screenshots or Videos

The server.mjs has this list of assets that is missing to include sitemap.xml:

const assets = {
  "/favicon.png": {
    "type": "image/png",
    "etag": '"58d2-yz8RygUU2g7H6U101cbusIN4uDA"',
    "mtime": "2026-02-02T08:11:51.383Z",
    "size": 22738,
    "path": "../public/favicon.png"
  },
  ...

Platform

  • Router / Start Version: [e.g. 1.121.0]
  • OS: [e.g. macOS, Windows, Linux]
  • Browser: [e.g. Chrome, Safari, Firefox]
  • Browser Version: [e.g. 91.1]
  • Bundler: [e.g. vite]
  • Bundler Version: [e.g. 7.0.0]

Additional context

No response

  1. isc30(GitHub profile, opens in a new tab)commented

    This has nothing to do with prerendering errors, the sitemap is generated properly. It's about the sitemap.xml asset not being included in nitro assets

  2. I noticed its not just the sitemap, also the pre-rendered pages (html) are not being served

  3. please provide a complete minimal reproducer project

  4. schiller-manuel(GitHub profile, opens in a new tab) added labelsinformation needed

  5. marekhanzlik(GitHub profile, opens in a new tab)commented

    Observing same behaviour. When built with pnpm run build and nitro() plugin in vite.config.ts, and then run with node output/server/index.mjs, i can access favicon, but not sitemap.xml althought they are both in /public folder

    When build with pnpm run build and previewed with vite preview the sitemap.xml works as expected so it must be somewhere in between these layers

    Edit:

    • Nitro Version: 3.0.1-20260225-084212-c0b46421 │
    • Nitro Preset: node-server
  6. Weird is that when i look into built index.mjs. There is not an entry for sitemap.xml in const assets={

    Only favicon, javascripts, site.webmanifest, robots.txt. But no sitemap

  7. I think its because sitemap.xml is created after nitro build, in prerender step (Which is last in build). So it does not know about sitemap.xml (cause it does not exist yet)

    I've created empty public/sitemap.xml and it works correctly then! So this is the workaround @isc30. It gets replaced by prerendered content in build. But nitro now knows about this public file

  8. @marekhanzlik haha great workaround! It doesn't work for pre-rendered pages tho.

    Hoping we can get this working by default

  9. marekhanzlik(GitHub profile, opens in a new tab)commented

    @schiller-manuel based on these findings. Minimal project should be anything with prerender and sitemap enabled

  10. I had this same problem so I had AI help me write this script to run after the build to patch the assets object with all the public files that are missing.

    import crypto from 'node:crypto';
    import fs from 'node:fs';
    import path from 'node:path';
    
    const serverEntry = './.output/server/index.mjs';
    const publicDir = './.output/public';
    
    // Nitro/h3 hex ETag algorithm: "hex(size)-hex(mtime)"
    function getNitroEtag(content) {
    	const size = content.length.toString(16);
    	const hash = crypto
    		.createHash('sha1')
    		.update(content)
    		.digest('base64')
    		// Nitro/h3 removes the base64 padding '=' characters
    		.replace(/=+$/, '');
    
    	return `"${size}-${hash}"`;
    }
    
    // Map common extensions to MIME types
    const mimeTypes = {
    	'.html': 'text/html',
    	'.xml': 'application/xml',
    	'.txt': 'text/plain',
    	'.json': 'application/json',
    	'.png': 'image/png',
    	'.ico': 'image/x-icon',
    	'.svg': 'image/svg+xml',
    	'.js': 'text/javascript',
    	'.css': 'text/css',
    	'.map': 'application/json',
    	'.webp': 'image/webp',
    	'.woff2': 'font/woff2',
    	'.woff': 'font/woff',
    	'.webmanifest': 'application/manifest+json',
    };
    
    function getAllFiles(dirPath, arrayOfFiles = []) {
    	const files = fs.readdirSync(dirPath);
    	files.forEach((file) => {
    		if (fs.statSync(path.join(dirPath, file)).isDirectory()) {
    			arrayOfFiles = getAllFiles(path.join(dirPath, file), arrayOfFiles);
    		} else {
    			arrayOfFiles.push(path.join(dirPath, file));
    		}
    	});
    	return arrayOfFiles;
    }
    
    if (fs.existsSync(serverEntry) && fs.existsSync(publicDir)) {
    	let serverContent = fs.readFileSync(serverEntry, 'utf8');
    	const assetRegex = /const assets = \{([\s\S]*?)\};/;
    	const match = serverContent.match(assetRegex);
    
    	if (match) {
    		// Try to parse existing assets to avoid total overwrite if possible, 
    		// but regex-based replacement is safer for raw JS files.
    		let updatedAssetsString = match[1];
    
    		const allPublicFiles = getAllFiles(publicDir);
    
    		allPublicFiles.forEach((fullPath) => {
    			const content = fs.readFileSync(fullPath);
    			const stats = fs.statSync(fullPath);
    			const relativePath = path.relative(publicDir, fullPath).replace(/\\/g, '/');
    			const fileKey = `/${relativePath}`;
    			const ext = path.extname(fullPath);
    
    			if (!(ext in mimeTypes)) {
    				throw new Error(`Unknown file extension: ${ext}`);
    			}
    			const entryRegex = new RegExp(`"${fileKey.replace(/\./g, '\\.')}":\\s*\\{[^}]+\\}`);
    			if (entryRegex.test(updatedAssetsString)) {
    				return;
    			}
    
    			const newEntry = `"${fileKey}": {
        "type": "${mimeTypes[ext] || 'application/octet-stream'}",
        "etag": '${getNitroEtag(content)}',
        "mtime": "${stats.mtime.toISOString()}",
        "size": ${stats.size},
        "path": "../public/${relativePath}"
      }`;
    
    			updatedAssetsString += `,\n  ${newEntry}`;
    		});
    
    		const finalContent = serverContent.replace(assetRegex, `const assets = {${updatedAssetsString}\n};`);
    		fs.writeFileSync(serverEntry, finalContent);
    	}
    }
  11. same issue on my side, @marekhanzlik fix works

  12. I'm experiencing the same issue. Thanks for your workaround @marekhanzlik, it works like a charm :)

Sign in to join this conversation.