Updates
This commit is contained in:
@@ -50,7 +50,7 @@ export default async function bunextRequestHandler({
|
||||
}
|
||||
|
||||
if (url.pathname === "/__hmr" && is_dev) {
|
||||
response = await handleHmr({ req });
|
||||
return handleHmr({ req });
|
||||
} else if (url.pathname.startsWith("/.bunext")) {
|
||||
response = await handleBunextPublicAssets({ req });
|
||||
} else if (url.pathname.startsWith("/api/")) {
|
||||
|
||||
@@ -17,7 +17,7 @@ type Params = {
|
||||
req: Request;
|
||||
};
|
||||
|
||||
export default async function ({ req }: Params): Promise<Response | undefined> {
|
||||
export default async function ({ req }: Params): Promise<Response> {
|
||||
const url = new URL(req.url);
|
||||
const is_dev = isDevelopment();
|
||||
|
||||
@@ -128,7 +128,7 @@ export default async function ({ req }: Params): Promise<Response | undefined> {
|
||||
return final_res;
|
||||
}
|
||||
|
||||
return undefined;
|
||||
return Response.json({ err: `Route handler error` });
|
||||
}
|
||||
|
||||
// const relative_path = match.filePath.replace(API_DIR, "");
|
||||
|
||||
@@ -62,6 +62,7 @@ export default async function grabPageCombinedServerRes({
|
||||
url,
|
||||
query,
|
||||
routeParams,
|
||||
props: rootServerRes?.props || null,
|
||||
});
|
||||
|
||||
const mergedServerRes = _.merge(rootServerRes || {}, serverRes || {});
|
||||
|
||||
@@ -8,6 +8,10 @@ import grabPageCombinedServerRes from "./grab-page-combined-server-res";
|
||||
import fullRebuild from "../full-rebuild";
|
||||
import serverPostBuildFn from "../server-post-build-fn";
|
||||
import isDevelopment from "../../../utils/is-development";
|
||||
import { existsSync } from "fs";
|
||||
import grabDirNames from "../../../utils/grab-dir-names";
|
||||
|
||||
const { BUNX_BUNDLER_ERROR_EXIT_FILE } = grabDirNames();
|
||||
|
||||
class NotFoundError extends Error {
|
||||
status = 404;
|
||||
@@ -52,6 +56,8 @@ export default async function grabPageComponent(
|
||||
|
||||
let routeParams: BunxRouteParams | undefined = undefined;
|
||||
|
||||
const does_error_file_exist = existsSync(BUNX_BUNDLER_ERROR_EXIT_FILE);
|
||||
|
||||
try {
|
||||
routeParams = req ? await grabRouteParams({ req }) : undefined;
|
||||
|
||||
@@ -83,12 +89,31 @@ export default async function grabPageComponent(
|
||||
throw new Error(errMsg);
|
||||
}
|
||||
|
||||
const bundledMap = global.BUNDLER_CTX_MAP[file_path];
|
||||
let bundledMap = global.BUNDLER_CTX_MAP[file_path];
|
||||
|
||||
if (!bundledMap?.path) {
|
||||
const errMsg = `No Bundled File Path for this request path!`;
|
||||
log.error(errMsg);
|
||||
throw new Error(errMsg);
|
||||
if (does_error_file_exist) {
|
||||
throw new Error(
|
||||
`Application Error. Please Check your components. ${match?.filePath} likely exists but has no exported module.`,
|
||||
);
|
||||
}
|
||||
|
||||
let retries = 0;
|
||||
const MAX_RETRIES = 2;
|
||||
|
||||
while (retries < MAX_RETRIES) {
|
||||
await fullRebuild({
|
||||
msg: `Retrying Bundle map for file \`${file_path}\``,
|
||||
});
|
||||
bundledMap = global.BUNDLER_CTX_MAP[file_path];
|
||||
if (bundledMap?.path) break;
|
||||
}
|
||||
|
||||
if (!bundledMap?.path) {
|
||||
const errMsg = `No Bundled File Path for this request path!`;
|
||||
log.error(errMsg);
|
||||
throw new Error(errMsg);
|
||||
}
|
||||
}
|
||||
|
||||
if (req && !is_hydration) {
|
||||
|
||||
@@ -13,6 +13,7 @@ type Params = {
|
||||
server_function?: BunextPageServerFn;
|
||||
query?: Record<string, string>;
|
||||
routeParams?: BunxRouteParams;
|
||||
props?: Record<string, any> | null;
|
||||
};
|
||||
|
||||
export default async function grabPageServerRes({
|
||||
@@ -20,6 +21,7 @@ export default async function grabPageServerRes({
|
||||
query,
|
||||
routeParams,
|
||||
server_function,
|
||||
props,
|
||||
}: Params): Promise<BunextPageModuleServerReturn> {
|
||||
const default_props: BunextPageModuleServerReturn = {
|
||||
url: url
|
||||
@@ -43,31 +45,28 @@ export default async function grabPageServerRes({
|
||||
query,
|
||||
};
|
||||
|
||||
const init_props = props || null;
|
||||
|
||||
try {
|
||||
if (routeParams && server_function) {
|
||||
const serverData = await server_function({
|
||||
...routeParams,
|
||||
query: { ...routeParams.query, ...query },
|
||||
props: init_props,
|
||||
});
|
||||
|
||||
return {
|
||||
...serverData,
|
||||
...default_props,
|
||||
};
|
||||
return _.merge(default_props, serverData);
|
||||
}
|
||||
|
||||
return {
|
||||
...default_props,
|
||||
};
|
||||
return _.merge(default_props);
|
||||
} catch (error: any) {
|
||||
log.error(
|
||||
`Page ${url?.pathname} Server Error => ${error.message}\n`,
|
||||
error,
|
||||
);
|
||||
|
||||
return {
|
||||
...default_props,
|
||||
return _.merge(default_props, {
|
||||
error: error.message,
|
||||
};
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user