Skip to content

Commit

Permalink
Fix response streaming with wrangler dev server (#1242)
Browse files Browse the repository at this point in the history
Fixes #1237.
  • Loading branch information
rmarscher authored Feb 18, 2025
1 parent c9ffbb6 commit bfae4ea
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 1 deletion.
5 changes: 4 additions & 1 deletion examples/07_cloudflare/src/pages/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { Link } from 'waku';
import { Suspense } from 'react';
import { Counter } from '../components/counter';
import { getHonoContext } from '../lib/hono';
import { getEnv } from '../lib/waku';
import { getEnv, isBuild } from '../lib/waku';

export default async function HomePage() {
const data = await getData();
Expand Down Expand Up @@ -44,6 +44,9 @@ export default async function HomePage() {

// Example async server component
const ServerMessage = async () => {
if (isBuild()) {
console.warn('Note: server components are awaited during build.');
}
await new Promise((resolve) => setTimeout(resolve, 2000));
return <p>Hello from server!</p>;
};
Expand Down
30 changes: 30 additions & 0 deletions examples/07_cloudflare/waku.cloudflare-middleware.ts
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;
1 change: 1 addition & 0 deletions examples/07_cloudflare/waku.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ export default defineConfig({
return [
import('waku/middleware/context'),
import('waku/middleware/dev-server'),
import('./waku.cloudflare-middleware'),
import('waku/middleware/handler'),
];
},
Expand Down

0 comments on commit bfae4ea

Please sign in to comment.