Skip to content

Commit

Permalink
feat: spa option, preview and dev for MPA and SSR apps (#8217)
Browse files Browse the repository at this point in the history
Co-authored-by: Ben McCann <322311+benmccann@users.noreply.github.com>
  • Loading branch information
brillout and benmccann committed May 23, 2022
1 parent 015ebed commit d7cba46
Show file tree
Hide file tree
Showing 5 changed files with 35 additions and 4 deletions.
7 changes: 7 additions & 0 deletions docs/config/index.md
Expand Up @@ -462,6 +462,13 @@ export default defineConfig(({ command, mode }) => {
`envPrefix` should not be set as `''`, which will expose all your env variables and cause unexpected leaking of of sensitive information. Vite will throw error when detecting `''`.
:::

### spa

- **Type:** `boolean`
- **Default:** `true`

Whether your application is a Single Page Application (SPA). Set to `false` for other kinds of apps like MPAs. Learn more in Vite's [SSR guide](/guide/ssr#vite-cli).

## Server Options

### server.host
Expand Down
11 changes: 11 additions & 0 deletions docs/guide/ssr.md
Expand Up @@ -264,3 +264,14 @@ In some cases like `webworker` runtimes, you might want to bundle your SSR build
- Treat all dependencies as `noExternal`
- Throw an error if any Node.js built-ins are imported
## Vite CLI
The CLI commands `$ vite dev` and `$ vite preview` can also be used for SSR apps:
1. Add your SSR middleware to the development server with [`configureServer`](/guide/api-plugin#configureserver) and to the preview server with [`configurePreviewServer`](/guide/api-plugin#configurepreviewserver).
:::tip Note
Use a post hook so that your SSR middleware runs _after_ Vite's middlewares.
:::
2. Set `config.spa` to `false`. This switches the development and preview server from SPA mode to SSR/MPA mode.
10 changes: 9 additions & 1 deletion packages/vite/src/node/config.ts
Expand Up @@ -208,6 +208,12 @@ export interface UserConfig {
'plugins' | 'input' | 'onwarn' | 'preserveEntrySignatures'
>
}
/**
* Whether your application is a Single Page Application (SPA). Set to `false`
* for other kinds of apps like MPAs.
* @default true
*/
spa?: boolean
}

export interface ExperimentalOptions {
Expand Down Expand Up @@ -274,6 +280,7 @@ export type ResolvedConfig = Readonly<
/** @internal */
packageCache: PackageCache
worker: ResolveWorkerOptions
spa: boolean
}
>

Expand Down Expand Up @@ -514,7 +521,8 @@ export async function resolveConfig(
...optimizeDeps.esbuildOptions
}
},
worker: resolvedWorkerOptions
worker: resolvedWorkerOptions,
spa: config.spa ?? true
}

// flat config.worker.plugin
Expand Down
2 changes: 1 addition & 1 deletion packages/vite/src/node/preview.ts
Expand Up @@ -104,7 +104,7 @@ export async function preview(
sirv(distDir, {
etag: true,
dev: true,
single: true
single: config.spa
})
)

Expand Down
9 changes: 7 additions & 2 deletions packages/vite/src/node/server/index.ts
Expand Up @@ -508,8 +508,10 @@ export async function createServer(
middlewares.use(serveRawFsMiddleware(server))
middlewares.use(serveStaticMiddleware(root, server))

const isMiddlewareMode = middlewareMode && middlewareMode !== 'html'

// spa fallback
if (!middlewareMode || middlewareMode === 'html') {
if (config.spa && !isMiddlewareMode) {
middlewares.use(spaFallbackMiddleware(root))
}

Expand All @@ -518,9 +520,12 @@ export async function createServer(
// serve custom content instead of index.html.
postHooks.forEach((fn) => fn && fn())

if (!middlewareMode || middlewareMode === 'html') {
if (config.spa && !isMiddlewareMode) {
// transform index.html
middlewares.use(indexHtmlMiddleware(server))
}

if (!isMiddlewareMode) {
// handle 404s
// Keep the named function. The name is visible in debug logs via `DEBUG=connect:dispatcher ...`
middlewares.use(function vite404Middleware(_, res) {
Expand Down

0 comments on commit d7cba46

Please sign in to comment.