diff --git a/site/vite.config.js b/site/vite.config.js index dbc81e53a..1e6895791 100644 --- a/site/vite.config.js +++ b/site/vite.config.js @@ -1,5 +1,6 @@ // `npm run dev`: vite serves site/, rebuilding the docs and reloading the page whenever // docs/, docs.css or build-docs.mjs change. A failed build shows in vite's error overlay. +import { existsSync } from 'node:fs'; import { join } from 'node:path'; import { defineConfig } from 'vite'; import { build } from './build-docs.mjs'; @@ -24,7 +25,18 @@ function docs() { async configureServer(server) { server.watcher.add(sources); server.watcher.on('all', (_, file) => sources.some((source) => file.startsWith(source)) && rebuild(server)); - server.httpServer?.once('listening', () => setTimeout(() => server.config.logger.info(` docs: ${server.resolvedUrls?.local[0]}docs/`))); + // Like the static host: a folder's address without its slash redirects to the folder. + server.middlewares.use((request, response, next) => { + const path = request.url.split('?')[0]; + if (path.endsWith('/') || path.includes('.') || !existsSync(join(here, path, 'index.html'))) return next(); + response.writeHead(301, { location: request.url.replace(path, `${path}/`) }).end(); + }); + server.httpServer?.once('listening', () => + setTimeout(() => { + const urls = [...(server.resolvedUrls?.local ?? []), ...(server.resolvedUrls?.network ?? [])]; + urls.forEach((url) => server.config.logger.info(` docs: ${url}docs/`)); + }), + ); await rebuild(server); }, }; @@ -32,6 +44,8 @@ function docs() { export default defineConfig({ plugins: [docs()], + // Separate static pages, not an app: an unknown address is a 404, not the landing page. + appType: 'mpa', // The built pages change on every rebuild; the plugin reloads once the whole build is written. server: { watch: { ignored: [join(here, 'docs', '**')] } }, });