diff --git a/CHANGES.md b/CHANGES.md index 674ed76ad..dce8915ed 100644 --- a/CHANGES.md +++ b/CHANGES.md @@ -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 -------------- diff --git a/packages/init/src/templates/next/instrumentation.ts.tpl b/packages/init/src/templates/next/instrumentation.ts.tpl new file mode 100644 index 000000000..6d782f34f --- /dev/null +++ b/packages/init/src/templates/next/instrumentation.ts.tpl @@ -0,0 +1,5 @@ +export async function register() { + if (process.env.NEXT_RUNTIME === "nodejs") { + await import("./logging"); + } +} diff --git a/packages/init/src/templates/nitro/server/plugins/logging.ts.tpl b/packages/init/src/templates/nitro/server/plugins/logging.ts.tpl new file mode 100644 index 000000000..32adf599a --- /dev/null +++ b/packages/init/src/templates/nitro/server/plugins/logging.ts.tpl @@ -0,0 +1,3 @@ +import "../logging"; + +export default function setupLogging() {} diff --git a/packages/init/src/webframeworks.test.ts b/packages/init/src/webframeworks.test.ts new file mode 100644 index 000000000..df25d74af --- /dev/null +++ b/packages/init/src/webframeworks.test.ts @@ -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")); + ok(instrumentation.includes('await import("./logging")')); +}); diff --git a/packages/init/src/webframeworks.ts b/packages/init/src/webframeworks.ts index f77cd4dfa..de4fa1561 100644 --- a/packages/init/src/webframeworks.ts +++ b/packages/init/src/webframeworks.ts @@ -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", ), @@ -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" ? {