diff options
author | 2021-08-17 13:31:33 -0700 | |
---|---|---|
committer | 2021-08-17 13:31:33 -0700 | |
commit | ae01e9b98d481332236976344fa0386bafa82f53 (patch) | |
tree | 6049d32360c93074a3848726687d1d6e991f523b /demos/hello-next/bun-framework-next/server.development.tsx | |
parent | afcbcd231c9328bfd9b6ae8c81ea296d4235ae81 (diff) | |
download | bun-ae01e9b98d481332236976344fa0386bafa82f53.tar.gz bun-ae01e9b98d481332236976344fa0386bafa82f53.tar.zst bun-ae01e9b98d481332236976344fa0386bafa82f53.zip |
Move bun-framework-next folder, add tsconfig.json
Former-commit-id: cfd7b425f071f200504493fef206afb7c68de6d9
Diffstat (limited to 'demos/hello-next/bun-framework-next/server.development.tsx')
-rw-r--r-- | demos/hello-next/bun-framework-next/server.development.tsx | 68 |
1 files changed, 68 insertions, 0 deletions
diff --git a/demos/hello-next/bun-framework-next/server.development.tsx b/demos/hello-next/bun-framework-next/server.development.tsx new file mode 100644 index 000000000..54181c271 --- /dev/null +++ b/demos/hello-next/bun-framework-next/server.development.tsx @@ -0,0 +1,68 @@ +globalThis.global = globalThis; +import { render } from "./renderDocument"; + +let buildId = 0; + +var DocumentNamespacePromise; + +DocumentNamespacePromise = import(Bun.routesDir + "_document"); +var DocumentLoaded = false; +var DocumentNamespace; + +addEventListener("fetch", async (event: FetchEvent) => { + if (!DocumentLoaded) { + DocumentLoaded = true; + try { + DocumentNamespace = await DocumentNamespacePromise; + } catch (exception) { + DocumentNamespace = null; + } + } + + var appRoute; + + try { + appRoute = await import(Bun.routesDir + "_app"); + } catch (exception) { + appRoute = null; + } + const appStylesheets = (Bun.getImportedStyles() as string[]).slice(); + var route = Bun.match(event); + + // This imports the currently matched route. + const PageNamespace = await import(route.filePath); + + // This returns all .css files that were imported in the line above. + // It's recursive, so any file that imports a CSS file will be included. + const pageStylesheets = (Bun.getImportedStyles() as string[]).slice(); + + event.respondWith( + render({ + route, + PageNamespace, + appStylesheets, + pageStylesheets, + DocumentNamespace, + AppNamespace: appRoute, + buildId, + routePaths: Bun.getRouteFiles(), + }) + ); + buildId++; +}); + +// typescript isolated modules +export {}; + +declare var Bun: any; + +function getNextData(request: Request, route) { + return { + NEXT_DATA: { + query: route.query, + props: {}, + page: route.path, + buildId: buildId.toString(16), + }, + }; +} |