-
-
Notifications
You must be signed in to change notification settings - Fork 6.2k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
fix(html)!: align html serving between dev and preview #14756
Merged
Merged
Changes from all commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
197dcda
fix(html)!: align html serving between dev and preview
bluwy e813603
Merge branch 'main' into align-html-dev-prod
bluwy c60da14
chore: improve test
bluwy 73e375b
Merge branch 'main' into align-html-dev-prod
bluwy 7bd876c
Merge branch 'main' into align-html-dev-prod
bluwy f96af05
docs: update migration
bluwy d5ed1d7
fix: handle mounted if not root only
bluwy File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
108 changes: 73 additions & 35 deletions
108
packages/vite/src/node/server/middlewares/htmlFallback.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,48 +1,86 @@ | ||
import fs from 'node:fs' | ||
import path from 'node:path' | ||
import history from 'connect-history-api-fallback' | ||
import type { Connect } from 'dep-types/connect' | ||
import { createDebugger } from '../../utils' | ||
import { cleanUrl, createDebugger } from '../../utils' | ||
|
||
const debug = createDebugger('vite:html-fallback') | ||
|
||
export function htmlFallbackMiddleware( | ||
root: string, | ||
spaFallback: boolean, | ||
mounted = false, | ||
): Connect.NextHandleFunction { | ||
const historyHtmlFallbackMiddleware = history({ | ||
disableDotRule: true, | ||
logger: createDebugger('vite:html-fallback'), | ||
rewrites: [ | ||
// support /dir/ without explicit index.html | ||
{ | ||
from: /\/$/, | ||
to({ parsedUrl, request }: any) { | ||
const rewritten = | ||
decodeURIComponent(parsedUrl.pathname) + 'index.html' | ||
|
||
if (fs.existsSync(path.join(root, rewritten))) { | ||
return rewritten | ||
} | ||
|
||
return spaFallback ? `/index.html` : request.url | ||
}, | ||
}, | ||
{ | ||
from: /\.html$/, | ||
to({ parsedUrl, request }: any) { | ||
// .html files are not handled by serveStaticMiddleware | ||
// so we need to check if the file exists | ||
const pathname = decodeURIComponent(parsedUrl.pathname) | ||
if (fs.existsSync(path.join(root, pathname))) { | ||
return request.url | ||
} | ||
return spaFallback ? `/index.html` : request.url | ||
}, | ||
}, | ||
], | ||
}) | ||
// When this middleware is mounted on a route, we need to re-assign `req.url` with a | ||
// leading `.` to signal a relative rewrite. Returning with a leading `/` returns a | ||
// buggy `req.url`. e.g.: | ||
// | ||
// mount /foo/bar: | ||
// req.url = /index.html | ||
// final = /foo/barindex.html | ||
// | ||
// mount /foo/bar: | ||
// req.url = ./index.html | ||
// final = /foo/bar/index.html | ||
sapphi-red marked this conversation as resolved.
Show resolved
Hide resolved
|
||
const prepend = mounted ? '.' : '' | ||
|
||
// Keep the named function. The name is visible in debug logs via `DEBUG=connect:dispatcher ...` | ||
return function viteHtmlFallbackMiddleware(req, res, next) { | ||
return historyHtmlFallbackMiddleware(req, res, next) | ||
if ( | ||
// Only accept GET or HEAD | ||
(req.method !== 'GET' && req.method !== 'HEAD') || | ||
// Require Accept header | ||
!req.headers || | ||
typeof req.headers.accept !== 'string' || | ||
// Ignore JSON requests | ||
req.headers.accept.includes('application/json') || | ||
// Require Accept: text/html or */* | ||
!( | ||
req.headers.accept.includes('text/html') || | ||
req.headers.accept.includes('*/*') | ||
) | ||
) { | ||
return next() | ||
} | ||
|
||
const url = cleanUrl(req.url!) | ||
const pathname = decodeURIComponent(url) | ||
|
||
// .html files are not handled by serveStaticMiddleware | ||
// so we need to check if the file exists | ||
if (pathname.endsWith('.html')) { | ||
const filePath = path.join(root, pathname) | ||
if (fs.existsSync(filePath)) { | ||
debug?.(`Rewriting ${req.method} ${req.url} to ${url}`) | ||
req.url = prepend + url | ||
return next() | ||
} | ||
} | ||
// trailing slash should check for fallback index.html | ||
else if (pathname[pathname.length - 1] === '/') { | ||
const filePath = path.join(root, pathname, 'index.html') | ||
if (fs.existsSync(filePath)) { | ||
const newUrl = url + 'index.html' | ||
debug?.(`Rewriting ${req.method} ${req.url} to ${newUrl}`) | ||
req.url = prepend + newUrl | ||
return next() | ||
} | ||
} | ||
// non-trailing slash should check for fallback .html | ||
else { | ||
const filePath = path.join(root, pathname + '.html') | ||
if (fs.existsSync(filePath)) { | ||
const newUrl = url + '.html' | ||
debug?.(`Rewriting ${req.method} ${req.url} to ${newUrl}`) | ||
req.url = prepend + newUrl | ||
return next() | ||
} | ||
} | ||
|
||
if (spaFallback) { | ||
debug?.(`Rewriting ${req.method} ${req.url} to /index.html`) | ||
req.url = prepend + '/index.html' | ||
} | ||
|
||
next() | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
import type { Connect } from 'dep-types/connect' | ||
|
||
export function notFoundMiddleware(): Connect.NextHandleFunction { | ||
// Keep the named function. The name is visible in debug logs via `DEBUG=connect:dispatcher ...` | ||
return function vite404Middleware(_, res) { | ||
res.statusCode = 404 | ||
res.end() | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Should we also align the base removal behavior? The dev server uses
baseMiddleware
that stips the base so that the middlewares doesn't have to takebase
into consideration.vite/packages/vite/src/node/server/index.ts
Lines 649 to 652 in ac5d8a7
vite/packages/vite/src/node/server/middlewares/base.ts
Lines 18 to 19 in ac5d8a7
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'm not quite sure about this one. It would be a big breaking change I think for those expecting the base not removed in preview, and I think it's a bit late to make that change now 🤔