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: spa option, preview and dev for MPA and SSR apps #8217

Merged
merged 21 commits into from
May 23, 2022
Merged
Show file tree
Hide file tree
Changes from 20 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
7 changes: 7 additions & 0 deletions docs/config/index.md
Original file line number Diff line number Diff line change
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).
patak-dev marked this conversation as resolved.
Show resolved Hide resolved

## Server Options

### server.host
Expand Down
11 changes: 11 additions & 0 deletions docs/guide/ssr.md
Original file line number Diff line number Diff line change
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.html#configureserver) and to the preview server with [`configurePreviewServer`](/guide/api-plugin.html#configurepreviewserver).
patak-dev marked this conversation as resolved.
Show resolved Hide resolved
:::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
Original file line number Diff line number Diff line change
Expand Up @@ -204,6 +204,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 @@ -270,6 +276,7 @@ export type ResolvedConfig = Readonly<
/** @internal */
packageCache: PackageCache
worker: ResolveWorkerOptions
spa: boolean
}
>

Expand Down Expand Up @@ -510,7 +517,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
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
Expand Up @@ -503,8 +503,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 @@ -513,9 +515,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
brillout marked this conversation as resolved.
Show resolved Hide resolved
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