aboutsummaryrefslogtreecommitdiff
path: root/examples/hello-next/bun-framework-next/server.development.tsx
blob: 82caf0ab7b8769b8a335d43a14e35074017194d1 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
import React from "react";
class URL {
  constructor(base, source) {
    this.pathname = source;
    this.href = base + source;
  }
}
var onlyChildPolyfill = React.Children.only;
React.Children.only = function (children) {
  if (children && typeof children === "object" && children.length == 1) {
    return onlyChildPolyfill(children[0]);
  }

  return onlyChildPolyfill(children);
};
globalThis.URL = URL;
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),
    },
  };
}