diff options
-rw-r--r-- | examples/next/bunfig.toml | 1 | ||||
-rw-r--r-- | examples/next/package.json | 6 | ||||
-rw-r--r-- | packages/bun-framework-next/package.json | 11 | ||||
-rw-r--r-- | packages/bun-framework-next/renderDocument.tsx | 47 | ||||
-rw-r--r-- | packages/bun-framework-next/server-polyfills.tsx | 2 | ||||
-rw-r--r-- | packages/bun-framework-next/server.development.tsx | 4 |
6 files changed, 46 insertions, 25 deletions
diff --git a/examples/next/bunfig.toml b/examples/next/bunfig.toml new file mode 100644 index 000000000..f191e4e10 --- /dev/null +++ b/examples/next/bunfig.toml @@ -0,0 +1 @@ +framework = "next" diff --git a/examples/next/package.json b/examples/next/package.json index 6988c5283..9def6d7cc 100644 --- a/examples/next/package.json +++ b/examples/next/package.json @@ -3,15 +3,15 @@ "version": "0.0.48", "main": "index.js", "dependencies": { - "next": "12.0.2", + "next": "^12.1.0", "react": "beta", "react-dom": "beta", "react-is": "^17.0.2" }, "devDependencies": { - "@types/react": "^17.0.19", + "@types/react": "latest", "bun-framework-next": "^12", - "typescript": "^4.4.0" + "typescript": "latest" }, "bun-create": { "postinstall": [ diff --git a/packages/bun-framework-next/package.json b/packages/bun-framework-next/package.json index 389a8c4fc..19b369e53 100644 --- a/packages/bun-framework-next/package.json +++ b/packages/bun-framework-next/package.json @@ -1,11 +1,10 @@ { "name": "bun-framework-next", - "version": "12.0.7", + "version": "12.0.8", "main": "empty.js", "module": "empty.js", - "description": "bun compatibility layer for Next.js v12.0.x", + "description": "bun compatibility layer for Next.js v12.x.x", "scripts": { - "test": "echo Worked", "check": "tsc --noEmit" }, "author": "", @@ -14,14 +13,14 @@ "react-is": "^17.0.2" }, "peerDependencies": { - "next": "^12.0.0" + "next": "^12" }, "devDependencies": { "@types/react": "^17.0.34", "@types/react-dom": "^17.0.11", - "next": "^12.0.0", + "next": "^12.1.0", + "react": "^17.0.2", "react-dom": "^17.0.2", - "react-refresh": "^0.10.0", "typescript": "^4.4.4" }, "framework": { diff --git a/packages/bun-framework-next/renderDocument.tsx b/packages/bun-framework-next/renderDocument.tsx index 358c52f7a..89eb987b5 100644 --- a/packages/bun-framework-next/renderDocument.tsx +++ b/packages/bun-framework-next/renderDocument.tsx @@ -23,9 +23,37 @@ 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 nextPackage from "next/package.json"; -// This constant was removed from Next, Is it doing anything? -const BODY_RENDER_TARGET = "__NEXT_BODY_RENDER_TARGET__"; +function appendNextBody(html: string, docPropsHtml) { + if (nextPackage.version.startsWith("12.0")) { + const NEXT_12_0_BODY_RENDER_TARGET = "__NEXT_BODY_RENDER_TARGET__"; + + const bodyRenderIdx = html.indexOf(NEXT_12_0_BODY_RENDER_TARGET); + + return ( + html.substring(0, bodyRenderIdx) + + docPropsHtml + + html.substring(bodyRenderIdx + NEXT_12_0_BODY_RENDER_TARGET.length) + ); + } else { + const end = html.lastIndexOf("</next-js-internal-body-render-target>"); + + const start = html.lastIndexOf( + "<next-js-internal-body-render-target>", + end + ); + + if (start === -1 || end === -1) { + throw new Error( + "Can't find where your <App /> starts or where the <Document /> ends. \nThis is probably a version incompatibility. Please mention this error in Bun's discord\n\n" + + html + ); + } + + return html.substring(0, start) + docPropsHtml + html.substring(end); + } +} const dev = process.env.NODE_ENV === "development"; @@ -53,8 +81,9 @@ function getScripts(files: DocumentFiles) { devOnlyCacheBusterQueryString, } = context; - const normalScripts = files.allFiles.filter(isJSFile); - const lowPriorityScripts = buildManifest.lowPriorityFiles?.filter(isJSFile); + const normalScripts = files?.allFiles?.filter(isJSFile) ?? []; + const lowPriorityScripts = + buildManifest?.lowPriorityFiles?.filter(isJSFile) ?? []; return [...normalScripts, ...lowPriorityScripts].map((file) => { return ( @@ -543,9 +572,6 @@ export async function render({ }); const pageProps = Object.assign({}, props.pageProps || {}); - // This isn't correct. - // 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; @@ -749,12 +775,7 @@ export async function render({ useMaybeDeferContent, }); // __NEXT_BODY_RENDER_TARGET__ - const bodyRenderIdx = html.indexOf(BODY_RENDER_TARGET); - html = - html.substring(0, bodyRenderIdx) + - (false ? "<!-- __NEXT_DATA__ -->" : "") + - docProps.html + - html.substring(bodyRenderIdx + BODY_RENDER_TARGET.length); + html = appendNextBody(html, docProps.html); if (responseHeaders) { return new Response( diff --git a/packages/bun-framework-next/server-polyfills.tsx b/packages/bun-framework-next/server-polyfills.tsx index 59c38e92c..94a751567 100644 --- a/packages/bun-framework-next/server-polyfills.tsx +++ b/packages/bun-framework-next/server-polyfills.tsx @@ -4,7 +4,5 @@ import { Buffer } from "buffer"; import { URL } from "./url-polyfill"; import * as React from "react"; -const onlyChildPolyfill = React.Children.only; - globalThis.Buffer ||= Buffer; globalThis.URL = URL; diff --git a/packages/bun-framework-next/server.development.tsx b/packages/bun-framework-next/server.development.tsx index 3689dc96e..41d616ba8 100644 --- a/packages/bun-framework-next/server.development.tsx +++ b/packages/bun-framework-next/server.development.tsx @@ -4,7 +4,9 @@ import { render } from "./renderDocument"; const { version } = nextPackage; if ( - (!version.startsWith("11.1") && !version.startsWith("12.0")) || + (!version.startsWith("11.1") && + !version.startsWith("12.0") && + !version.startsWith("12.1")) || version === "11.1.0" || version === "11.1.1" ) { |