This commit is contained in:
Benjamin Toby 2026-03-29 17:08:10 +01:00
parent 950bdc3dca
commit 2b170d24e1
11 changed files with 27 additions and 13 deletions

View File

@ -1,5 +1,6 @@
type Params = { type Params = {
req: Request; req: Request;
server: Bun.Server<any>;
}; };
export default function bunextRequestHandler({ req: initial_req, }: Params): Promise<Response>; export default function bunextRequestHandler({ req: initial_req, server, }: Params): Promise<Response>;
export {}; export {};

View File

@ -6,7 +6,7 @@ import handleHmr from "./handle-hmr";
import handlePublic from "./handle-public"; import handlePublic from "./handle-public";
import handleFiles from "./handle-files"; import handleFiles from "./handle-files";
import handleBunextPublicAssets from "./handle-bunext-public-assets"; import handleBunextPublicAssets from "./handle-bunext-public-assets";
export default async function bunextRequestHandler({ req: initial_req, }) { export default async function bunextRequestHandler({ req: initial_req, server, }) {
const is_dev = isDevelopment(); const is_dev = isDevelopment();
let req = initial_req.clone(); let req = initial_req.clone();
try { try {
@ -25,6 +25,10 @@ export default async function bunextRequestHandler({ req: initial_req, }) {
req = middleware_res; req = middleware_res;
} }
} }
// const server_upgrade = server.upgrade(req);
// if (server_upgrade) {
// return undefined;
// }
if (url.pathname === "/__hmr" && is_dev) { if (url.pathname === "/__hmr" && is_dev) {
response = await handleHmr({ req }); response = await handleHmr({ req });
} }

View File

@ -9,7 +9,7 @@ export default async function () {
const config = await grabConfig(); const config = await grabConfig();
return { return {
async fetch(req, server) { async fetch(req, server) {
return await bunextRequestHandler({ req }); return await bunextRequestHandler({ req, server });
}, },
port, port,
idleTimeout: development ? 0 : undefined, idleTimeout: development ? 0 : undefined,

View File

@ -14,7 +14,7 @@ export default async function serverPostBuildFn() {
} }
const target_artifact = global.BUNDLER_CTX_MAP[controller.target_map.local_path]; const target_artifact = global.BUNDLER_CTX_MAP[controller.target_map.local_path];
const mock_req = new Request(controller.page_url); const mock_req = new Request(controller.page_url);
const { serverRes } = await grabPageComponent({ const { serverRes, bundledMap, module, root_module } = await grabPageComponent({
req: mock_req, req: mock_req,
}); });
const final_artifact = { const final_artifact = {

View File

@ -62,7 +62,7 @@ export default async function genWebHTML({ component, pageProps, bundledMap, mod
__html: `window.${ClientWindowPagePropsName} = ${serializedProps}`, __html: `window.${ClientWindowPagePropsName} = ${serializedProps}`,
} }), bundledMap?.path ? (_jsxs(_Fragment, { children: [_jsx("script", { type: "importmap", dangerouslySetInnerHTML: { } }), bundledMap?.path ? (_jsxs(_Fragment, { children: [_jsx("script", { type: "importmap", dangerouslySetInnerHTML: {
__html: importMap, __html: importMap,
}, fetchPriority: "high" }), _jsx("script", { src: `/${bundledMap.path}`, type: "module", id: AppData["BunextClientHydrationScriptID"], defer: true })] })) : null, is_dev ? (_jsx("script", { defer: true, dangerouslySetInnerHTML: { }, defer: true }), _jsx("script", { src: `/${bundledMap.path}`, type: "module", id: AppData["BunextClientHydrationScriptID"], defer: true })] })) : null, is_dev ? (_jsx("script", { defer: true, dangerouslySetInnerHTML: {
__html: page_hydration_script, __html: page_hydration_script,
} })) : null, RootHead ? (_jsx(RootHead, { serverRes: pageProps, ctx: routeParams })) : null, Head ? _jsx(Head, { serverRes: pageProps, ctx: routeParams }) : null] }), _jsx("body", { children: _jsx("div", { id: ClientRootElementIDName, suppressHydrationWarning: !dev, children: component }) })] })); } })) : null, RootHead ? (_jsx(RootHead, { serverRes: pageProps, ctx: routeParams })) : null, Head ? _jsx(Head, { serverRes: pageProps, ctx: routeParams }) : null] }), _jsx("body", { children: _jsx("div", { id: ClientRootElementIDName, suppressHydrationWarning: !dev, children: component }) })] }));
let html = `<!DOCTYPE html>\n`; let html = `<!DOCTYPE html>\n`;

View File

@ -14,8 +14,8 @@ await bunext.bunextInit();
const server = Bun.serve({ const server = Bun.serve({
routes: { routes: {
"/*": { "/*": {
async GET(req) { async GET(req, server) {
return await bunext.bunextRequestHandler({ req }); return await bunext.bunextRequestHandler({ req, server });
}, },
}, },
}, },

View File

@ -2,7 +2,7 @@
"name": "@moduletrace/bunext", "name": "@moduletrace/bunext",
"module": "index.ts", "module": "index.ts",
"type": "module", "type": "module",
"version": "1.0.40", "version": "1.0.41",
"main": "dist/index.js", "main": "dist/index.js",
"types": "dist/index.d.ts", "types": "dist/index.d.ts",
"exports": { "exports": {

View File

@ -8,10 +8,12 @@ import handleFiles from "./handle-files";
import handleBunextPublicAssets from "./handle-bunext-public-assets"; import handleBunextPublicAssets from "./handle-bunext-public-assets";
type Params = { type Params = {
req: Request; req: Request;
server: Bun.Server<any>;
}; };
export default async function bunextRequestHandler({ export default async function bunextRequestHandler({
req: initial_req, req: initial_req,
server,
}: Params): Promise<Response> { }: Params): Promise<Response> {
const is_dev = isDevelopment(); const is_dev = isDevelopment();
let req = initial_req.clone(); let req = initial_req.clone();
@ -38,6 +40,12 @@ export default async function bunextRequestHandler({
} }
} }
// const server_upgrade = server.upgrade(req);
// if (server_upgrade) {
// return undefined;
// }
if (url.pathname === "/__hmr" && is_dev) { if (url.pathname === "/__hmr" && is_dev) {
response = await handleHmr({ req }); response = await handleHmr({ req });
} else if (url.pathname.startsWith("/.bunext/public/pages")) { } else if (url.pathname.startsWith("/.bunext/public/pages")) {

View File

@ -12,7 +12,7 @@ export default async function (): Promise<Bun.Serve.Options<any>> {
return { return {
async fetch(req, server) { async fetch(req, server) {
return await bunextRequestHandler({ req }); return await bunextRequestHandler({ req, server });
}, },
port, port,
idleTimeout: development ? 0 : undefined, idleTimeout: development ? 0 : undefined,

View File

@ -23,9 +23,10 @@ export default async function serverPostBuildFn() {
const mock_req = new Request(controller.page_url); const mock_req = new Request(controller.page_url);
const { serverRes } = await grabPageComponent({ const { serverRes, bundledMap, module, root_module } =
req: mock_req, await grabPageComponent({
}); req: mock_req,
});
const final_artifact: Omit<GlobalHMRControllerObject, "controller"> = { const final_artifact: Omit<GlobalHMRControllerObject, "controller"> = {
..._.omit(controller, ["controller"]), ..._.omit(controller, ["controller"]),

View File

@ -125,7 +125,7 @@ export default async function genWebHTML({
dangerouslySetInnerHTML={{ dangerouslySetInnerHTML={{
__html: importMap, __html: importMap,
}} }}
fetchPriority="high" defer
/> />
<script <script
src={`/${bundledMap.path}`} src={`/${bundledMap.path}`}