Skip to content
Open
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
12 changes: 10 additions & 2 deletions packages/root/src/cli/dev.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -228,14 +235,15 @@ async function createViteMiddleware(options: {
function rootPublicDirMiddleware(options: {
publicDir: string;
viteServer: ViteDevServer;
sirvOptions: SirvOptions;
}) {
const publicDir = options.publicDir;

// The `{dev: false}` option is used for performance reasons. When dev is set
// 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(() => {
Expand Down
6 changes: 5 additions & 1 deletion packages/root/src/cli/preview.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
6 changes: 5 additions & 1 deletion packages/root/src/cli/start.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
7 changes: 7 additions & 0 deletions packages/root/src/core/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
/**
Expand Down Expand Up @@ -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 {
Expand Down