Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: preserve prerendered asset content-type header #1587

Merged
merged 3 commits into from
Aug 16, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 11 additions & 2 deletions src/prerender.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import { joinURL, parseURL, withBase, withoutBase } from "ufo";
import chalk from "chalk";
import { createRouter as createRadixRouter, toRouteMatcher } from "radix3";
import { defu } from "defu";
import mime from "mime";
import { createNitro } from "./nitro";
import { build } from "./build";
import type { Nitro, NitroRouteRules, PrerenderRoute } from "./types";
Expand Down Expand Up @@ -49,6 +50,7 @@ export async function prerender(nitro: Nitro) {
// Build with prerender preset
nitro.logger.info("Initializing prerenderer");
nitro._prerenderedRoutes = [];
nitro._prerenderMeta = nitro._prerenderMeta || {};
const prerendererConfig = {
...nitro.options._config,
static: false,
Expand Down Expand Up @@ -208,17 +210,24 @@ export async function prerender(nitro: Nitro) {
_route.generateTimeMS = Date.now() - start;

// Guess route type and populate fileName
const contentType = res.headers.get("content-type") || "";
const isImplicitHTML =
!route.endsWith(".html") &&
(res.headers.get("content-type") || "").includes("html");
!route.endsWith(".html") && contentType.includes("html");
const routeWithIndex = route.endsWith("/") ? route + "index" : route;
_route.fileName = withoutBase(
isImplicitHTML ? joinURL(route, "index.html") : routeWithIndex,
nitro.options.baseURL
);
// Allow overriding content-type in `prerender:generate` hook
const inferredContentType = mime.getType(_route.fileName) || "text/plain";
_route.contentType = contentType || inferredContentType;

// Allow hooking before generate
await nitro.hooks.callHook("prerender:generate", _route, nitro);
if (_route.contentType !== inferredContentType) {
nitro._prerenderMeta[_route.fileName] ||= {};
nitro._prerenderMeta[_route.fileName].contentType = _route.contentType;
}

// Check if route is skipped or has errors
if (_route.skip || _route.error) {
Expand Down
2 changes: 1 addition & 1 deletion src/rollup/plugins/public-assets.ts
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ export function publicAssets(nitro: Nitro): Plugin {
}

assets[assetId] = {
type: mimeType,
type: nitro._prerenderMeta?.[id]?.contentType || mimeType,
encoding,
etag,
mtime: stat.mtime.toJSON(),
Expand Down
2 changes: 2 additions & 0 deletions src/types/nitro.ts
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,7 @@ export interface Nitro {

/* @internal */
_prerenderedRoutes?: PrerenderRoute[];
_prerenderMeta?: Record<string, { contentType?: string }>;
}

export interface PrerenderRoute {
Expand All @@ -70,6 +71,7 @@ export interface PrerenderRoute {
error?: Error & { statusCode: number; statusMessage: string };
generateTimeMS?: number;
skip?: boolean;
contentType?: string;
}

/** @deprecated Internal type will be removed in future versions */
Expand Down