aboutsummaryrefslogtreecommitdiff
path: root/demos/hello-next/bun-framework-next/page-loader.ts
diff options
context:
space:
mode:
authorGravatar Jack Hanford <jackhanford@gmail.com> 2021-08-18 16:40:23 -0700
committerGravatar Jack Hanford <jackhanford@gmail.com> 2021-08-18 16:40:23 -0700
commitabdc26a5fc495092c865f89091bd39242fdb2b07 (patch)
tree78e71af008a042a73b1dcea8868fe43ce18f67f7 /demos/hello-next/bun-framework-next/page-loader.ts
parent306c7dda6189521b44253eaf4696eb1ea1b1227f (diff)
downloadbun-abdc26a5fc495092c865f89091bd39242fdb2b07.tar.gz
bun-abdc26a5fc495092c865f89091bd39242fdb2b07.tar.zst
bun-abdc26a5fc495092c865f89091bd39242fdb2b07.zip
Get most of the Next.js router working
Former-commit-id: 3521bd1bb606f164f6ef1cdc4cfaae1663c22891
Diffstat (limited to 'demos/hello-next/bun-framework-next/page-loader.ts')
-rw-r--r--demos/hello-next/bun-framework-next/page-loader.ts57
1 files changed, 44 insertions, 13 deletions
diff --git a/demos/hello-next/bun-framework-next/page-loader.ts b/demos/hello-next/bun-framework-next/page-loader.ts
index 62cfab583..84e623ab1 100644
--- a/demos/hello-next/bun-framework-next/page-loader.ts
+++ b/demos/hello-next/bun-framework-next/page-loader.ts
@@ -1,4 +1,6 @@
import NextPageLoader from "next/dist/client/page-loader";
+import getAssetPathFromRoute from "next/dist/shared/lib/router/utils/get-asset-path-from-route";
+import createRouteLoader from "./route-loader";
export default class PageLoader extends NextPageLoader {
public routeLoader: RouteLoader;
@@ -6,6 +8,8 @@ export default class PageLoader extends NextPageLoader {
constructor(_, __, pages) {
super(_, __);
+ // TODO: assetPrefix?
+ this.routeLoader = createRouteLoader("");
this.pages = pages;
}
@@ -13,20 +17,47 @@ export default class PageLoader extends NextPageLoader {
return Object.keys(this.pages);
}
- loadPage(route: string): Promise<GoodPageCache> {
- return this.routeLoader.loadRoute(route).then((res) => {
- if ("component" in res) {
- return {
- page: res.component,
- mod: res.exports,
- styleSheets: res.styles.map((o) => ({
- href: o.href,
- text: o.content,
- })),
- };
+ async loadPage(route: string): Promise<GoodPageCache> {
+ try {
+ const assets =
+ globalThis.__NEXT_DATA__.pages[route] ||
+ globalThis.__NEXT_DATA__.pages[getAssetPathFromRoute(route)];
+
+ var src;
+ console.log(getAssetPathFromRoute(route), assets);
+ for (let asset of assets) {
+ if (!asset.endsWith(".css")) {
+ src = asset;
+ break;
+ }
}
- throw res.error;
- });
+
+ console.assert(src, "Invalid or unknown route passed to loadPage");
+ const res = await import(src);
+ console.log({ res });
+
+ return {
+ page: res.default,
+ mod: res,
+ __N_SSG: false,
+ __N_SSP: false,
+ };
+ } catch (err) {}
+
+ // return this.routeLoader.loadRoute(route).then((res) => {
+ // debugger;
+ // if ("component" in res) {
+ // return {
+ // page: res.component,
+ // mod: res.exports,
+ // styleSheets: res.styles.map((o) => ({
+ // href: o.href,
+ // text: o.content,
+ // })),
+ // };
+ // }
+ // throw res.error;
+ // });
}
// not used in development!