htmlFallback.ts — vite Source File
Architecture documentation for htmlFallback.ts, a typescript file in the vite codebase. 12 imports, 2 dependents.
Entity Profile
Dependency Diagram
graph LR 56b9dfe1_f57e_b16b_3e97_b64544e748cd["htmlFallback.ts"] 031bc221_67a8_c579_f2bf_bb30a08beeb2["utils.ts"] 56b9dfe1_f57e_b16b_3e97_b64544e748cd --> 031bc221_67a8_c579_f2bf_bb30a08beeb2 23a2e685_f919_9578_27ba_bde71c122058["createDebugger"] 56b9dfe1_f57e_b16b_3e97_b64544e748cd --> 23a2e685_f919_9578_27ba_bde71c122058 c9db8630_93b3_267d_8e26_8b62626a11ca["joinUrlSegments"] 56b9dfe1_f57e_b16b_3e97_b64544e748cd --> c9db8630_93b3_267d_8e26_8b62626a11ca abfc9e70_3c15_b3f0_a595_3cf27afb7e64["utils.ts"] 56b9dfe1_f57e_b16b_3e97_b64544e748cd --> abfc9e70_3c15_b3f0_a595_3cf27afb7e64 10b9dea8_362c_1af2_93be_afa4dd9aed9e["cleanUrl"] 56b9dfe1_f57e_b16b_3e97_b64544e748cd --> 10b9dea8_362c_1af2_93be_afa4dd9aed9e 7916c84f_5621_2b3b_d220_a171ebce997f["environment.ts"] 56b9dfe1_f57e_b16b_3e97_b64544e748cd --> 7916c84f_5621_2b3b_d220_a171ebce997f f634223d_ed8e_a65b_08f8_a839ec17994a["DevEnvironment"] 56b9dfe1_f57e_b16b_3e97_b64544e748cd --> f634223d_ed8e_a65b_08f8_a839ec17994a f070fb78_2f2a_ef11_0530_d34862fa95ca["fullBundleEnvironment.ts"] 56b9dfe1_f57e_b16b_3e97_b64544e748cd --> f070fb78_2f2a_ef11_0530_d34862fa95ca 8fcd8920_61ef_93b8_499a_c18a48b852e5["FullBundleDevEnvironment"] 56b9dfe1_f57e_b16b_3e97_b64544e748cd --> 8fcd8920_61ef_93b8_499a_c18a48b852e5 51e96894_3556_ed5c_1ede_97d449867adf["node:path"] 56b9dfe1_f57e_b16b_3e97_b64544e748cd --> 51e96894_3556_ed5c_1ede_97d449867adf e6032fbc_44cf_58d6_868d_dd15106c18c5["node:fs"] 56b9dfe1_f57e_b16b_3e97_b64544e748cd --> e6032fbc_44cf_58d6_868d_dd15106c18c5 9165291b_077b_bedb_8c23_36e44bc99390["connect"] 56b9dfe1_f57e_b16b_3e97_b64544e748cd --> 9165291b_077b_bedb_8c23_36e44bc99390 e49f0ff7_5101_3a1d_5a1f_33fae58eea2d["preview.ts"] e49f0ff7_5101_3a1d_5a1f_33fae58eea2d --> 56b9dfe1_f57e_b16b_3e97_b64544e748cd a423a1ed_f7d8_0eb5_9b8f_ddfa7fa8147e["index.ts"] a423a1ed_f7d8_0eb5_9b8f_ddfa7fa8147e --> 56b9dfe1_f57e_b16b_3e97_b64544e748cd style 56b9dfe1_f57e_b16b_3e97_b64544e748cd fill:#6366f1,stroke:#818cf8,color:#fff
Relationship Graph
Source Code
import path from 'node:path'
import fs from 'node:fs'
import type { Connect } from '#dep-types/connect'
import { createDebugger, joinUrlSegments } from '../../utils'
import { cleanUrl } from '../../../shared/utils'
import type { DevEnvironment } from '../environment'
import { FullBundleDevEnvironment } from '../environments/fullBundleEnvironment'
const debug = createDebugger('vite:html-fallback')
export function htmlFallbackMiddleware(
root: string,
spaFallback: boolean,
clientEnvironment?: DevEnvironment,
): Connect.NextHandleFunction {
const memoryFiles =
clientEnvironment instanceof FullBundleDevEnvironment
? clientEnvironment.memoryFiles
: undefined
function checkFileExists(relativePath: string) {
return (
memoryFiles?.has(
relativePath.slice(1), // remove first /
) ?? fs.existsSync(path.join(root, relativePath))
)
}
// Keep the named function. The name is visible in debug logs via `DEBUG=connect:dispatcher ...`
return function viteHtmlFallbackMiddleware(req, _res, next) {
if (
// Only accept GET or HEAD
(req.method !== 'GET' && req.method !== 'HEAD') ||
// Exclude default favicon requests
req.url === '/favicon.ico' ||
// Require Accept: text/html or */*
!(
req.headers.accept === undefined || // equivalent to `Accept: */*`
req.headers.accept === '' || // equivalent to `Accept: */*`
req.headers.accept.includes('text/html') ||
req.headers.accept.includes('*/*')
)
) {
return next()
}
const url = cleanUrl(req.url!)
let pathname
try {
pathname = decodeURIComponent(url)
} catch {
// ignore malformed URI
return next()
}
// .html files are not handled by serveStaticMiddleware
// so we need to check if the file exists
if (pathname.endsWith('.html')) {
if (checkFileExists(pathname)) {
debug?.(`Rewriting ${req.method} ${req.url} to ${url}`)
req.url = url
return next()
}
}
// trailing slash should check for fallback index.html
else if (pathname.endsWith('/')) {
if (checkFileExists(joinUrlSegments(pathname, 'index.html'))) {
const newUrl = url + 'index.html'
debug?.(`Rewriting ${req.method} ${req.url} to ${newUrl}`)
req.url = newUrl
return next()
}
}
// non-trailing slash should check for fallback .html
else {
if (checkFileExists(pathname + '.html')) {
const newUrl = url + '.html'
debug?.(`Rewriting ${req.method} ${req.url} to ${newUrl}`)
req.url = newUrl
return next()
}
}
if (spaFallback) {
debug?.(`Rewriting ${req.method} ${req.url} to /index.html`)
req.url = '/index.html'
}
next()
}
}
Domain
Subdomains
Functions
Dependencies
Source
Frequently Asked Questions
What does htmlFallback.ts do?
htmlFallback.ts is a source file in the vite codebase, written in typescript. It belongs to the ViteCore domain, ConfigEngine subdomain.
What functions are defined in htmlFallback.ts?
htmlFallback.ts defines 1 function(s): htmlFallbackMiddleware.
What does htmlFallback.ts depend on?
htmlFallback.ts imports 12 module(s): DevEnvironment, FullBundleDevEnvironment, cleanUrl, connect, createDebugger, environment.ts, fullBundleEnvironment.ts, joinUrlSegments, and 4 more.
What files import htmlFallback.ts?
htmlFallback.ts is imported by 2 file(s): index.ts, preview.ts.
Where is htmlFallback.ts in the architecture?
htmlFallback.ts is located at packages/vite/src/node/server/middlewares/htmlFallback.ts (domain: ViteCore, subdomain: ConfigEngine, directory: packages/vite/src/node/server/middlewares).
Analyze Your Own Codebase
Get architecture documentation, dependency graphs, and domain analysis for your codebase in minutes.
Try Supermodel Free