fixed nextjs build + confirmed node adapter

This commit is contained in:
dswbx
2024-11-27 17:07:56 +01:00
parent c31bc2ccb0
commit b6e3c934c4
5 changed files with 19 additions and 19 deletions

View File

@@ -173,3 +173,9 @@ await tsup.build({
await tsup.build({ await tsup.build({
...baseConfig("bun") ...baseConfig("bun")
}); });
await tsup.build({
...baseConfig("node"),
platform: "node",
format: ["esm", "cjs"]
});

View File

@@ -1,6 +1,5 @@
import type { IncomingMessage, ServerResponse } from "node:http"; import type { IncomingMessage, ServerResponse } from "node:http";
import { Api, App, type CreateAppConfig } from "bknd"; import { Api, App, type CreateAppConfig } from "bknd";
import { isDebug } from "bknd/core";
import { nodeRequestToRequest } from "../index"; import { nodeRequestToRequest } from "../index";
type GetServerSidePropsContext = { type GetServerSidePropsContext = {
@@ -46,7 +45,7 @@ function getCleanRequest(req: Request) {
let app: App; let app: App;
export function serve(config: CreateAppConfig) { export function serve(config: CreateAppConfig) {
return async (req: Request) => { return async (req: Request) => {
if (!app || isDebug()) { if (!app) {
app = App.create(config); app = App.create(config);
await app.build(); await app.build();
} }

View File

@@ -1,10 +1,8 @@
import { readFile } from "node:fs/promises";
import path from "node:path"; import path from "node:path";
import { serve as honoServe } from "@hono/node-server"; import { serve as honoServe } from "@hono/node-server";
import { serveStatic } from "@hono/node-server/serve-static"; import { serveStatic } from "@hono/node-server/serve-static";
import { App, type CreateAppConfig } from "bknd"; import { App, type CreateAppConfig } from "bknd";
import { LibsqlConnection } from "bknd/data"; import { LibsqlConnection } from "bknd/data";
import type { Manifest } from "vite";
async function getConnection(conn?: CreateAppConfig["connection"]) { async function getConnection(conn?: CreateAppConfig["connection"]) {
if (conn) { if (conn) {
@@ -26,7 +24,6 @@ async function getConnection(conn?: CreateAppConfig["connection"]) {
export type NodeAdapterOptions = { export type NodeAdapterOptions = {
relativeDistPath?: string; relativeDistPath?: string;
viteManifest?: Manifest;
port?: number; port?: number;
hostname?: string; hostname?: string;
listener?: Parameters<typeof honoServe>[1]; listener?: Parameters<typeof honoServe>[1];
@@ -51,22 +48,16 @@ export function serve(_config: Partial<CreateAppConfig> = {}, options: NodeAdapt
connection connection
}); });
const viteManifest =
options.viteManifest ??
JSON.parse(await readFile(path.resolve(root, ".vite/manifest.json"), "utf-8"));
app.emgr.on( app.emgr.on(
"app-built", "app-built",
async () => { async () => {
app.modules.server.get( app.modules.server.get(
"/assets/*", "/*",
serveStatic({ serveStatic({
root root
}) })
); );
app.registerAdminController({ app.registerAdminController();
viteManifest
});
}, },
"sync" "sync"
); );

View File

@@ -12,10 +12,10 @@ Install bknd as a dependency:
``` tsx ``` tsx
// pages/api/[...route].ts // pages/api/[...route].ts
import { serve } from "bknd/adapter/nextjs"; import { serve } from "bknd/adapter/nextjs";
import type { PageConfig } from "next";
export const config: PageConfig = { export const config = {
runtime: "edge" runtime: "experimental-edge",
unstable_allowDynamic: ["**/*.js"]
}; };
export default serve({ export default serve({

View File

@@ -1,8 +1,12 @@
import { serve } from "bknd/adapter/nextjs"; import { serve } from "bknd/adapter/nextjs";
import type { PageConfig } from "next";
export const config: PageConfig = { export const config = {
runtime: "edge" runtime: "experimental-edge",
// add a matcher for bknd dist to allow dynamic otherwise build may fail.
// inside this repo it's '../../app/dist/index.js', outside probably inside node_modules
// see https://github.com/vercel/next.js/issues/51401
// and https://github.com/vercel/next.js/pull/69402
unstable_allowDynamic: ["**/*.js"]
}; };
export default serve({ export default serve({