H
Hono6mo ago
ZX

Experimenting with custom NextJS Server

Hey guys, I'm running into an issue where nextjs is not hydrating properly and hono is giving an error: unhandledRejection: Error [ERR_HTTP_HEADERS_SENT]: Cannot set headers after they are sent to the client Here's the code:
import { HttpBindings, getRequestListener } from "@hono/node-server";
import { Hono } from "hono";
import { createServer } from "http";
import { initialize } from "next/dist/server/lib/router-server";

const NODE_ENV = process.env.NODE_ENV! ?? "development";
const NODE_DEBUG = process.env.NODE_DEBUG!;
const PORT = process.env.PORT! ?? "3000";
const HOSTNAME = process.env.HOSTNAME! ?? "localhost";
const EMPTY_BUFFER = Buffer.alloc(0);

const app = new Hono<{ Bindings: HttpBindings }>();
const listener = getRequestListener(app.fetch);

const main = async () => {
const [handle, handleUpgrade] = await initialize({
dev: NODE_ENV !== "production",
port: parseInt(PORT ?? "3000"),
hostname: HOSTNAME,
dir: process.cwd(),
isNodeDebugging: NODE_DEBUG === "true",
customServer: true,
});

app.all("*", (c) => {
if (c.env.incoming.headers.upgrade) {
return handleUpgrade(c.env.incoming, c.env.incoming.socket, EMPTY_BUFFER);
}
return handle(c.env.incoming, c.env.outgoing);
});

createServer(listener).listen(parseInt(PORT), () => {
console.log(`Listening on port ${PORT}`);
});
};

main();
import { HttpBindings, getRequestListener } from "@hono/node-server";
import { Hono } from "hono";
import { createServer } from "http";
import { initialize } from "next/dist/server/lib/router-server";

const NODE_ENV = process.env.NODE_ENV! ?? "development";
const NODE_DEBUG = process.env.NODE_DEBUG!;
const PORT = process.env.PORT! ?? "3000";
const HOSTNAME = process.env.HOSTNAME! ?? "localhost";
const EMPTY_BUFFER = Buffer.alloc(0);

const app = new Hono<{ Bindings: HttpBindings }>();
const listener = getRequestListener(app.fetch);

const main = async () => {
const [handle, handleUpgrade] = await initialize({
dev: NODE_ENV !== "production",
port: parseInt(PORT ?? "3000"),
hostname: HOSTNAME,
dir: process.cwd(),
isNodeDebugging: NODE_DEBUG === "true",
customServer: true,
});

app.all("*", (c) => {
if (c.env.incoming.headers.upgrade) {
return handleUpgrade(c.env.incoming, c.env.incoming.socket, EMPTY_BUFFER);
}
return handle(c.env.incoming, c.env.outgoing);
});

createServer(listener).listen(parseInt(PORT), () => {
console.log(`Listening on port ${PORT}`);
});
};

main();
I'm wondering if anyone with more experience or insight might have an idea with what's going wrong here
1 Reply
g1wq2
g1wq23mo ago
Hi, did you solve the problem? Does anyone have an implementation of NextJS custom server + Hono?
Want results from more Discord servers?
Add your server