diff --git a/packages/root/src/cli/dev.ts b/packages/root/src/cli/dev.ts index 3ec99ecd..486cf199 100644 --- a/packages/root/src/cli/dev.ts +++ b/packages/root/src/cli/dev.ts @@ -5,6 +5,7 @@ import cookieParser from 'cookie-parser'; import {default as express} from 'express'; import {dim} from 'kleur/colors'; import sirv from 'sirv'; +import type {Options as SirvOptions} from 'sirv'; import glob from 'tiny-glob'; import {ViteDevServer} from 'vite'; @@ -111,7 +112,13 @@ export async function createDevServer(options?: { // Add static file middleware. const publicDir = path.join(rootDir, 'public'); if (await dirExists(publicDir)) { - server.use(rootPublicDirMiddleware({publicDir, viteServer})); + const sirvOptions = Object.assign( + {dev: false}, + rootConfig.server?.sirvOptions || {} + ); + server.use( + rootPublicDirMiddleware({publicDir, viteServer, sirvOptions}) + ); } // NOTE: The trailing slash middleware needs to come after public files so @@ -228,6 +235,7 @@ async function createViteMiddleware(options: { function rootPublicDirMiddleware(options: { publicDir: string; viteServer: ViteDevServer; + sirvOptions: SirvOptions; }) { const publicDir = options.publicDir; @@ -235,7 +243,7 @@ function rootPublicDirMiddleware(options: { // to `true`, every request will traverse the filesystem to check if a // matching file exists. Setting it to `false` uses a cache, which can be // reloaded whenever a file change is detected in the `public` directory. - const sirvOptions = {dev: false}; + const sirvOptions = options.sirvOptions; let handler = sirv(publicDir, sirvOptions); const reloadPublicDirCache = debounce(() => { diff --git a/packages/root/src/cli/preview.ts b/packages/root/src/cli/preview.ts index f4be09f2..66940195 100644 --- a/packages/root/src/cli/preview.ts +++ b/packages/root/src/cli/preview.ts @@ -97,7 +97,11 @@ export async function createPreviewServer(options: { // Add static file middleware. const publicDir = path.join(distDir, 'html'); - server.use(sirv(publicDir, {dev: false})); + const sirvOptions = Object.assign( + {dev: false}, + rootConfig.server?.sirvOptions || {} + ); + server.use(sirv(publicDir, sirvOptions)); // NOTE: The trailing slash middleware needs to come after public files so // that slashes are not appended to public file routes. diff --git a/packages/root/src/cli/start.ts b/packages/root/src/cli/start.ts index 44612ad2..4e25d4a4 100644 --- a/packages/root/src/cli/start.ts +++ b/packages/root/src/cli/start.ts @@ -92,7 +92,11 @@ export async function createProdServer(options: { // Add static file middleware. const publicDir = path.join(distDir, 'html'); - server.use(sirv(publicDir, {dev: false})); + const sirvOptions = Object.assign( + {dev: false}, + rootConfig.server?.sirvOptions || {} + ); + server.use(sirv(publicDir, sirvOptions)); // NOTE: The trailing slash middleware needs to come after public files so // that slashes are not appended to public file routes. diff --git a/packages/root/src/core/config.ts b/packages/root/src/core/config.ts index 4e20d42e..ae9fbf18 100644 --- a/packages/root/src/core/config.ts +++ b/packages/root/src/core/config.ts @@ -3,6 +3,7 @@ import {HtmlMinifyOptions} from '../render/html-minify.js'; import {HtmlPrettyOptions} from '../render/html-pretty.js'; import {Plugin} from './plugin.js'; import {RequestMiddleware} from './types.js'; +import type {Options as SirvOptions} from 'sirv'; export interface RootUserConfig { /** @@ -270,6 +271,12 @@ export interface RootServerConfig { * Home page URL path, which is printed when the dev server starts. */ homePagePath?: string; + + /** + * Options passed to sirv when serving static files. These options are merged + * with Root.js defaults. + */ + sirvOptions?: SirvOptions; } export function defineConfig(config: RootUserConfig): RootUserConfig {