Try local react imports

This commit is contained in:
2026-03-31 06:22:26 +01:00
parent d6985b3335
commit 68d88bce4a
9 changed files with 191 additions and 69 deletions
+1 -1
View File
@@ -48,7 +48,7 @@ export default async function bunextRequestHandler({
if (url.pathname === "/__hmr" && is_dev) {
response = await handleHmr({ req });
} else if (url.pathname.startsWith("/.bunext/public/pages")) {
} else if (url.pathname.startsWith("/.bunext")) {
response = await handleBunextPublicAssets({ req });
} else if (url.pathname.startsWith("/api/")) {
response = await handleRoutes({ req });
@@ -2,6 +2,7 @@ import grabDirNames from "../../utils/grab-dir-names";
import path from "path";
import isDevelopment from "../../utils/is-development";
import { existsSync } from "fs";
import { readFileResponse } from "./handle-public";
const { HYDRATION_DST_DIR } = grabDirNames();
@@ -13,6 +14,45 @@ export default async function ({ req }: Params): Promise<Response> {
try {
const is_dev = isDevelopment();
const url = new URL(req.url);
// switch (url.pathname) {
// case "/.bunext/react":
// return readFileResponse({
// file_path: is_dev
// ? global.DIR_NAMES.REACT_DEVELOPMENT_MODULE
// : global.DIR_NAMES.REACT_PRODUCTION_MODULE,
// });
// case "/.bunext/react-dom":
// return readFileResponse({
// file_path: is_dev
// ? global.DIR_NAMES.REACT_DOM_DEVELOPMENT_MODULE
// : global.DIR_NAMES.REACT_DOM_PRODUCTION_MODULE,
// });
// case "/.bunext/react-dom-client":
// return readFileResponse({
// file_path: is_dev
// ? global.DIR_NAMES.REACT_DOM_CLIENT_DEVELOPMENT_MODULE
// : global.DIR_NAMES.REACT_DOM_CLIENT_PRODUCTION_MODULE,
// });
// case "/.bunext/react-jsx-runtime":
// return readFileResponse({
// file_path: is_dev
// ? global.DIR_NAMES.REACT_JSX_RUNTIME_DEVELOPMENT_MODULE
// : global.DIR_NAMES.REACT_JSX_RUNTIME_PRODUCTION_MODULE,
// });
// case "/.bunext/react-jsx-dev-runtime":
// return readFileResponse({
// file_path: is_dev
// ? global.DIR_NAMES
// .REACT_JSX_DEVELOPMENT_RUNTIME_DEVELOPMENT_MODULE
// : global.DIR_NAMES
// .REACT_JSX_DEVELOPMENT_RUNTIME_PRODUCTION_MODULE,
// });
// default:
// break;
// }
const file_path = path.join(
HYDRATION_DST_DIR,
url.pathname.replace(/\/\.bunext\/public\/pages\//, ""),
@@ -22,14 +62,7 @@ export default async function ({ req }: Params): Promise<Response> {
return new Response("Forbidden", { status: 403 });
}
if (!existsSync(file_path)) {
return new Response(`File Doesn't Exist`, {
status: 404,
});
}
const file = Bun.file(file_path);
return new Response(file);
return readFileResponse({ file_path });
} catch (error) {
return new Response(`File Not Found`, {
status: 404,
+15 -11
View File
@@ -23,20 +23,24 @@ export default async function ({ req }: Params): Promise<Response> {
return new Response("Forbidden", { status: 403 });
}
if (!existsSync(file_path)) {
return new Response(`Public File Doesn't Exist`, {
status: 404,
});
}
const file = Bun.file(file_path);
let res_opts: ResponseInit = {};
return new Response(file, res_opts);
return readFileResponse({ file_path });
} catch (error) {
return new Response(`Public File Not Found`, {
status: 404,
});
}
}
export function readFileResponse({ file_path }: { file_path: string }) {
if (!existsSync(file_path)) {
return new Response(`Public File Doesn't Exist`, {
status: 404,
});
}
const file = Bun.file(file_path);
// let res_opts: ResponseInit = {};
return new Response(file);
}
@@ -1,4 +1,4 @@
import { renderToString } from "react-dom/server";
import { renderToReadableStream, renderToString } from "react-dom/server";
import grabContants from "../../../utils/grab-constants";
import EJSON from "../../../utils/ejson";
import type { LivePageDistGenParams } from "../../../types";
@@ -10,6 +10,9 @@ import { AppData } from "../../../data/app-data";
import { readFileSync } from "fs";
import path from "path";
import _ from "lodash";
import grabDirNames from "../../../utils/grab-dir-names";
const {} = grabDirNames();
let _reactVersion = "19";
try {
@@ -70,6 +73,15 @@ export default async function genWebHTML({
const dev = isDevelopment();
const devSuffix = dev ? "?dev" : "";
// const browser_imports: Record<string, string> = {
// react: `/.bunext/react`,
// "react-dom": `/.bunext/react-dom`,
// "react-dom/client": `/.bunext/react-dom-client`,
// "react/jsx-runtime": `/.bunext/react-jsx-runtime`,
// "react/jsx-dev-runtime": `/.bunext/react-jsx-dev-runtime`,
// };
const browser_imports: Record<string, string> = {
react: `https://esm.sh/react@${_reactVersion}`,
"react-dom": `https://esm.sh/react-dom@${_reactVersion}`,
@@ -110,15 +122,10 @@ export default async function genWebHTML({
}}
/>
{/* {global.SKIPPED_BROWSER_MODULES ? (
<script
type="importmap"
dangerouslySetInnerHTML={{
__html: importMap,
}}
fetchPriority="high"
/>
) : null} */}
{RootHead ? (
<RootHead serverRes={pageProps} ctx={routeParams} />
) : null}
{Head ? <Head serverRes={pageProps} ctx={routeParams} /> : null}
{bundledMap?.path ? (
<>
@@ -146,11 +153,6 @@ export default async function genWebHTML({
}}
/>
) : null}
{RootHead ? (
<RootHead serverRes={pageProps} ctx={routeParams} />
) : null}
{Head ? <Head serverRes={pageProps} ctx={routeParams} /> : null}
</head>
<body>
<div
@@ -165,21 +167,21 @@ export default async function genWebHTML({
let html = `<!DOCTYPE html>\n`;
// const stream = await renderToReadableStream(final_component, {
// onError(error: any) {
// // This is where you "omit" or handle the errors
// // You can log it silently or ignore it
// if (error.message.includes('unique "key" prop')) return;
// console.error(error);
// },
// });
const stream = await renderToReadableStream(final_component, {
onError(error: any) {
// This is where you "omit" or handle the errors
// You can log it silently or ignore it
if (error.message.includes('unique "key" prop')) return;
console.error(error);
},
});
// // 2. Convert the Web Stream to a String (Bun-optimized)
// const htmlBody = await new Response(stream).text();
// 2. Convert the Web Stream to a String (Bun-optimized)
const htmlBody = await new Response(stream).text();
// html += htmlBody;
html += htmlBody;
html += renderToString(final_component);
// html += renderToString(final_component);
return html;
}