history 主要使用的是 connect-history-api-fallback 库来让单页面应用能通过不同的 URL 正常访问
其实其作用过类似于 nginx 的 rewrite /index.html,只不过 vite 做了另外一个处理,就是如果真的通过 url 找到一个文件了,就不重定向至 /index.html

  1. middlewares.use(
  2. history({
  3. logger: createDebugger('vite:spa-fallback'),
  4. // support /dir/ without explicit index.html
  5. rewrites: [
  6. {
  7. from: /\/$/,
  8. to({ parsedUrl }: any) {
  9. const rewritten = parsedUrl.pathname + 'index.html'
  10. if (fs.existsSync(path.join(root, rewritten))) {
  11. return rewritten
  12. } else {
  13. return `/index.html`
  14. }
  15. }
  16. }
  17. ]
  18. })
  19. )

参考

connect-history-api-fallback库的理解