Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 11 additions & 0 deletions CHANGES.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,17 @@ Version 2.0.15

To be released.

### @fedify/init

- Fixed the Nitro and Next.js project templates so their generated
*logging.ts* files are loaded during server startup. Nitro projects now
get a server plugin that imports the LogTape configuration, and Next.js
projects get an *instrumentation.ts* `register()` hook that imports it in
the Node.js runtime before Fedify handles requests. [[#725], [#727]]

[#725]: https://github.com/fedify-dev/fedify/issues/725
[#727]: https://github.com/fedify-dev/fedify/pull/727


Version 2.0.14
--------------
Expand Down
5 changes: 5 additions & 0 deletions packages/init/src/templates/next/instrumentation.ts.tpl
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
export async function register() {
if (process.env.NEXT_RUNTIME === "nodejs") {
Comment thread
dahlia marked this conversation as resolved.
await import("./logging");
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
import "../logging";

export default function setupLogging() {}
45 changes: 45 additions & 0 deletions packages/init/src/webframeworks.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
import { ok } from "node:assert/strict";
import test from "node:test";
import webFrameworks from "./webframeworks.ts";

test("Nitro template loads LogTape during server startup", async () => {
const { files } = await webFrameworks.nitro.init({
projectName: "test-app",
dir: ".",
command: "init",
packageManager: "npm",
kvStore: "in-memory",
messageQueue: "in-process",
webFramework: "nitro",
testMode: false,
dryRun: true,
});

ok(files);
ok("server/plugins/logging.ts" in files);
const plugin = files["server/plugins/logging.ts"];
ok(plugin);
ok(plugin.includes('import "../logging";'));
});

test("Next.js template loads LogTape through instrumentation", async () => {
const { files } = await webFrameworks.next.init({
projectName: "test-app",
dir: ".",
command: "init",
packageManager: "npm",
kvStore: "in-memory",
messageQueue: "in-process",
webFramework: "next",
testMode: false,
dryRun: true,
});

ok(files);
ok("instrumentation.ts" in files);
const instrumentation = files["instrumentation.ts"];
ok(instrumentation);
ok(instrumentation.includes("export async function register()"));
ok(instrumentation.includes("process.env.NEXT_RUNTIME"));
Comment thread
dahlia marked this conversation as resolved.
ok(instrumentation.includes('await import("./logging")'));
});
4 changes: 4 additions & 0 deletions packages/init/src/webframeworks.ts
Original file line number Diff line number Diff line change
Expand Up @@ -300,6 +300,9 @@ const webFrameworks: WebFrameworks = {
federationFile: "server/federation.ts",
loggingFile: "server/logging.ts",
files: {
"server/plugins/logging.ts": await readTemplate(
"nitro/server/plugins/logging.ts",
),
"server/middleware/federation.ts": await readTemplate(
"nitro/server/middleware/federation.ts",
),
Expand Down Expand Up @@ -338,6 +341,7 @@ const webFrameworks: WebFrameworks = {
federationFile: "federation/index.ts",
loggingFile: "logging.ts",
files: {
"instrumentation.ts": await readTemplate("next/instrumentation.ts"),
"middleware.ts": await readTemplate("next/middleware.ts"),
...(pm !== "deno"
? {
Expand Down
Loading