fix: normalize html path in transformIndexHtml()#18976
Conversation
|
Also noticed that SSR guide uses Not sure if this is known, but it doesn't work with |
| return applyHtmlTransforms(html, transformHooks, { | ||
| path: url, | ||
| filename: getHtmlFilename(url, server), | ||
| path: normalized.url, |
There was a problem hiding this comment.
I think we shouldn't normalize path here. We don't know if / is responding with the same HTML file with /index.html when appType: 'custom'.
Instead, I think we should make devHtmlHook function work with path ending with /.
There was a problem hiding this comment.
My reasoning was that the path should be the same during build and development. And I assumed that the paths always refer to a file during build.
Can the html path be / during build?
There was a problem hiding this comment.
Can the html path be
/during build?
No, it always ends with .html during build currently. But it's valid to only call transformIndexHtml in dev and not use it in build.
There was a problem hiding this comment.
In that case, if no constraints are placed on the path, shouldn't the user decide filename as well?
What if there would be 2 versions of the function, and one of them would work like the builtin vite server but allow using SSR?
There was a problem hiding this comment.
In that case, if no constraints are placed on the path, shouldn't the user decide filename as well?
Yes, I was thinking of adding file parameter to server.transformIndexHtml. Something like:
type TransformIndexHtmlInput = {
/** the served URL path (may include queries and hashes) (should include base if exists) */
url: string
/**
* the file path to the HTML file
* used to resolve the relative URLs for script tags and other assets
*/
file: string
}
interface ViteDevServer {
transformIndexHtml(input: TransformIndexHtmlInput): Promise<string>
/** @deprecated -- kept for backward compat */
transformIndexHtml(
url: string,
html: string,
originalUrl?: string,
): Promise<string>
}But this requires more code changes. It would be awesome if you want to work on this (no pressure, I'm also fine only with the fix).
Problem
Currently, the
urlaccepted bytransformIndexHtml()is passed unaltered toapplyHtmlTransforms().This causes issues if the url/index.html?a=borindex.html#id).This is different from how default vite server and build behave; they use urls that refer to a file. (The server modifies urls in
htmlFallbackMiddleware()).Solution
I extracted html path normalization from
htmlFallbackMiddleware()and used it increateDevHtmlTransformFn().Fixes #18964