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 17 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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` if using Vite's [SSR](/guide/ssr#vite-cli) functionality.

## 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.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 mode.
10 changes: 9 additions & 1 deletion packages/vite/src/node/config.ts
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`
* if using Vite's SSR functionality.
* @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
5 changes: 4 additions & 1 deletion packages/vite/src/node/preview.ts
@@ -1,5 +1,6 @@
import path from 'path'
import type * as http from 'http'
import fs from 'fs'
brillout marked this conversation as resolved.
Show resolved Hide resolved
import sirv from 'sirv'
import connect from 'connect'
import type { Connect } from 'types/connect'
Expand Down Expand Up @@ -97,14 +98,16 @@ export async function preview(

app.use(compression())

const { spa } = config

// static assets
const distDir = path.resolve(config.root, config.build.outDir)
app.use(
config.base,
sirv(distDir, {
etag: true,
dev: true,
single: true
single: spa
})
)

Expand Down
9 changes: 7 additions & 2 deletions packages/vite/src/node/server/index.ts
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