Skip to content

Commit 7ce50b2

Browse files
authored
Merge pull request #482 from Stonebubble/fix/top-level-await-keyword-in-server-js
1 parent 62a79d8 commit 7ce50b2

File tree

1 file changed

+94
-91
lines changed

1 file changed

+94
-91
lines changed

frontend/server.js

Lines changed: 94 additions & 91 deletions
Original file line numberDiff line numberDiff line change
@@ -13,117 +13,120 @@ import * as nodeUrl from "node:url";
1313

1414
installGlobals();
1515

16-
const base = process.env.BASE || "/";
17-
const port = process.argv.includes("--port")
18-
? process.argv[process.argv.indexOf("--port") + 1]
19-
: process.env.NODE_PORT || 5678;
20-
const isProduction = process.env.NODE_ENV === "production";
16+
async function main() {
17+
const base = process.env.BASE || "/";
18+
const port = process.argv.includes("--port")
19+
? process.argv[process.argv.indexOf("--port") + 1]
20+
: process.env.NODE_PORT || 5678;
21+
const isProduction = process.env.NODE_ENV === "production";
2122

22-
const __dirname = path.dirname(fileURLToPath(import.meta.url));
23+
const __dirname = path.dirname(fileURLToPath(import.meta.url));
2324

24-
const templateHtml = isProduction
25-
? await fs.readFile("./dist/client/index.html", "utf-8")
26-
: "";
25+
const templateHtml = isProduction
26+
? await fs.readFile("./dist/client/index.html", "utf-8")
27+
: "";
2728

28-
const ssrManifest = isProduction
29-
? await fs.readFile("./dist/client/.vite/ssr-manifest.json", "utf-8")
30-
: undefined;
29+
const ssrManifest = isProduction
30+
? await fs.readFile("./dist/client/.vite/ssr-manifest.json", "utf-8")
31+
: undefined;
3132

32-
const app = express();
33-
app.use(cookieParser());
33+
const app = express();
34+
app.use(cookieParser());
3435

35-
app.use('/.well-known', express.static(path.join(__dirname, 'public/.well-known')));
36+
app.use('/.well-known', express.static(path.join(__dirname, 'public/.well-known')));
3637

37-
let vite;
38+
let vite;
3839

39-
if (!isProduction) {
40-
vite = await viteServer({
41-
server: {middlewareMode: true},
42-
appType: "custom",
43-
base,
44-
});
45-
46-
app.use(vite.middlewares);
47-
} else {
48-
app.use(compression());
49-
app.use(base, sirv(path.join(__dirname, "./dist/client"), {extensions: []}));
50-
}
40+
if (!isProduction) {
41+
vite = await viteServer({
42+
server: { middlewareMode: true },
43+
appType: "custom",
44+
base,
45+
});
5146

52-
const getViteEnvironmentVariables = () => {
53-
const envVars = {};
54-
for (const key in process.env) {
55-
if (key.startsWith('VITE_')) {
56-
envVars[key] = process.env[key];
57-
}
47+
app.use(vite.middlewares);
48+
} else {
49+
app.use(compression());
50+
app.use(base, sirv(path.join(__dirname, "./dist/client"), { extensions: [] }));
5851
}
59-
return JSON.stringify(envVars);
60-
};
61-
62-
app.use("*", async (req, res) => {
63-
const url = req.originalUrl.replace(base, "");
64-
65-
try {
66-
let template;
67-
let render;
68-
69-
if (!isProduction) {
70-
template = await fs.readFile(path.join(__dirname, "./index.html"), "utf-8");
71-
template = await vite.transformIndexHtml(url, template);
72-
render = (await vite.ssrLoadModule("/src/entry.server.tsx")).render;
73-
} else {
74-
template = templateHtml;
75-
render = (await dynamicImport(path.join(__dirname, "./dist/server/entry.server.js"))).render;
52+
53+
const getViteEnvironmentVariables = () => {
54+
const envVars = {};
55+
for (const key in process.env) {
56+
if (key.startsWith('VITE_')) {
57+
envVars[key] = process.env[key];
58+
}
7659
}
60+
return JSON.stringify(envVars);
61+
};
7762

78-
const {appHtml, dehydratedState, helmetContext} = await render(
79-
{req, res},
80-
ssrManifest
81-
);
82-
const stringifiedState = JSON.stringify(dehydratedState);
63+
app.use("*", async (req, res) => {
64+
const url = req.originalUrl.replace(base, "");
65+
66+
try {
67+
let template;
68+
let render;
69+
70+
if (!isProduction) {
71+
template = await fs.readFile(path.join(__dirname, "./index.html"), "utf-8");
72+
template = await vite.transformIndexHtml(url, template);
73+
render = (await vite.ssrLoadModule("/src/entry.server.tsx")).render;
74+
} else {
75+
template = templateHtml;
76+
render = (await dynamicImport(path.join(__dirname, "./dist/server/entry.server.js"))).render;
77+
}
8378

84-
const helmetHtml = Object.values(helmetContext.helmet || {})
85-
.map((value) => value.toString() || "")
86-
.join(" ");
79+
const { appHtml, dehydratedState, helmetContext } = await render(
80+
{ req, res },
81+
ssrManifest
82+
);
83+
const stringifiedState = JSON.stringify(dehydratedState);
8784

88-
const envVariablesHtml = `<script>window.hievents = ${getViteEnvironmentVariables()};</script>`;
85+
const helmetHtml = Object.values(helmetContext.helmet || {})
86+
.map((value) => value.toString() || "")
87+
.join(" ");
8988

90-
const headSnippets = [];
91-
if (process.env.VITE_FATHOM_SITE_ID) {
92-
headSnippets.push(`
89+
const envVariablesHtml = `<script>window.hievents = ${getViteEnvironmentVariables()};</script>`;
90+
91+
const headSnippets = [];
92+
if (process.env.VITE_FATHOM_SITE_ID) {
93+
headSnippets.push(`
9394
<script src="https://cdn.usefathom.com/script.js" data-spa="auto" data-site="${process.env.VITE_FATHOM_SITE_ID}" defer></script>
9495
`);
95-
}
96+
}
9697

97-
const html = template
98-
.replace("<!--head-snippets-->", headSnippets.join("\n"))
99-
.replace("<!--app-html-->", appHtml)
100-
.replace("<!--dehydrated-state-->", `<script>window.__REHYDRATED_STATE__ = ${stringifiedState}</script>`)
101-
.replace("<!--environment-variables-->", envVariablesHtml)
102-
.replace(/<!--render-helmet-->.*?<!--\/render-helmet-->/s, helmetHtml);
103-
104-
res.setHeader("Content-Type", "text/html");
105-
return res.status(200).end(html);
106-
} catch (error) {
107-
if (error instanceof Response) {
108-
if (error.status >= 300 && error.status < 400) {
109-
return res.redirect(error.status, error.headers.get("Location") || "/");
110-
} else {
111-
return res.status(error.status).send(await error.text());
98+
const html = template
99+
.replace("<!--head-snippets-->", headSnippets.join("\n"))
100+
.replace("<!--app-html-->", appHtml)
101+
.replace("<!--dehydrated-state-->", `<script>window.__REHYDRATED_STATE__ = ${stringifiedState}</script>`)
102+
.replace("<!--environment-variables-->", envVariablesHtml)
103+
.replace(/<!--render-helmet-->.*?<!--\/render-helmet-->/s, helmetHtml);
104+
105+
res.setHeader("Content-Type", "text/html");
106+
return res.status(200).end(html);
107+
} catch (error) {
108+
if (error instanceof Response) {
109+
if (error.status >= 300 && error.status < 400) {
110+
return res.redirect(error.status, error.headers.get("Location") || "/");
111+
} else {
112+
return res.status(error.status).send(await error.text());
113+
}
112114
}
113-
}
114115

115-
console.error(error);
116-
res.status(500).send("Internal Server Error");
117-
}
118-
});
116+
console.error(error);
117+
res.status(500).send("Internal Server Error");
118+
}
119+
});
119120

120-
app.listen(port, () => {
121-
console.info(`SSR Serving at http://localhost:${port}`);
122-
});
121+
app.listen(port, () => {
122+
console.info(`SSR Serving at http://localhost:${port}`);
123+
});
123124

124-
const dynamicImport = async (path) => {
125-
return import(
126-
nodePath.isAbsolute(path) ? nodeUrl.pathToFileURL(path).toString() : path
127-
);
125+
const dynamicImport = async (path) => {
126+
return import(
127+
nodePath.isAbsolute(path) ? nodeUrl.pathToFileURL(path).toString() : path
128+
);
128129

130+
}
129131
}
132+
main();

0 commit comments

Comments
 (0)