File tree Expand file tree Collapse file tree 4 files changed +24
-2
lines changed
packages/vite-plugin-cloudflare Expand file tree Collapse file tree 4 files changed +24
-2
lines changed Original file line number Diff line number Diff line change 1+ ---
2+ " @cloudflare/vite-plugin " : patch
3+ ---
4+
5+ Set the ` x-forwarded-host ` header to the original host in requests. This fixes a bug where libraries such as Clerk would redirect to the workerd host rather than the Vite host.
Original file line number Diff line number Diff line change 11import { expect , test } from "vitest" ;
2- import { getTextResponse , serverLogs } from "../../__test-utils__" ;
2+ import { getTextResponse , serverLogs , viteTestUrl } from "../../__test-utils__" ;
33
44test ( "basic hello-world functionality" , async ( ) => {
55 expect ( await getTextResponse ( ) ) . toEqual ( "Hello World!" ) ;
@@ -10,3 +10,9 @@ test("basic dev logging", async () => {
1010 expect ( serverLogs . errors . join ( ) ) . toContain ( "__console error__" ) ;
1111 expect ( serverLogs . errors . join ( ) ) . toContain ( "__console warn__" ) ;
1212} ) ;
13+
14+ test ( "receives the original host as the `X-Forwarded-Host` header" , async ( ) => {
15+ const testUrl = new URL ( viteTestUrl ) ;
16+ const response = await getTextResponse ( "/x-forwarded-host" ) ;
17+ expect ( response ) . toBe ( testUrl . host ) ;
18+ } ) ;
Original file line number Diff line number Diff line change 11export default {
2- async fetch ( ) {
2+ async fetch ( request ) {
3+ const url = new URL ( request . url ) ;
4+
5+ if ( url . pathname === "/x-forwarded-host" ) {
6+ return new Response ( request . headers . get ( "X-Forwarded-Host" ) ) ;
7+ }
8+
39 console . log ( "__console log__" ) ;
410 console . warn ( "__console warn__" ) ;
511 console . error ( "__console error__" ) ;
Original file line number Diff line number Diff line change @@ -22,6 +22,11 @@ export function getRouterWorker(miniflare: Miniflare) {
2222}
2323
2424export function toMiniflareRequest ( request : Request ) : MiniflareRequest {
25+ // We set the X-Forwarded-Host header to the original host as the `Host` header inside a Worker will contain the workerd host
26+ const host = request . headers . get ( "Host" ) ;
27+ if ( host ) {
28+ request . headers . set ( "X-Forwarded-Host" , host ) ;
29+ }
2530 // Undici sets the `Sec-Fetch-Mode` header to `cors` so we capture it in a custom header to be converted back later.
2631 const secFetchMode = request . headers . get ( "Sec-Fetch-Mode" ) ;
2732 if ( secFetchMode ) {
You can’t perform that action at this time.
0 commit comments