Skip to content

Commit ad94d8a

Browse files
enhance(build): warn only once about missing BLOG_ROOT (#10269)
Co-authored-by: Florian Dieminger <[email protected]>
1 parent cea32ef commit ad94d8a

File tree

2 files changed

+39
-36
lines changed

2 files changed

+39
-36
lines changed

Diff for: build/utils.ts

-2
Original file line numberDiff line numberDiff line change
@@ -352,10 +352,8 @@ export function makeTOC(doc, withH3 = false) {
352352

353353
export function findPostFileBySlug(slug: string): string | null {
354354
if (!BLOG_ROOT) {
355-
console.warn("'BLOG_ROOT' not set in .env file");
356355
return null;
357356
}
358-
359357
try {
360358
const { stdout, stderr, status } = spawnSync(rgPath, [
361359
"-il",

Diff for: server/index.ts

+39-34
Original file line numberDiff line numberDiff line change
@@ -272,41 +272,46 @@ app.get("/:locale/blog/author/:slug/:asset", async (req, res) => {
272272
)
273273
).pipe(res);
274274
});
275-
app.get("/:locale/blog/:slug/index.json", async (req, res) => {
276-
const { slug } = req.params;
277-
const data = await findPostBySlug(slug);
278-
if (!data) {
275+
if (BLOG_ROOT) {
276+
app.get("/:locale/blog/:slug/index.json", async (req, res) => {
277+
const { slug } = req.params;
278+
const data = await findPostBySlug(slug);
279+
if (!data) {
280+
return res.status(404).send("Nothing here 🤷‍♂️");
281+
}
282+
return res.json(data);
283+
});
284+
app.get(
285+
["/:locale/blog/:slug/runner.html", "/:locale/blog/:slug/runner.html"],
286+
async (req, res) => {
287+
return res
288+
.setHeader("Content-Security-Policy", PLAYGROUND_UNSAFE_CSP_VALUE)
289+
.status(200)
290+
.sendFile(path.join(STATIC_ROOT, "runner.html"));
291+
}
292+
);
293+
app.get("/:locale/blog/:slug/_sample_.:id.html", async (req, res) => {
294+
const { slug, id } = req.params;
295+
try {
296+
return res.send(await findPostLiveSampleBySlug(slug, id));
297+
} catch (e) {
298+
return res.status(404).send(e.toString());
299+
}
300+
});
301+
app.get("/:locale/blog/:slug/:asset", async (req, res) => {
302+
const { slug, asset } = req.params;
303+
const p = findPostPathBySlug(slug);
304+
if (p) {
305+
return send(
306+
req,
307+
path.resolve(path.join(p, sanitizeFilename(asset)))
308+
).pipe(res);
309+
}
279310
return res.status(404).send("Nothing here 🤷‍♂️");
280-
}
281-
return res.json(data);
282-
});
283-
app.get(
284-
["/:locale/blog/:slug/runner.html", "/:locale/blog/:slug/runner.html"],
285-
async (req, res) => {
286-
return res
287-
.setHeader("Content-Security-Policy", PLAYGROUND_UNSAFE_CSP_VALUE)
288-
.status(200)
289-
.sendFile(path.join(STATIC_ROOT, "runner.html"));
290-
}
291-
);
292-
app.get("/:locale/blog/:slug/_sample_.:id.html", async (req, res) => {
293-
const { slug, id } = req.params;
294-
try {
295-
return res.send(await findPostLiveSampleBySlug(slug, id));
296-
} catch (e) {
297-
return res.status(404).send(e.toString());
298-
}
299-
});
300-
app.get("/:locale/blog/:slug/:asset", async (req, res) => {
301-
const { slug, asset } = req.params;
302-
const p = findPostPathBySlug(slug);
303-
if (p) {
304-
return send(req, path.resolve(path.join(p, sanitizeFilename(asset)))).pipe(
305-
res
306-
);
307-
}
308-
return res.status(404).send("Nothing here 🤷‍♂️");
309-
});
311+
});
312+
} else {
313+
console.warn("'BLOG_ROOT' not set in .env file");
314+
}
310315
app.get("/*", async (req, res, ...args) => {
311316
const parsedUrl = new URL(req.url, `http://localhost:${PORT}`);
312317
if (req.url.startsWith("/_")) {

0 commit comments

Comments
 (0)