Skip to content

vite preview does not set correct mime types for some static files (e.g. xml) #10839

@jasongitmail

Description

@jasongitmail

Describe the bug

When a prerendered route's static file is served via vite preview, the content type is text/html and not what is expected, in at least a few scenarios:

  • sitemap.xml should be application/xml
  • manifest.webmanifest should be application/manifest+json
  • atom.xml and arguably feed.xml should be application/atom+xml
  • rss.xml should be application/rss+xml

Possibly others, but those are the only I'm aware of.

Given this is for the preview server, imo it'd be fine to simplify to:

  • *.xml -> application/xml
  • *.webmanifest -> application/manifest+json

Reproduction

  1. npm i super-sitemap
  2. Create this route:
// /src/routes/sitemap.xml/+server.ts
import * as sitemap from 'super-sitemap';
import type { RequestHandler } from '@sveltejs/kit';

export const prerender = true;

export const GET: RequestHandler = async () => {
  return await sitemap.response({
    origin: 'https://example.com'
  });
};
  1. npm run build && npm run preview

Visit http://localhost:4173/sitemap.xml, open web inspector and observe content type is text/html, not application/xml as expected.

Or

// /src/routes/sitemap.xml/+server.js

export const prerender = true;

export async function GET() {
	return new Response(
		`
		<?xml version="1.0" encoding="UTF-8" ?>
		<urlset
			xmlns="https://www.sitemaps.org/schemas/sitemap/0.9"
			xmlns:xhtml="https://www.w3.org/1999/xhtml"
			xmlns:mobile="https://www.google.com/schemas/sitemap-mobile/1.0"
			xmlns:news="https://www.google.com/schemas/sitemap-news/0.9"
			xmlns:image="https://www.google.com/schemas/sitemap-image/1.1"
			xmlns:video="https://www.google.com/schemas/sitemap-video/1.1"
		>
			<url>
                            <loc>https://example.com/</loc>
                         </url>
		</urlset>`.trim(),
		{
			headers: {
				'Content-Type': 'application/xml'
			}
		}
	);
}

Logs

No response

System Info

npmPackages:
    @sveltejs/adapter-cloudflare: ^2.3.3 => 2.3.3 
    @sveltejs/kit: ^1.25.1 => 1.25.1 
    svelte: ^4.2.1 => 4.2.1 
    vite: ^4.4.9 => 4.4.10

Severity

annoyance

Additional Information

No response

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugSomething isn't working

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions