From cb18bf7e5d3657313359c448bcc24ff3f27f2faa Mon Sep 17 00:00:00 2001
From: Jack Hanford <jackhanford@gmail.com>
Date: Mon, 15 Nov 2021 16:21:57 -0800
Subject: begin working on renderDocument

---
 packages/bun-framework-next/renderDocument.tsx | 23 +++++++++++++++++------
 1 file changed, 17 insertions(+), 6 deletions(-)

(limited to 'packages/bun-framework-next/renderDocument.tsx')

diff --git a/packages/bun-framework-next/renderDocument.tsx b/packages/bun-framework-next/renderDocument.tsx
index 59e18fbe7..d0ceb8599 100644
--- a/packages/bun-framework-next/renderDocument.tsx
+++ b/packages/bun-framework-next/renderDocument.tsx
@@ -18,6 +18,7 @@ import {
   RenderPageResult,
   HtmlContext,
 } from "next/dist/shared/lib/utils";
+import { RenderOpts } from "next/dist/server/render";
 import * as NextDocument from "next/document";
 import * as ReactDOMServer from "react-dom/server.browser";
 import * as React from "react";
@@ -70,6 +71,12 @@ const notImplementedProxy = (base) =>
     }
   );
 
+type DocumentFiles = {
+  sharedFiles: readonly string[];
+  pageFiles: readonly string[];
+  allFiles: readonly string[];
+};
+
 function getScripts(files: DocumentFiles) {
   const { context, props } = this;
   const {
@@ -147,6 +154,9 @@ function renderDocument(
     disableOptimizedLoading,
   }: RenderOpts & {
     props: any;
+    //
+    page: string;
+    //
     docComponentsRendered: DocumentProps["docComponentsRendered"];
     docProps: DocumentInitialProps;
     pathname: string;
@@ -454,7 +464,9 @@ export async function render({
 
   const hasPageGetInitialProps = !!(Component as any).getInitialProps;
   const pageIsDynamic = route.kind === "dynamic";
+  const isPreview = false;
   const isAutoExport = false;
+  const nextExport = isAutoExport || isFallback;
 
   if (isAutoExport || isFallback) {
     // // remove query values except ones that will be set during export
@@ -476,7 +488,6 @@ export async function render({
     <meta name="viewport" content="width=device-width" />,
   ];
 
-  const nextExport = isAutoExport || isFallback;
   const reactLoadableModules: string[] = [];
   var scriptLoader = {};
   const AppContainer = ({ children }: any) => (
@@ -728,8 +739,6 @@ export async function render({
   // });
   const docComponentsRendered: DocumentProps["docComponentsRendered"] = {};
 
-  const isPreview = false;
-
   let html = renderDocument(Document, {
     docComponentsRendered,
     ...renderOpts,
@@ -743,11 +752,13 @@ export async function render({
       pages: pages,
     },
     // Only enabled in production as development mode has features relying on HMR (style injection for example)
+    // @ts-expect-error
     unstable_runtimeJS: true,
     //   process.env.NODE_ENV === "production"
     //     ? pageConfig.unstable_runtimeJS
     //     : undefined,
     // unstable_JsPreload: pageConfig.unstable_JsPreload,
+    // @ts-expect-error
     unstable_JsPreload: true,
     dangerousAsPath: router.asPath,
     ampState: undefined,
@@ -770,9 +781,9 @@ export async function render({
     appGip: !defaultAppGetInitialProps ? true : undefined,
     devOnlyCacheBusterQueryString: "",
     scriptLoader,
-    isPreview: isPreview === true ? true : undefined,
-    autoExport: isAutoExport === true ? true : undefined,
-    nextExport: nextExport === true ? true : undefined,
+    isPreview: isPreview,
+    autoExport: isAutoExport,
+    nextExport: nextExport,
     useMaybeDeferContent,
   });
   const bodyRenderIdx = html.indexOf(BODY_RENDER_TARGET);
-- 
cgit v1.2.3


From a79153f6e0d8f41a99b9b37bd791217884f91f10 Mon Sep 17 00:00:00 2001
From: Jack Hanford <jackhanford@gmail.com>
Date: Mon, 15 Nov 2021 16:34:22 -0800
Subject: remove unused proxy shim

---
 packages/bun-framework-next/renderDocument.tsx | 38 ++------------------------
 1 file changed, 2 insertions(+), 36 deletions(-)

(limited to 'packages/bun-framework-next/renderDocument.tsx')

diff --git a/packages/bun-framework-next/renderDocument.tsx b/packages/bun-framework-next/renderDocument.tsx
index d0ceb8599..5b4cab0ce 100644
--- a/packages/bun-framework-next/renderDocument.tsx
+++ b/packages/bun-framework-next/renderDocument.tsx
@@ -36,41 +36,6 @@ const isJSFile = (file: string) =>
   file.endsWith(".ts") ||
   file.endsWith(".tsx");
 
-const notImplementedProxy = (base) =>
-  new Proxy(
-    {},
-    {
-      deleteProperty: function (target, prop) {
-        return undefined;
-      },
-      enumerate: function (oTarget, sKey) {
-        return [].entries();
-      },
-      ownKeys: function (oTarget, sKey) {
-        return [].values();
-      },
-      has: function (oTarget, sKey) {
-        return false;
-      },
-      defineProperty: function (oTarget, sKey, oDesc) {
-        return undefined;
-      },
-      getOwnPropertyDescriptor: function (oTarget, sKey) {
-        return undefined;
-      },
-      get(this, prop) {
-        throw new ReferenceError(
-          `${base} is not available for this environment.`
-        );
-      },
-      set(this, prop, value) {
-        throw new ReferenceError(
-          `${base} is not available for this environment.`
-        );
-      },
-    }
-  );
-
 type DocumentFiles = {
   sharedFiles: readonly string[];
   pageFiles: readonly string[];
@@ -458,7 +423,7 @@ export async function render({
   delete query.__nextDefaultLocale;
 
   const isSSG = !!getStaticProps;
-  const isBuildTimeSSG = isSSG && false;
+
   const defaultAppGetInitialProps =
     App.getInitialProps === (App as any).origGetInitialProps;
 
@@ -515,6 +480,7 @@ export async function render({
     </RouterContext.Provider>
   );
 
+  // Todo: Double check this when adding support for dynamic()
   await Loadable.preloadAll(); // Make sure all dynamic imports are loaded
 
   const router = new ServerRouter(
-- 
cgit v1.2.3


From c06e34c687187a528d0da39eddec71323b56e77a Mon Sep 17 00:00:00 2001
From: Jack Hanford <jackhanford@gmail.com>
Date: Mon, 22 Nov 2021 10:23:14 -0800
Subject: misc

---
 packages/bun-framework-next/renderDocument.tsx   | 24 +++++++++---------------
 packages/bun-framework-next/server-polyfills.tsx |  7 ++-----
 2 files changed, 11 insertions(+), 20 deletions(-)

(limited to 'packages/bun-framework-next/renderDocument.tsx')

diff --git a/packages/bun-framework-next/renderDocument.tsx b/packages/bun-framework-next/renderDocument.tsx
index 5b4cab0ce..661bebdc0 100644
--- a/packages/bun-framework-next/renderDocument.tsx
+++ b/packages/bun-framework-next/renderDocument.tsx
@@ -1,4 +1,4 @@
-import * as App from "next/app";
+import App from "next/app";
 import { AmpStateContext } from "next/dist/shared/lib/amp-context";
 import { HeadManagerContext } from "next/dist/shared/lib/head-manager-context";
 import Loadable from "next/dist/shared/lib/loadable";
@@ -23,7 +23,9 @@ import * as NextDocument from "next/document";
 import * as ReactDOMServer from "react-dom/server.browser";
 import * as React from "react";
 import * as ReactIs from "react-is";
-import { BODY_RENDER_TARGET } from "next/constants";
+
+// This constant was removed from Next, Is it doing anything?
+const BODY_RENDER_TARGET = "__NEXT_BODY_RENDER_TARGET__";
 
 const dev = process.env.NODE_ENV === "development";
 
@@ -362,11 +364,8 @@ export async function render({
     pages["/_app"] = [];
   }
 
-  const AppComponent = AppComponent_ || App.default;
-  const Document =
-    (DocumentNamespace && DocumentNamespace.default) || NextDocument.default;
-  //   Document.Html.prototype.getScripts = getScripts;
-  // }
+  const AppComponent = AppComponent_ || App;
+  const Document = (DocumentNamespace as any)?.default || NextDocument.default;
 
   const callMiddleware = async (method: string, args: any[], props = false) => {
     let results: any = props ? {} : [];
@@ -422,7 +421,7 @@ export async function render({
   delete query.__nextLocale;
   delete query.__nextDefaultLocale;
 
-  const isSSG = !!getStaticProps;
+  // const isSSG = !!getStaticProps;
 
   const defaultAppGetInitialProps =
     App.getInitialProps === (App as any).origGetInitialProps;
@@ -518,7 +517,7 @@ export async function render({
       );
     },
     defaultGetInitialProps: async (
-      docCtx: DocumentContext
+      docCtx: NextDocument.DocumentContext
     ): Promise<DocumentInitialProps> => {
       const enhanceApp = (AppComp: any) => {
         return (props: any) => <AppComp {...props} />;
@@ -696,13 +695,7 @@ export async function render({
   const renderOpts = {
     params: route.params,
   };
-  // renderOpts.params = _params || params;
 
-  // parsedUrl.pathname = denormalizePagePath(parsedUrl.pathname!);
-  // renderOpts.resolvedUrl = formatUrl({
-  //   ...parsedUrl,
-  //   query: origQuery,
-  // });
   const docComponentsRendered: DocumentProps["docComponentsRendered"] = {};
 
   let html = renderDocument(Document, {
@@ -752,6 +745,7 @@ export async function render({
     nextExport: nextExport,
     useMaybeDeferContent,
   });
+  // __NEXT_BODY_RENDER_TARGET__
   const bodyRenderIdx = html.indexOf(BODY_RENDER_TARGET);
   html =
     html.substring(0, bodyRenderIdx) +
diff --git a/packages/bun-framework-next/server-polyfills.tsx b/packages/bun-framework-next/server-polyfills.tsx
index 887621978..57960e4fa 100644
--- a/packages/bun-framework-next/server-polyfills.tsx
+++ b/packages/bun-framework-next/server-polyfills.tsx
@@ -3,12 +3,9 @@ globalThis.global = globalThis;
 import { Buffer } from "buffer";
 import { URL } from "url-polyfill";
 import { TextEncoder, TextDecoder } from "fast-text-encoding";
-
-globalThis.Buffer = Buffer;
-
 import * as React from "react";
 
-var onlyChildPolyfill = React.Children.only;
+const onlyChildPolyfill = React.Children.only;
 
 React.Children.only = function (children) {
   if (
@@ -22,7 +19,7 @@ React.Children.only = function (children) {
   return onlyChildPolyfill(children);
 };
 
+globalThis.Buffer = Buffer;
 globalThis.URL = URL;
-
 globalThis.TextEncoder ||= TextEncoder;
 globalThis.TextDecoder ||= TextDecoder;
-- 
cgit v1.2.3


From f747fd9dfa19ac9edc839161d0adaeb962e2ade0 Mon Sep 17 00:00:00 2001
From: Jack Hanford <jackhanford@gmail.com>
Date: Mon, 22 Nov 2021 21:25:57 -0500
Subject: ts-expect-error the remaining ts-errors

---
 packages/bun-framework-next/client.development.tsx |  3 +++
 packages/bun-framework-next/renderDocument.tsx     | 26 +++++++++++++++++-----
 packages/bun-framework-next/server.development.tsx |  2 ++
 3 files changed, 25 insertions(+), 6 deletions(-)

(limited to 'packages/bun-framework-next/renderDocument.tsx')

diff --git a/packages/bun-framework-next/client.development.tsx b/packages/bun-framework-next/client.development.tsx
index bf03af289..aad808965 100644
--- a/packages/bun-framework-next/client.development.tsx
+++ b/packages/bun-framework-next/client.development.tsx
@@ -289,6 +289,9 @@ export async function _boot(EntryPointNamespace, isError) {
 
   const appScripts = globalThis.__NEXT_DATA__.pages["/_app"];
 
+  //  Type 'typeof App' is not assignable to type 'ComponentClass<AppProps, any>'.
+  //  Construct signature return types 'App<any, any, any>' and 'Component<AppProps, any, any>' are incompatible.
+  // @ts-expect-error
   CachedApp = NextApp;
 
   if (appScripts && appScripts.length > 0) {
diff --git a/packages/bun-framework-next/renderDocument.tsx b/packages/bun-framework-next/renderDocument.tsx
index 661bebdc0..417e200ac 100644
--- a/packages/bun-framework-next/renderDocument.tsx
+++ b/packages/bun-framework-next/renderDocument.tsx
@@ -31,6 +31,9 @@ const dev = process.env.NODE_ENV === "development";
 
 type ParsedUrlQuery = Record<string, string | string[]>;
 
+// @ts-expect-error TS doesn't understand that Bun is a global
+const origin: string = Bun.origin;
+
 const isJSFile = (file: string) =>
   file.endsWith(".js") ||
   file.endsWith(".jsx") ||
@@ -199,7 +202,11 @@ function renderDocument(
     "<!DOCTYPE html>" +
     ReactDOMServer.renderToStaticMarkup(
       <AmpStateContext.Provider value={ampState}>
+        {/* HTMLContextProvider expects useMainContent */}
+        {/* @ts-expect-error */}
         <HtmlContext.Provider value={htmlProps}>
+          {/* Document doesn't expect useMaybeDeferContent */}
+          {/* @ts-expect-error */}
           <Document {...htmlProps} {...docProps}></Document>
         </HtmlContext.Provider>
       </AmpStateContext.Provider>
@@ -336,7 +343,8 @@ export async function render({
   routeNames: string[];
   request: Request;
 }): Promise<Response> {
-  const { default: Component, getStaticProps = null } = PageNamespace || {};
+  const { default: Component } = PageNamespace || {};
+  const getStaticProps = (PageNamespace as any).getStaticProps || null;
   const { default: AppComponent_ } = AppNamespace || {};
   var query = Object.assign({}, route.query);
 
@@ -348,7 +356,7 @@ export async function render({
   for (let i = 0; i < routeNames.length; i++) {
     const filePath = routePaths[i];
     const name = routeNames[i];
-    pages[name] = [Bun.origin + filePath];
+    pages[name] = [origin + filePath];
   }
 
   if (appStylesheets.length > 0) {
@@ -490,7 +498,7 @@ export async function render({
       isFallback: isFallback,
     },
     true,
-    Bun.origin,
+    origin,
     null,
     [], // renderOpts.locales,
     null, //renderOpts.defaultLocale,
@@ -529,7 +537,7 @@ export async function render({
     },
   };
 
-  var props = await loadGetInitialProps(AppComponent, {
+  var props: any = await loadGetInitialProps(AppComponent, {
     AppTree: ctx.AppTree,
     Component,
     router,
@@ -541,6 +549,7 @@ export async function render({
   // We don't call getServerSideProps on clients.
   // This isn't correct.
   // We don't call getServerSideProps on clients.
+  // @ts-expect-error
   const getServerSideProps = PageNamespace.getServerSideProps;
 
   var responseHeaders: Headers;
@@ -637,6 +646,7 @@ export async function render({
 
   const renderToString = ReactDOMServer.renderToString;
   const ErrorDebug = null;
+
   props.pageProps = pageProps;
 
   const renderPage: RenderPage = (
@@ -659,6 +669,8 @@ export async function render({
     }
 
     const { App: EnhancedApp, Component: EnhancedComponent } =
+      // Argument of type 'NextComponentType<any, {}, {}> | typeof App' is not assignable to parameter of type 'AppType'.
+      // @ts-expect-error
       enhanceComponents(options, AppComponent, Component);
 
     const htmlOrPromise = renderToString(
@@ -702,13 +714,15 @@ export async function render({
     docComponentsRendered,
     ...renderOpts,
     disableOptimizedLoading: false,
-    canonicalBase: Bun.origin,
+    canonicalBase: origin,
     buildManifest: {
       devFiles: [],
       allFiles: [],
       polyfillFiles: [],
       lowPriorityFiles: [],
-      pages: pages,
+      // buildManifest doesn't expect pages, even though its used
+      // @ts-expect-error
+      pages,
     },
     // Only enabled in production as development mode has features relying on HMR (style injection for example)
     // @ts-expect-error
diff --git a/packages/bun-framework-next/server.development.tsx b/packages/bun-framework-next/server.development.tsx
index 96ba55812..4ac2621e8 100644
--- a/packages/bun-framework-next/server.development.tsx
+++ b/packages/bun-framework-next/server.development.tsx
@@ -26,6 +26,8 @@ import(Bun.routesDir + "_document").then(
     DocumentLoaded = true;
   },
   (err) => {
+    // ResolveError is defined outside of bun-framework-next in ../../src/runtime/errors
+    // @ts-expect-error
     if (err instanceof ResolveError) {
       DocumentLoaded = true;
     } else {
-- 
cgit v1.2.3


From 25a49005cc4e8ec46323c2edb5bc2826117c5266 Mon Sep 17 00:00:00 2001
From: Jack Hanford <jackhanford@gmail.com>
Date: Mon, 22 Nov 2021 21:58:35 -0500
Subject: another try

---
 packages/bun-framework-next/libs.d.ts          | 4 ----
 packages/bun-framework-next/renderDocument.tsx | 2 +-
 2 files changed, 1 insertion(+), 5 deletions(-)
 delete mode 100644 packages/bun-framework-next/libs.d.ts

(limited to 'packages/bun-framework-next/renderDocument.tsx')

diff --git a/packages/bun-framework-next/libs.d.ts b/packages/bun-framework-next/libs.d.ts
deleted file mode 100644
index b8bd3dfaf..000000000
--- a/packages/bun-framework-next/libs.d.ts
+++ /dev/null
@@ -1,4 +0,0 @@
-declare module "react-dom/server.browser" {
-  const renderToStaticMarkup: any;
-  const renderToString: any;
-}
diff --git a/packages/bun-framework-next/renderDocument.tsx b/packages/bun-framework-next/renderDocument.tsx
index 417e200ac..4832c9eb2 100644
--- a/packages/bun-framework-next/renderDocument.tsx
+++ b/packages/bun-framework-next/renderDocument.tsx
@@ -20,7 +20,7 @@ import {
 } from "next/dist/shared/lib/utils";
 import { RenderOpts } from "next/dist/server/render";
 import * as NextDocument from "next/document";
-import * as ReactDOMServer from "react-dom/server.browser";
+import * as ReactDOMServer from "react-dom/server";
 import * as React from "react";
 import * as ReactIs from "react-is";
 
-- 
cgit v1.2.3


From 1d01ffeb9e3fa90b009afe08a43b34843856c074 Mon Sep 17 00:00:00 2001
From: Jack Hanford <jackhanford@gmail.com>
Date: Mon, 22 Nov 2021 22:19:53 -0500
Subject: remove .then

---
 packages/bun-framework-next/renderDocument.tsx | 15 +++------------
 1 file changed, 3 insertions(+), 12 deletions(-)

(limited to 'packages/bun-framework-next/renderDocument.tsx')

diff --git a/packages/bun-framework-next/renderDocument.tsx b/packages/bun-framework-next/renderDocument.tsx
index 4832c9eb2..218d21a3c 100644
--- a/packages/bun-framework-next/renderDocument.tsx
+++ b/packages/bun-framework-next/renderDocument.tsx
@@ -654,12 +654,7 @@ export async function render({
   ): RenderPageResult | Promise<RenderPageResult> => {
     if (ctx.err && ErrorDebug) {
       const htmlOrPromise = renderToString(<ErrorDebug error={ctx.err} />);
-      return typeof htmlOrPromise === "string"
-        ? { html: htmlOrPromise, head }
-        : htmlOrPromise.then((html) => ({
-            html,
-            head,
-          }));
+      return { html: htmlOrPromise, head };
     }
 
     if (dev && (props.router || props.Component)) {
@@ -683,12 +678,8 @@ export async function render({
         />
       </AppContainer>
     );
-    return typeof htmlOrPromise === "string"
-      ? { html: htmlOrPromise, head }
-      : htmlOrPromise.then((html) => ({
-          html,
-          head,
-        }));
+
+    return { html: htmlOrPromise, head };
   };
 
   const documentCtx = { ...ctx, renderPage };
-- 
cgit v1.2.3


From cdeb39bd7f0f2fa280f2c0ab833b789d3099f3ed Mon Sep 17 00:00:00 2001
From: Jack Hanford <jackhanford@gmail.com>
Date: Tue, 23 Nov 2021 10:42:04 -0500
Subject: begin addressing more feedback

---
 packages/bun-framework-next/renderDocument.tsx | 6 +++---
 1 file changed, 3 insertions(+), 3 deletions(-)

(limited to 'packages/bun-framework-next/renderDocument.tsx')

diff --git a/packages/bun-framework-next/renderDocument.tsx b/packages/bun-framework-next/renderDocument.tsx
index 218d21a3c..9cc6654c7 100644
--- a/packages/bun-framework-next/renderDocument.tsx
+++ b/packages/bun-framework-next/renderDocument.tsx
@@ -20,7 +20,7 @@ import {
 } from "next/dist/shared/lib/utils";
 import { RenderOpts } from "next/dist/server/render";
 import * as NextDocument from "next/document";
-import * as ReactDOMServer from "react-dom/server";
+import * as ReactDOMServer from "react-dom/server.browser";
 import * as React from "react";
 import * as ReactIs from "react-is";
 
@@ -344,7 +344,7 @@ export async function render({
   request: Request;
 }): Promise<Response> {
   const { default: Component } = PageNamespace || {};
-  const getStaticProps = (PageNamespace as any).getStaticProps || null;
+  const getStaticProps = (PageNamespace as any)?.getStaticProps || null;
   const { default: AppComponent_ } = AppNamespace || {};
   var query = Object.assign({}, route.query);
 
@@ -746,7 +746,7 @@ export async function render({
     devOnlyCacheBusterQueryString: "",
     scriptLoader,
     isPreview: isPreview,
-    autoExport: isAutoExport,
+    autoExport: nextExport === true ? true : undefined,
     nextExport: nextExport,
     useMaybeDeferContent,
   });
-- 
cgit v1.2.3

