feat(html): pass req + res to transformIndexHtml#14797
Conversation
|
|
a8479d5 to
bc21e49
Compare
req + res to transformIndexHtmlreq + res to transformIndexHtml
| @@ -374,6 +374,9 @@ Vite plugins can also provide hooks that serve Vite-specific purposes. These hoo | |||
| server?: ViteDevServer | |||
There was a problem hiding this comment.
Starting thread for open questions on this PR...
-
It was not immediately apparent to me where tests for this change would belong. Would appreciate some pointers!
-
Should this change also affect
ViteDevServer.transformRequest?
vite/packages/vite/src/node/server/index.ts
Lines 230 to 237 in 979534e
From looking at the implementation of
transformRequest, my hunch is "no", but wanted to call this out. -
Would an example of this feature in the
playgroundfolder be useful? -
Should this change be back-ported this to 4.x? Or is 4.x feature-frozen as of now? (Happy to back-port if given the go-ahead.)
There was a problem hiding this comment.
Tests can go into playground/html with a new plugin that uses this new property perhaps. e.g setting headers. We have a couple tests that tests x-served-by header for example.
transformRequest should not be affected. Those are use for JS mostly.
Yeah this would go into Vite 5. We don't usually backport features to 4.0
| server?: ViteDevServer | ||
| bundle?: import('rollup').OutputBundle | ||
| chunk?: import('rollup').OutputChunk | ||
| originalUrl?: string |
There was a problem hiding this comment.
Note
originalUrlwas added toIndexHtmlTransformContextin 13d41d8, but the docs still reflected the previous type signature. Taking this opportunity to roll in the fix.
| bundle?: import('rollup').OutputBundle | ||
| chunk?: import('rollup').OutputChunk | ||
| originalUrl?: string | ||
| req?: import('connect').IncomingMessage |
There was a problem hiding this comment.
Since transformIndexHtml is also called during dev server warmup, when there is no req / res to pass through, is this something which warrants additional documentation or comments?
vite/packages/vite/src/node/server/warmup.ts
Lines 34 to 40 in 17fb5ee
There was a problem hiding this comment.
I think it should be fine undocumented about this case, as long as we document that consumers can optionally pass req or res in dev, e.g. in SSR. So it's not a guarantee.
| @@ -938,6 +940,8 @@ export interface IndexHtmlTransformContext { | |||
| bundle?: OutputBundle | |||
There was a problem hiding this comment.
Wanted to double check something... The type signature of IndexHtmlTransformContext currently permits some invalid values, such as objects with server and bundle. I wonder, why this type not a disjunction?
interface IndexHtmlTransformBuildContext {
path: string
filename: string
// NOTE: `bundle` and `chunk` are optional because they are not set for
// `transformIndexHtml` with `order: pre`.
bundle?: OutputBundle
chunk?: OutputChunk
}
interface IndexHtmlTransformDevContext {
path: string
filename: string
server: ViteDevServer
originalUrl: string
// NOTE: `req` and `res` are optional because they are optional in
// `ViteDevServer.transformIndexHtml`
req?: Connect.IncomingMessage
res?: http.ServerResponse
}
export type IndexHtmlTransformDevContext =
IndexHtmlTransformBuildContext | IndexHtmlTransformDevContext;Is it because such a change would break types for upstream dependents?
Alternatively, we could keep the existing type signature but group related fields together under commented groups, like:
export interface IndexHtmlTransformContext {
/**
* public path when served
*/
path: string
/**
* filename on disk
*/
filename: string
// Parameters available during `vite dev` only
server?: ViteDevServer
originalUrl?: string
req?: Connect.IncomingMessage
res?: http.ServerResponse
// Parameters available during `vite build only`
bundle?: OutputBundle
chunk?: OutputChunk
}There was a problem hiding this comment.
Probably was easier implemented in the past that way 😄 But I think we could change that separately, but yeah I think it would break downstream quite a bit, so maybe not quite worth the change.
| bundle?: OutputBundle | ||
| chunk?: OutputChunk | ||
| originalUrl?: string | ||
| req?: Connect.IncomingMessage |
There was a problem hiding this comment.
Since transformIndexHtml is also called during dev server warmup, when there is no req / res to pass through, is this something which warrants additional documentation or comments?
vite/packages/vite/src/node/server/warmup.ts
Lines 34 to 40 in 17fb5ee
|
Thanks for the PR! Just to set expectations, we haven't discussed this yet so there's a chance the PR might not go through. But appreciate the work here! The point about " |
Makes sense! I assume it's okay to leave this PR as-is for now pending the Vite team discussion, since even in its current state it shows the basic implementation and calls out gotchas to consider. Happy to implement the feedback per #14797 (comment) if the team wants to move ahead. |
Fixes #14431.
Description
As discussed in #14431 (comment), it may be useful to pass
reqandrestotransformIndexHtmlduringvite dev. This has no effect onvite previeworvite build.Additional context
Original discussion: #14431 (comment)
What is the purpose of this pull request?
Before submitting the PR, please make sure you do the following
fixes #123).