Skip to content

Commit 1cb9a6b

Browse files
authored
Merge pull request #727 from dahlia/bugfix/nitro-next-logging
Load logging in init templates
2 parents 855f4e5 + ec1edc7 commit 1cb9a6b

5 files changed

Lines changed: 68 additions & 0 deletions

File tree

CHANGES.md

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,17 @@ Version 2.0.15
88

99
To be released.
1010

11+
### @fedify/init
12+
13+
- Fixed the Nitro and Next.js project templates so their generated
14+
*logging.ts* files are loaded during server startup. Nitro projects now
15+
get a server plugin that imports the LogTape configuration, and Next.js
16+
projects get an *instrumentation.ts* `register()` hook that imports it in
17+
the Node.js runtime before Fedify handles requests. [[#725], [#727]]
18+
19+
[#725]: https://github.com/fedify-dev/fedify/issues/725
20+
[#727]: https://github.com/fedify-dev/fedify/pull/727
21+
1122

1223
Version 2.0.14
1324
--------------
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
export async function register() {
2+
if (process.env.NEXT_RUNTIME === "nodejs") {
3+
await import("./logging");
4+
}
5+
}
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
import "../logging";
2+
3+
export default function setupLogging() {}
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
import { ok } from "node:assert/strict";
2+
import test from "node:test";
3+
import webFrameworks from "./webframeworks.ts";
4+
5+
test("Nitro template loads LogTape during server startup", async () => {
6+
const { files } = await webFrameworks.nitro.init({
7+
projectName: "test-app",
8+
dir: ".",
9+
command: "init",
10+
packageManager: "npm",
11+
kvStore: "in-memory",
12+
messageQueue: "in-process",
13+
webFramework: "nitro",
14+
testMode: false,
15+
dryRun: true,
16+
});
17+
18+
ok(files);
19+
ok("server/plugins/logging.ts" in files);
20+
const plugin = files["server/plugins/logging.ts"];
21+
ok(plugin);
22+
ok(plugin.includes('import "../logging";'));
23+
});
24+
25+
test("Next.js template loads LogTape through instrumentation", async () => {
26+
const { files } = await webFrameworks.next.init({
27+
projectName: "test-app",
28+
dir: ".",
29+
command: "init",
30+
packageManager: "npm",
31+
kvStore: "in-memory",
32+
messageQueue: "in-process",
33+
webFramework: "next",
34+
testMode: false,
35+
dryRun: true,
36+
});
37+
38+
ok(files);
39+
ok("instrumentation.ts" in files);
40+
const instrumentation = files["instrumentation.ts"];
41+
ok(instrumentation);
42+
ok(instrumentation.includes("export async function register()"));
43+
ok(instrumentation.includes("process.env.NEXT_RUNTIME"));
44+
ok(instrumentation.includes('await import("./logging")'));
45+
});

packages/init/src/webframeworks.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -300,6 +300,9 @@ const webFrameworks: WebFrameworks = {
300300
federationFile: "server/federation.ts",
301301
loggingFile: "server/logging.ts",
302302
files: {
303+
"server/plugins/logging.ts": await readTemplate(
304+
"nitro/server/plugins/logging.ts",
305+
),
303306
"server/middleware/federation.ts": await readTemplate(
304307
"nitro/server/middleware/federation.ts",
305308
),
@@ -338,6 +341,7 @@ const webFrameworks: WebFrameworks = {
338341
federationFile: "federation/index.ts",
339342
loggingFile: "logging.ts",
340343
files: {
344+
"instrumentation.ts": await readTemplate("next/instrumentation.ts"),
341345
"middleware.ts": await readTemplate("next/middleware.ts"),
342346
...(pm !== "deno"
343347
? {

0 commit comments

Comments
 (0)