Skip to content

Commit

Permalink
fix: revert SPA mode while keeping SSR mode as-is
Browse files Browse the repository at this point in the history
  • Loading branch information
brillout committed May 22, 2022
1 parent bcfda16 commit 40d275e
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 19 deletions.
13 changes: 3 additions & 10 deletions packages/vite/src/node/preview.ts
Expand Up @@ -100,12 +100,6 @@ export async function preview(

const { spa } = config

if (spa) {
// We need to apply the plugins' server post hooks before `sirv()`. (Because
// `sirv(_, { single: true })` catches all routes.)
postHooks.forEach((fn) => fn && fn())
}

// static assets
const distDir = path.resolve(config.root, config.build.outDir)
app.use(
Expand All @@ -117,11 +111,10 @@ export async function preview(
})
)

if (!spa) {
// We apply the plugins' server post hooks after `sirv()` so that `sirv()`
// can serve pre-rendered pages before any SSR middleware.
postHooks.forEach((fn) => fn && fn())
// apply post server hooks from plugins
postHooks.forEach((fn) => fn && fn())

if (!spa) {
// 404 handling (this simulates what most static hosts do)
app.use(config.base, (_, res, next) => {
const file = path.join(distDir, './404.html')
Expand Down
19 changes: 10 additions & 9 deletions packages/vite/src/node/server/index.ts
Expand Up @@ -503,23 +503,24 @@ export async function createServer(
middlewares.use(serveRawFsMiddleware(server))
middlewares.use(serveStaticMiddleware(root, server))

// We need to apply post server hooks before `spaFallbackMiddleware()`.
// (Because `spaFallbackMiddleware()` catches all routes.)
postHooks.forEach((fn) => fn && fn())

const isMiddlewareModeSSR = middlewareMode && middlewareMode !== 'html'

if (config.spa && !isMiddlewareModeSSR) {
// SPA catch-all fallback routing
// spa fallback
middlewares.use(spaFallbackMiddleware(root))
}

// run post config hooks
// This is applied before the html middleware so that user middleware can
// serve custom content instead of index.html.
postHooks.forEach((fn) => fn && fn())

if (config.spa && !isMiddlewareModeSSR) {
// transform index.html
middlewares.use(indexHtmlMiddleware(server))
}

// Handle 404s.
// We keep 404 handling when `config.spa === false` because some SSR tools,
// such as vite-plugin-ssr, cannot always render 404 pages. (E.g. if the
// vite-plugin-ssr user didn't define a `_error.page.js`.)
// handle 404s
if (!isMiddlewareModeSSR) {
// Keep the named function. The name is visible in debug logs via
// `DEBUG=connect:dispatcher ...`.
Expand Down

0 comments on commit 40d275e

Please sign in to comment.