-
-
Notifications
You must be signed in to change notification settings - Fork 138
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Fix response streaming with wrangler dev server (#1242)
Fixes #1237.
- Loading branch information
Showing
3 changed files
with
35 additions
and
1 deletion.
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
// Workaround https://github.com/cloudflare/workers-sdk/issues/6577 | ||
import type { Middleware } from 'waku/config'; | ||
|
||
function isWranglerDev(headers?: Record<string, string | string[]>): boolean { | ||
// This header seems to only be set for production cloudflare workers | ||
return !headers?.['cf-visitor']; | ||
} | ||
|
||
const cloudflareMiddleware: Middleware = () => { | ||
return async (ctx, next) => { | ||
await next(); | ||
if (!import.meta.env?.PROD) { | ||
return; | ||
} | ||
if (!isWranglerDev(ctx.req.headers)) { | ||
return; | ||
} | ||
const contentType = ctx.res.headers?.['content-type']; | ||
if ( | ||
!contentType || | ||
contentType.includes('text/html') || | ||
contentType.includes('text/plain') | ||
) { | ||
ctx.res.headers ||= {}; | ||
ctx.res.headers['content-encoding'] = 'Identity'; | ||
} | ||
}; | ||
}; | ||
|
||
export default cloudflareMiddleware; |
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