File tree Expand file tree Collapse file tree 3 files changed +21
-3
lines changed
packages/vite-plugin-cloudflare
playground/worker-♫/__tests__ Expand file tree Collapse file tree 3 files changed +21
-3
lines changed Original file line number Diff line number Diff line change 1+ ---
2+ " @cloudflare/vite-plugin " : patch
3+ ---
4+
5+ Preserve the original ` x-forwarded-host ` header if it is set.
Original file line number Diff line number Diff line change @@ -32,7 +32,15 @@ test("basic dev logging", async () => {
3232 expect ( serverLogs . errors . join ( ) ) . toContain ( "__console warn__" ) ;
3333} ) ;
3434
35- test ( "receives the original host as the `X-Forwarded-Host` header" , async ( ) => {
35+ test ( "receives the original `x-forwarded-host` header if it is set" , async ( ) => {
36+ const response = await fetch ( `${ viteTestUrl } /x-forwarded-host` , {
37+ headers : { "x-forwarded-host" : "example.com:8080" } ,
38+ } ) ;
39+
40+ expect ( await response . text ( ) ) . toBe ( "example.com:8080" ) ;
41+ } ) ;
42+
43+ test ( "receives the Vite server host as the `x-forwarded-host` header if the `x-forwarded-host` header is not set" , async ( ) => {
3644 const testUrl = new URL ( viteTestUrl ) ;
3745 await vi . waitFor (
3846 async ( ) =>
Original file line number Diff line number Diff line change @@ -70,11 +70,16 @@ export function createRequestHandler(
7070}
7171
7272function toMiniflareRequest ( request : Request ) : MiniflareRequest {
73- // We set the X-Forwarded-Host header to the original host as the `Host` header inside a Worker will contain the workerd host
7473 const host = request . headers . get ( "Host" ) ;
75- if ( host ) {
74+ const xForwardedHost = request . headers . get ( "X-Forwarded-Host" ) ;
75+
76+ if ( host && ! xForwardedHost ) {
77+ // Set the `x-forwarded-host` header to the host of the Vite server if it is not already set
78+ // Note that the `host` header inside the Worker will contain the workerd host
79+ // TODO: reconsider this when adopting `miniflare.dispatchFetch` as it may be possible to provide the Vite server host in the `host` header
7680 request . headers . set ( "X-Forwarded-Host" , host ) ;
7781 }
82+
7883 // Undici sets the `Sec-Fetch-Mode` header to `cors` so we capture it in a custom header to be converted back later.
7984 const secFetchMode = request . headers . get ( "Sec-Fetch-Mode" ) ;
8085 if ( secFetchMode ) {
You can’t perform that action at this time.
0 commit comments