Skip to content

Commit

Permalink
feat(web): add node adapter
Browse files Browse the repository at this point in the history
  • Loading branch information
yjl9903 committed Oct 1, 2024
1 parent 5c2736c commit 9447ef4
Show file tree
Hide file tree
Showing 6 changed files with 61 additions and 4 deletions.
8 changes: 4 additions & 4 deletions apps/frontend/web/cloudflare/entry.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,17 +8,17 @@ import * as build from '../build/server'; // eslint-disable-line import/no-unres
// @ts-ignore
import __STATIC_CONTENT_MANIFEST from '__STATIC_CONTENT_MANIFEST';

import type { Bindings } from './types';
import type { Bindings } from '../node/types';

import { api } from '../node/api';

import { remix } from './remix';

export const app = new Hono<{ Bindings: Bindings }>();

app.all('*', remix({ build, manifest: __STATIC_CONTENT_MANIFEST }));

app.all('/api/*', (c) => {
return c.json({ ok: false });
});
app.all('/api/*', api());

app.onError((err, c) => {
if (err.message) {
Expand Down
2 changes: 2 additions & 0 deletions apps/frontend/web/cloudflare/remix.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@ export const remix = <E extends Env = any>(options: RemixHandlerOptions): Handle
const handleRemixRequest = createRequestHandler(options.build);

return async (ctx) => {
console.log('ctx', ctx, ctx.env, ctx.var);

const waitUntil = ctx.event.waitUntil.bind(ctx.event);
const passThroughOnException = ctx.event.passThroughOnException.bind(ctx.event);
const request = ctx.req.raw;
Expand Down
7 changes: 7 additions & 0 deletions apps/frontend/web/node/api.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
import type { Handler, Env } from 'hono';

export const api = <E extends Env = any>(): Handler<E> => {
return async (ctx) => {
return ctx.json({ ok: false });
};
};
26 changes: 26 additions & 0 deletions apps/frontend/web/node/entry.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
import { Hono } from 'hono';

// eslint-disable-next-line @typescript-eslint/ban-ts-comment
// @ts-ignore This file won’t exist if it hasn’t yet been built
import * as build from '../build/server'; // eslint-disable-line import/no-unresolved

import type { Bindings } from './types';

import { api } from './api';
import { remix } from './remix';

export const app = new Hono<{ Bindings: Bindings }>();

app.all('*', remix({ build }));

app.all('/api/*', api());

app.onError((err, c) => {
if (err.message) {
console.log(...err.message.trim().split('\n'));
}
if (err.stack) {
console.log(...err.stack.trim().split('\n'));
}
return c.json({ status: 500, messsage: err?.message ?? 'Internal Error' }, 500);
});
22 changes: 22 additions & 0 deletions apps/frontend/web/node/remix.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
import type { Handler, Env } from 'hono';

import { createRequestHandler, type ServerBuild } from '@remix-run/node';

export interface RemixHandlerOptions {
build: ServerBuild;
}

export const remix = <E extends Env = any>(options: RemixHandlerOptions): Handler<E> => {
const handleRemixRequest = createRequestHandler(options.build);

return async (ctx) => {
const request = ctx.req.raw;

try {
const loadContext = {};
return await handleRemixRequest(request, loadContext);
} catch (error) {
throw error;
}
};
};
File renamed without changes.

0 comments on commit 9447ef4

Please sign in to comment.