Change server paradigm
This commit is contained in:
@@ -79,6 +79,13 @@ export default async function genWebHTML({
|
||||
},
|
||||
});
|
||||
|
||||
// let skipped_modules_import_map: { [k: string]: string } = {};
|
||||
|
||||
// [...global.SKIPPED_BROWSER_MODULES].forEach((sk) => {
|
||||
// skipped_modules_import_map[sk] =
|
||||
// "data:text/javascript,export default {}";
|
||||
// });
|
||||
|
||||
let final_component = (
|
||||
<html {...html_props}>
|
||||
<head>
|
||||
@@ -101,6 +108,16 @@ export default async function genWebHTML({
|
||||
}}
|
||||
/>
|
||||
|
||||
{/* {global.SKIPPED_BROWSER_MODULES ? (
|
||||
<script
|
||||
type="importmap"
|
||||
dangerouslySetInnerHTML={{
|
||||
__html: importMap,
|
||||
}}
|
||||
fetchPriority="high"
|
||||
/>
|
||||
) : null} */}
|
||||
|
||||
{bundledMap?.path ? (
|
||||
<>
|
||||
<script
|
||||
|
||||
@@ -1,3 +1,4 @@
|
||||
import _ from "lodash";
|
||||
import type { GrabPageComponentRes } from "../../../types";
|
||||
import isDevelopment from "../../../utils/is-development";
|
||||
import { log } from "../../../utils/log";
|
||||
@@ -53,9 +54,10 @@ export default async function generateWebPageResponseFromComponentReturn({
|
||||
};
|
||||
}
|
||||
|
||||
const cache_page =
|
||||
module.config?.cachePage || serverRes?.cachePage || false;
|
||||
const expiry_seconds = module.config?.cacheExpiry || serverRes?.cacheExpiry;
|
||||
const config = _.merge(root_module?.config, module.config);
|
||||
|
||||
const cache_page = config?.cachePage || serverRes?.cachePage || false;
|
||||
const expiry_seconds = config?.cacheExpiry || serverRes?.cacheExpiry;
|
||||
|
||||
if (cache_page && routeParams?.url) {
|
||||
const key = routeParams.url.pathname + (routeParams.url.search || "");
|
||||
|
||||
@@ -2,6 +2,7 @@ import grabRouteParams from "../../../utils/grab-route-params";
|
||||
import type {
|
||||
BunextPageModule,
|
||||
BunextPageModuleServerReturn,
|
||||
BunextPageServerModule,
|
||||
BunextRootModule,
|
||||
BunxRouteParams,
|
||||
GrabPageComponentRes,
|
||||
@@ -12,6 +13,7 @@ import _ from "lodash";
|
||||
import { log } from "../../../utils/log";
|
||||
import grabRootFilePath from "./grab-root-file-path";
|
||||
import grabPageServerRes from "./grab-page-server-res";
|
||||
import grabPageServerPath from "./grab-page-server-path";
|
||||
|
||||
class NotFoundError extends Error {}
|
||||
|
||||
@@ -77,20 +79,23 @@ export default async function grabPageComponent({
|
||||
}
|
||||
|
||||
const { root_file_path } = grabRootFilePath();
|
||||
|
||||
const module: BunextPageModule = await import(`${file_path}?t=${now}`);
|
||||
const root_module: BunextRootModule | undefined = root_file_path
|
||||
? await import(`${root_file_path}?t=${now}`)
|
||||
: undefined;
|
||||
const { server_file_path: root_server_file_path } = root_file_path
|
||||
? grabPageServerPath({ file_path: root_file_path })
|
||||
: {};
|
||||
const root_server_module: BunextPageServerModule = root_server_file_path
|
||||
? await import(`${root_server_file_path}?t=${now}`)
|
||||
: undefined;
|
||||
|
||||
if (debug) {
|
||||
log.info(`module:`, module);
|
||||
}
|
||||
const root_server_fn =
|
||||
root_server_module?.default || root_server_module?.server;
|
||||
|
||||
const rootServerRes: BunextPageModuleServerReturn | undefined =
|
||||
root_module?.server
|
||||
root_server_fn
|
||||
? await grabPageServerRes({
|
||||
module: root_module,
|
||||
server_function: root_server_fn,
|
||||
url,
|
||||
query: match?.query,
|
||||
routeParams,
|
||||
@@ -101,39 +106,38 @@ export default async function grabPageComponent({
|
||||
log.info(`rootServerRes:`, rootServerRes);
|
||||
}
|
||||
|
||||
const serverRes: BunextPageModuleServerReturn = await grabPageServerRes(
|
||||
{
|
||||
module,
|
||||
url,
|
||||
query: match?.query,
|
||||
routeParams,
|
||||
},
|
||||
);
|
||||
const module: BunextPageModule = await import(`${file_path}?t=${now}`);
|
||||
const { server_file_path } = grabPageServerPath({ file_path });
|
||||
const server_module: BunextPageServerModule = server_file_path
|
||||
? await import(`${server_file_path}?t=${now}`)
|
||||
: undefined;
|
||||
|
||||
if (debug) {
|
||||
log.info(`module:`, module);
|
||||
}
|
||||
|
||||
const server_fn = server_module?.default || server_module?.server;
|
||||
|
||||
const serverRes: BunextPageModuleServerReturn | undefined = server_fn
|
||||
? await grabPageServerRes({
|
||||
server_function: server_fn,
|
||||
url,
|
||||
query: match?.query,
|
||||
routeParams,
|
||||
})
|
||||
: undefined;
|
||||
|
||||
if (debug) {
|
||||
log.info(`serverRes:`, serverRes);
|
||||
}
|
||||
|
||||
const meta = module.meta
|
||||
? typeof module.meta == "function" && routeParams
|
||||
? await module.meta({
|
||||
ctx: routeParams,
|
||||
serverRes,
|
||||
})
|
||||
: typeof module.meta == "object"
|
||||
? module.meta
|
||||
: undefined
|
||||
: undefined;
|
||||
|
||||
if (debug) {
|
||||
log.info(`meta:`, meta);
|
||||
}
|
||||
const mergedServerRes = _.merge(rootServerRes || {}, serverRes || {});
|
||||
|
||||
const { component } =
|
||||
(await grabPageBundledReactComponent({
|
||||
file_path,
|
||||
root_file_path,
|
||||
server_res: serverRes,
|
||||
server_res: mergedServerRes,
|
||||
})) || {};
|
||||
|
||||
if (!component) {
|
||||
@@ -146,7 +150,7 @@ export default async function grabPageComponent({
|
||||
|
||||
return {
|
||||
component,
|
||||
serverRes: _.merge(rootServerRes || {}, serverRes),
|
||||
serverRes: mergedServerRes,
|
||||
routeParams,
|
||||
module,
|
||||
bundledMap,
|
||||
|
||||
@@ -13,10 +13,10 @@ export default function grabPageReactComponentString({
|
||||
server_res,
|
||||
}: Params): string | undefined {
|
||||
try {
|
||||
const target_path = pagePathTransform({ page_path: file_path });
|
||||
const target_root_path = root_file_path
|
||||
? pagePathTransform({ page_path: root_file_path })
|
||||
: undefined;
|
||||
// const target_path = pagePathTransform({ page_path: file_path });
|
||||
// const target_root_path = root_file_path
|
||||
// ? pagePathTransform({ page_path: root_file_path })
|
||||
// : undefined;
|
||||
|
||||
let tsx = ``;
|
||||
|
||||
@@ -24,18 +24,23 @@ export default function grabPageReactComponentString({
|
||||
EJSON.stringify(server_res || {}) ?? "{}",
|
||||
);
|
||||
|
||||
if (target_root_path) {
|
||||
tsx += `import Root from "${target_root_path}"\n`;
|
||||
// Import Root from its original source path so that all sub-components
|
||||
// that import __root (e.g. AppContext) resolve to the same module instance.
|
||||
// Using the rewritten .bunext/pages/__root would create a separate
|
||||
// createContext() call, breaking context for any sub-component that
|
||||
// imports AppContext via a relative path to the source __root.
|
||||
if (root_file_path) {
|
||||
tsx += `import Root from "${root_file_path}"\n`;
|
||||
}
|
||||
|
||||
tsx += `import Page from "${target_path}"\n`;
|
||||
tsx += `import Page from "${file_path}"\n`;
|
||||
tsx += `export default function Main() {\n\n`;
|
||||
tsx += `const props = JSON.parse(${server_res_json})\n\n`;
|
||||
tsx += ` return (\n`;
|
||||
if (target_root_path) {
|
||||
tsx += ` <Root suppressHydrationWarning={true} {...props}><Page {...props} /></Root>\n`;
|
||||
if (root_file_path) {
|
||||
tsx += ` <Root {...props}><Page {...props} /></Root>\n`;
|
||||
} else {
|
||||
tsx += ` <Page suppressHydrationWarning={true} {...props} />\n`;
|
||||
tsx += ` <Page {...props} />\n`;
|
||||
}
|
||||
tsx += ` )\n`;
|
||||
tsx += `}\n`;
|
||||
|
||||
@@ -0,0 +1,18 @@
|
||||
import { existsSync } from "fs";
|
||||
|
||||
type Params = {
|
||||
file_path: string;
|
||||
};
|
||||
|
||||
export default function grabPageServerPath({ file_path }: Params) {
|
||||
const page_server_ts_file = file_path.replace(/\.tsx?$/, ".server.ts");
|
||||
const page_server_tsx_file = file_path.replace(/\.tsx?$/, ".server.tsx");
|
||||
|
||||
const server_file_path = existsSync(page_server_ts_file)
|
||||
? page_server_ts_file
|
||||
: existsSync(page_server_tsx_file)
|
||||
? page_server_tsx_file
|
||||
: undefined;
|
||||
|
||||
return { server_file_path };
|
||||
}
|
||||
@@ -1,6 +1,7 @@
|
||||
import type {
|
||||
BunextPageModule,
|
||||
BunextPageModuleServerReturn,
|
||||
BunextPageServerFn,
|
||||
BunxRouteParams,
|
||||
GrabPageComponentRes,
|
||||
} from "../../../types";
|
||||
@@ -8,7 +9,7 @@ import _ from "lodash";
|
||||
|
||||
type Params = {
|
||||
url?: URL;
|
||||
module: BunextPageModule;
|
||||
server_function: BunextPageServerFn;
|
||||
query?: Record<string, string>;
|
||||
routeParams?: BunxRouteParams;
|
||||
};
|
||||
@@ -17,7 +18,7 @@ export default async function grabPageServerRes({
|
||||
url,
|
||||
query,
|
||||
routeParams,
|
||||
module,
|
||||
server_function,
|
||||
}: Params): Promise<BunextPageModuleServerReturn> {
|
||||
const default_props: BunextPageModuleServerReturn = {
|
||||
url: url
|
||||
@@ -43,7 +44,7 @@ export default async function grabPageServerRes({
|
||||
|
||||
try {
|
||||
if (routeParams) {
|
||||
const serverData = await module["server"]?.({
|
||||
const serverData = await server_function({
|
||||
...routeParams,
|
||||
query: { ...routeParams.query, ...query },
|
||||
});
|
||||
|
||||
@@ -2,6 +2,7 @@ import isDevelopment from "../../../utils/is-development";
|
||||
import tailwindcss from "bun-plugin-tailwind";
|
||||
import grabDirNames from "../../../utils/grab-dir-names";
|
||||
import path from "path";
|
||||
import BunSkipNonBrowserPlugin from "../../bundler/plugins/bun-skip-browser-plugin";
|
||||
|
||||
type Params = {
|
||||
tsx: string;
|
||||
@@ -31,7 +32,7 @@ export default async function grabTsxStringModule<T extends any = any>({
|
||||
|
||||
await Bun.write(src_file_path, tsx);
|
||||
|
||||
await Bun.build({
|
||||
const build = await Bun.build({
|
||||
entrypoints: [src_file_path],
|
||||
format: "esm",
|
||||
target: "bun",
|
||||
@@ -43,7 +44,7 @@ export default async function grabTsxStringModule<T extends any = any>({
|
||||
),
|
||||
},
|
||||
metafile: true,
|
||||
plugins: [tailwindcss],
|
||||
plugins: [tailwindcss, BunSkipNonBrowserPlugin],
|
||||
jsx: {
|
||||
runtime: "automatic",
|
||||
development: dev,
|
||||
|
||||
Reference in New Issue
Block a user