Skip to content

Commit

Permalink
chore: give history middleware a name (#4908)
Browse files Browse the repository at this point in the history
* chore: give history middleware a name

* chore: move spa fallback middleware to new file

* Update packages/vite/src/node/server/middlewares/spaFallback.ts

Co-authored-by: patak <matias.capeletto@gmail.com>

* fix: typo

* chore: better naming for impl function

Co-authored-by: patak <matias.capeletto@gmail.com>
  • Loading branch information
benmccann and patak-dev committed Sep 13, 2021
1 parent 015290a commit bfbafee
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 21 deletions.
24 changes: 3 additions & 21 deletions packages/vite/src/node/server/index.ts
Expand Up @@ -15,12 +15,12 @@ import { FSWatcher, WatchOptions } from 'types/chokidar'
import { createWebSocketServer, WebSocketServer } from './ws'
import { baseMiddleware } from './middlewares/base'
import { proxyMiddleware, ProxyOptions } from './middlewares/proxy'
import { spaFallbackMiddleware } from './middlewares/spaFallback'
import { transformMiddleware } from './middlewares/transform'
import {
createDevHtmlTransformFn,
indexHtmlMiddleware
} from './middlewares/indexHtml'
import history from 'connect-history-api-fallback'
import {
serveRawFsMiddleware,
servePublicMiddleware,
Expand All @@ -29,7 +29,7 @@ import {
import { timeMiddleware } from './middlewares/time'
import { ModuleGraph, ModuleNode } from './moduleGraph'
import { Connect } from 'types/connect'
import { createDebugger, ensureLeadingSlash, normalizePath } from '../utils'
import { ensureLeadingSlash, normalizePath } from '../utils'
import { errorMiddleware, prepareError } from './middlewares/error'
import { handleHMRUpdate, HmrOptions, handleFileAddUnlink } from './hmr'
import { openBrowser } from './openBrowser'
Expand Down Expand Up @@ -502,25 +502,7 @@ export async function createServer(

// spa fallback
if (!middlewareMode || middlewareMode === 'html') {
middlewares.use(
history({
logger: createDebugger('vite:spa-fallback'),
// support /dir/ without explicit index.html
rewrites: [
{
from: /\/$/,
to({ parsedUrl }: any) {
const rewritten = parsedUrl.pathname + 'index.html'
if (fs.existsSync(path.join(root, rewritten))) {
return rewritten
} else {
return `/index.html`
}
}
}
]
})
)
middlewares.use(spaFallbackMiddleware(root))
}

// run post config hooks
Expand Down
32 changes: 32 additions & 0 deletions packages/vite/src/node/server/middlewares/spaFallback.ts
@@ -0,0 +1,32 @@
import fs from 'fs'
import history from 'connect-history-api-fallback'
import path from 'path'
import { Connect } from 'types/connect'
import { createDebugger } from '../../utils'

export function spaFallbackMiddleware(
root: string
): Connect.NextHandleFunction {
const historySpaFallbackMiddleware = history({
logger: createDebugger('vite:spa-fallback'),
// support /dir/ without explicit index.html
rewrites: [
{
from: /\/$/,
to({ parsedUrl }: any) {
const rewritten = parsedUrl.pathname + 'index.html'
if (fs.existsSync(path.join(root, rewritten))) {
return rewritten
} else {
return `/index.html`
}
}
}
]
})

// Keep the named function. The name is visible in debug logs via `DEBUG=connect:dispatcher ...`
return function viteSpaFallbackMiddleware(req, res, next) {
return historySpaFallbackMiddleware(req, res, next)
}
}

0 comments on commit bfbafee

Please sign in to comment.