H
Hono4d ago
Lazuee

Context is not finalized

<-- GET /
Error: Context is not finalized. Did you forget to return a Response object or `await next()`?
at file:///workspace/react-router/node_modules/.pnpm/[email protected]/node_modules/hono/dist/hono-base.js:202:17
at getRequestListener.overrideGlobalObjects (file:///workspace/react-router/node_modules/.pnpm/@[email protected][email protected]/node_modules/@hono/vite-dev-server/dist/dev-server.js:78:32)
at responseViaResponseObject (file:///workspace/react-router/node_modules/.pnpm/@[email protected][email protected]/node_modules/@hono/node-server/dist/index.mjs:330:15)
--> GET / 200 8ms
<-- GET /
Error: Context is not finalized. Did you forget to return a Response object or `await next()`?
at file:///workspace/react-router/node_modules/.pnpm/[email protected]/node_modules/hono/dist/hono-base.js:202:17
at getRequestListener.overrideGlobalObjects (file:///workspace/react-router/node_modules/.pnpm/@[email protected][email protected]/node_modules/@hono/vite-dev-server/dist/dev-server.js:78:32)
at responseViaResponseObject (file:///workspace/react-router/node_modules/.pnpm/@[email protected][email protected]/node_modules/@hono/node-server/dist/index.mjs:330:15)
--> GET / 200 8ms
5 Replies
Nico
Nico4d ago
I would try disabling all your middleware one by one to see what’s causing the issue
Lazuee
Lazuee4d ago
Lazuee
Lazuee4d ago
// index.ts
import { Hono } from "hono";
import { logger } from "hono/logger";
import { reactRouter } from "./middleware";
import routes from "./routes";

declare module "react-router" {
export interface AppLoadContext {
readonly nonce?: string;
readonly clientIp?: string;
readonly env: typeof env;
}
}
declare module "react-router" {
interface LoaderFunctionArgs {
context: AppLoadContext;
}
}

const createServer = async () => {
const build = await getServerBuild();
const app = new Hono().use(
logger(),
reactRouter({
build,
mode: env.IS_PRODUCTION_BUILD ? "production" : "development",
getLoadContext: (ctx) => ({
clientIp: ctx.var.clientIp,
nonce: ctx.var.nonce,
env: env,
}),
}),
)
// index.ts
import { Hono } from "hono";
import { logger } from "hono/logger";
import { reactRouter } from "./middleware";
import routes from "./routes";

declare module "react-router" {
export interface AppLoadContext {
readonly nonce?: string;
readonly clientIp?: string;
readonly env: typeof env;
}
}
declare module "react-router" {
interface LoaderFunctionArgs {
context: AppLoadContext;
}
}

const createServer = async () => {
const build = await getServerBuild();
const app = new Hono().use(
logger(),
reactRouter({
build,
mode: env.IS_PRODUCTION_BUILD ? "production" : "development",
getLoadContext: (ctx) => ({
clientIp: ctx.var.clientIp,
nonce: ctx.var.nonce,
env: env,
}),
}),
)
// middleware/react-router.ts
import { createRequestHandler } from "react-router";

import type { AppLoadContext, ServerBuild } from "react-router";
import { type Context, type Env } from "hono";
import { createMiddleware } from "hono/factory";

export interface RemixMiddlewareOptions {
build: ServerBuild;
mode?: "development" | "production";
getLoadContext?(c: Context): Promise<AppLoadContext> | AppLoadContext;
}
export function reactRouter<TEnv extends Env>({ build, mode, getLoadContext }: RemixMiddlewareOptions) {
const requestHandler = createRequestHandler(build, mode);
return createMiddleware<TEnv>(async (c) => {
const loadContext = getLoadContext?.(c);
return await requestHandler(c.req.raw, loadContext instanceof Promise ? await loadContext : loadContext);
// ^^ the cause of error,.
});
}
// middleware/react-router.ts
import { createRequestHandler } from "react-router";

import type { AppLoadContext, ServerBuild } from "react-router";
import { type Context, type Env } from "hono";
import { createMiddleware } from "hono/factory";

export interface RemixMiddlewareOptions {
build: ServerBuild;
mode?: "development" | "production";
getLoadContext?(c: Context): Promise<AppLoadContext> | AppLoadContext;
}
export function reactRouter<TEnv extends Env>({ build, mode, getLoadContext }: RemixMiddlewareOptions) {
const requestHandler = createRequestHandler(build, mode);
return createMiddleware<TEnv>(async (c) => {
const loadContext = getLoadContext?.(c);
return await requestHandler(c.req.raw, loadContext instanceof Promise ? await loadContext : loadContext);
// ^^ the cause of error,.
});
}
await requestHandler(c.req.raw, loadContext instanceof Promise ? await loadContext : loadContext);
await requestHandler(c.req.raw, loadContext instanceof Promise ? await loadContext : loadContext);
this is the cause of the error and it didn't catch any error when I do try-catch. Also, the response is okay but i'm wondering why do i get Context is not finalized?
Nico
Nico4d ago
Generally you should not return anything from inside a middleware there are some cases where you can but it skips all the rest of the middleware and Hono application Is what your returning a valid response object?
Lazuee
Lazuee4d ago
fixed, missing return on csp ... i should have checked the my other middleware.. which was used on reactRouter function.
No description
Want results from more Discord servers?
Add your server