|
1 | 1 | import fs from 'node:fs' |
2 | 2 | import path from 'node:path' |
3 | | -import history from 'connect-history-api-fallback' |
4 | 3 | import type { Connect } from 'dep-types/connect' |
5 | | -import { createDebugger } from '../../utils' |
| 4 | +import { cleanUrl, createDebugger } from '../../utils' |
| 5 | + |
| 6 | +const debug = createDebugger('vite:html-fallback') |
6 | 7 |
|
7 | 8 | export function htmlFallbackMiddleware( |
8 | 9 | root: string, |
9 | 10 | spaFallback: boolean, |
| 11 | + mounted = false, |
10 | 12 | ): Connect.NextHandleFunction { |
11 | | - const historyHtmlFallbackMiddleware = history({ |
12 | | - disableDotRule: true, |
13 | | - logger: createDebugger('vite:html-fallback'), |
14 | | - rewrites: [ |
15 | | - // support /dir/ without explicit index.html |
16 | | - { |
17 | | - from: /\/$/, |
18 | | - to({ parsedUrl, request }: any) { |
19 | | - const rewritten = |
20 | | - decodeURIComponent(parsedUrl.pathname) + 'index.html' |
21 | | - |
22 | | - if (fs.existsSync(path.join(root, rewritten))) { |
23 | | - return rewritten |
24 | | - } |
25 | | - |
26 | | - return spaFallback ? `/index.html` : request.url |
27 | | - }, |
28 | | - }, |
29 | | - { |
30 | | - from: /\.html$/, |
31 | | - to({ parsedUrl, request }: any) { |
32 | | - // .html files are not handled by serveStaticMiddleware |
33 | | - // so we need to check if the file exists |
34 | | - const pathname = decodeURIComponent(parsedUrl.pathname) |
35 | | - if (fs.existsSync(path.join(root, pathname))) { |
36 | | - return request.url |
37 | | - } |
38 | | - return spaFallback ? `/index.html` : request.url |
39 | | - }, |
40 | | - }, |
41 | | - ], |
42 | | - }) |
| 13 | + // When this middleware is mounted on a route, we need to re-assign `req.url` with a |
| 14 | + // leading `.` to signal a relative rewrite. Returning with a leading `/` returns a |
| 15 | + // buggy `req.url`. e.g.: |
| 16 | + // |
| 17 | + // mount /foo/bar: |
| 18 | + // req.url = /index.html |
| 19 | + // final = /foo/barindex.html |
| 20 | + // |
| 21 | + // mount /foo/bar: |
| 22 | + // req.url = ./index.html |
| 23 | + // final = /foo/bar/index.html |
| 24 | + const prepend = mounted ? '.' : '' |
43 | 25 |
|
44 | 26 | // Keep the named function. The name is visible in debug logs via `DEBUG=connect:dispatcher ...` |
45 | 27 | return function viteHtmlFallbackMiddleware(req, res, next) { |
46 | | - return historyHtmlFallbackMiddleware(req, res, next) |
| 28 | + if ( |
| 29 | + // Only accept GET or HEAD |
| 30 | + (req.method !== 'GET' && req.method !== 'HEAD') || |
| 31 | + // Require Accept header |
| 32 | + !req.headers || |
| 33 | + typeof req.headers.accept !== 'string' || |
| 34 | + // Ignore JSON requests |
| 35 | + req.headers.accept.includes('application/json') || |
| 36 | + // Require Accept: text/html or */* |
| 37 | + !( |
| 38 | + req.headers.accept.includes('text/html') || |
| 39 | + req.headers.accept.includes('*/*') |
| 40 | + ) |
| 41 | + ) { |
| 42 | + return next() |
| 43 | + } |
| 44 | + |
| 45 | + const url = cleanUrl(req.url!) |
| 46 | + const pathname = decodeURIComponent(url) |
| 47 | + |
| 48 | + // .html files are not handled by serveStaticMiddleware |
| 49 | + // so we need to check if the file exists |
| 50 | + if (pathname.endsWith('.html')) { |
| 51 | + const filePath = path.join(root, pathname) |
| 52 | + if (fs.existsSync(filePath)) { |
| 53 | + debug?.(`Rewriting ${req.method} ${req.url} to ${url}`) |
| 54 | + req.url = prepend + url |
| 55 | + return next() |
| 56 | + } |
| 57 | + } |
| 58 | + // trailing slash should check for fallback index.html |
| 59 | + else if (pathname[pathname.length - 1] === '/') { |
| 60 | + const filePath = path.join(root, pathname, 'index.html') |
| 61 | + if (fs.existsSync(filePath)) { |
| 62 | + const newUrl = url + 'index.html' |
| 63 | + debug?.(`Rewriting ${req.method} ${req.url} to ${newUrl}`) |
| 64 | + req.url = prepend + newUrl |
| 65 | + return next() |
| 66 | + } |
| 67 | + } |
| 68 | + // non-trailing slash should check for fallback .html |
| 69 | + else { |
| 70 | + const filePath = path.join(root, pathname + '.html') |
| 71 | + if (fs.existsSync(filePath)) { |
| 72 | + const newUrl = url + '.html' |
| 73 | + debug?.(`Rewriting ${req.method} ${req.url} to ${newUrl}`) |
| 74 | + req.url = prepend + newUrl |
| 75 | + return next() |
| 76 | + } |
| 77 | + } |
| 78 | + |
| 79 | + if (spaFallback) { |
| 80 | + debug?.(`Rewriting ${req.method} ${req.url} to /index.html`) |
| 81 | + req.url = prepend + '/index.html' |
| 82 | + } |
| 83 | + |
| 84 | + next() |
47 | 85 | } |
48 | 86 | } |
0 commit comments