Skip to content

fix(build,azure-swa): prefer index.mjs serverEntry; fix Azure SWA request URL and body#4352

Open
tonoizer wants to merge 4 commits into
nitrojs:mainfrom
tonoizer:fix/mf-server-entry-azure-swa
Open

fix(build,azure-swa): prefer index.mjs serverEntry; fix Azure SWA request URL and body#4352
tonoizer wants to merge 4 commits into
nitrojs:mainfrom
tonoizer:fix/mf-server-entry-azure-swa

Conversation

@tonoizer

@tonoizer tonoizer commented Jun 15, 2026

Copy link
Copy Markdown

Problem

Three bugs that compound when deploying a Nitro fullstack app with Vite Module Federation exposes (and when using the Azure Static Web Apps preset).

Reproduction demo

Demo repo: https://github.com/tonoizer/demo-nitro-vite-module-federation-serverEntry-bug

Minimal repro for the serverEntry / vite preview issue:

pnpm dlx create-nitro-app my-app
cd my-app
pnpm add -D @module-federation/vite
# add federation plugin + expose (see demo repo vite.config.ts / module-federation.config.ts)
pnpm build
pnpm check          # fails: serverEntry is server/_chunks/app.mjs
pnpm preview        # broken: 404 on / and /api/*
pnpm preview:workaround   # works: node .output/server/index.mjs

pnpm dev works — only production preview is broken because it relies on .output/nitro.json.


1. Wrong serverEntry in nitro.json

writeBuildInfo used .find() to pick the first isEntry chunk from Rollup output. Vite Module Federation marks expose chunks as entry points too, so when they appear before server/index.mjs in the output array, nitro.json points at e.g. server/_chunks/app.mjs instead of the server bundle. vite preview then loads a file with no fetch handler → 404 everywhere.

2. Azure SWA: relative URL passed to new Request()

azure-swa.ts constructed a relative path (/api/... or pathname + search) and passed it directly to new Request(url, ...). Node.js requires an absolute URL — this throws in production and breaks all /api/* routes.

3. Azure SWA: response body handling

context.res.body was set incorrectly for Azure Functions v3 (which expects string | Buffer). Text responses could be empty/wrong; binary responses were corrupted when unconditionally calling .text().

Fix

  • resolveNitroServerEntryFileName — when multiple isEntry chunks exist, prefer the one matching /(^|\/)index\.mjs$/ (Nitro's server entry convention) instead of taking array-first.
  • resolveAzureSwaRequestUrl — use x-ms-original-url as-is (it's already an absolute URL) or construct new URL(path, "http://nitro.local").href for direct /api/* calls.
  • azureResponseBody — inspect content-type and return UTF-8 string for text types or Buffer for binary (same pattern as AWS Lambda preset).
  • Unit tests for extracted helpers.

Test plan

  • pnpm vitest run test/unit/build-info.test.ts test/unit/azure-swa.test.ts
  • Demo repo: pnpm build && pnpm check passes; pnpm preview serves SPA + /api/hello
  • Azure SWA preset: /api/* routes return real JSON/HTML; binary routes return correct bytes

…RL and body

- extract resolveNitroServerEntryFileName: when multiple isEntry chunks exist
  (e.g. Module Federation expose chunks), prefer the one matching index.mjs
  instead of taking the first chunk arbitrarily
- resolveAzureSwaRequestUrl: use x-ms-original-url as-is (full absolute URL)
  or construct an absolute URL via new URL(..., "http://nitro.local") —
  new Request() requires an absolute URL in Node.js
- materialise response.body as text before assigning to context.res.body;
  Azure Functions v3 cannot serialise a ReadableStream
- add unit tests for both helpers
@tonoizer
tonoizer requested a review from pi0 as a code owner June 15, 2026 18:47
@vercel

vercel Bot commented Jun 15, 2026

Copy link
Copy Markdown

@tonoizer is attempting to deploy a commit to the Nitro Team on Vercel.

A member of the Team first needs to authorize it.

@coderabbitai

coderabbitai Bot commented Jun 15, 2026

Copy link
Copy Markdown
Contributor

Review Change Stack

📝 Walkthrough

Walkthrough

Updates build metadata to select the configured server entry chunk, with fallbacks for index.mjs and the first entry. Azure SWA now resolves absolute request URLs and serializes response bodies as text or binary data, with unit tests covering both changes.

Changes

Server Entry Filename Resolution

Layer / File(s) Summary
Entry selection and build-info validation
src/build/info.ts, test/unit/build-info.test.ts
writeBuildInfo selects an entry chunk matching Nitro’s configured entry, then falls back to index.mjs or the first entry. Tests cover plugin ordering and a custom worker.mjs entry.

Azure SWA URL and Body Handling

Layer / File(s) Summary
Request URL resolution and handler integration
src/presets/azure/runtime/azure-swa.ts, test/unit/azure-swa.test.ts
resolveAzureSwaRequestUrl prefers x-ms-original-url or reconstructs an absolute /api/ URL. handle() uses the resolved URL and assigns the converted response body and status.
Response body conversion and validation
src/presets/azure/runtime/_utils.ts, test/unit/azure-swa.test.ts
azureResponseBody returns undefined for empty responses, strings for text-like content types, and Buffer values for binary responses.

Estimated code review effort: 3 (Moderate) | ~20 minutes

Possibly related PRs

  • nitrojs/nitro#3999: Overlaps with the writeBuildInfo server-entry derivation logic in src/build/info.ts.
🚥 Pre-merge checks | ✅ 4 | ❌ 1

❌ Failed checks (1 warning)

Check name Status Explanation Resolution
Docstring Coverage ⚠️ Warning Docstring coverage is 22.22% which is insufficient. The required threshold is 80.00%. Write docstrings for the functions missing them to satisfy the coverage threshold.
✅ Passed checks (4 passed)
Check name Status Explanation
Title check ✅ Passed The title follows conventional commits and accurately summarizes the build-info and Azure SWA fixes.
Description check ✅ Passed The description directly explains the same Nitro build-entry and Azure SWA request/body fixes covered by the changeset.
Linked Issues check ✅ Passed Check skipped because no linked issues were found for this pull request.
Out of Scope Changes check ✅ Passed Check skipped because no linked issues were found for this pull request.
✨ Finishing Touches
🧪 Generate unit tests (beta)
  • Create PR with unit tests

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

Comment @coderabbitai help to get the list of available commands.

@coderabbitai coderabbitai Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actionable comments posted: 1

🤖 Prompt for all review comments with AI agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

Inline comments:
In `@src/presets/azure/runtime/azure-swa.ts`:
- Around line 27-28: The response body conversion in azure-swa.ts is using
`.text()` which corrupts binary responses by assuming UTF-8 encoding. Instead of
unconditionally calling `.text()` on the Response object, inspect the
content-type header from the response to determine the correct handling: for
binary content types (images, PDFs, etc.), use `arrayBuffer()` and convert the
result to a Buffer that Azure Functions v3 expects, and for text content types
use `.text()`. Follow the same pattern already implemented in the AWS Lambda
preset to maintain consistency across presets.
🪄 Autofix (Beta)

Fix all unresolved CodeRabbit comments on this PR:

  • Push a commit to this branch (recommended)
  • Create a new PR with the fixes

ℹ️ Review info
⚙️ Run configuration

Configuration used: Organization UI

Review profile: CHILL

Plan: Pro

Run ID: 34ecfe6a-7c6d-44a2-9946-b082cf2dc6e0

📥 Commits

Reviewing files that changed from the base of the PR and between 7765bcb and a115713.

📒 Files selected for processing (4)
  • src/build/info.ts
  • src/presets/azure/runtime/azure-swa.ts
  • test/unit/azure-swa.test.ts
  • test/unit/build-info.test.ts

Comment thread src/presets/azure/runtime/azure-swa.ts Outdated

@coderabbitai coderabbitai Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actionable comments posted: 1

🤖 Prompt for all review comments with AI agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

Inline comments:
In `@src/presets/azure/runtime/_utils.ts`:
- Around line 68-70: The `isTextType` function's regex pattern only matches
media types with `/json`, `/xml`, and `/javascript` but misses the `+json` and
`+xml` suffixes commonly used in structured media types like
`application/problem+json`. Update the regex pattern in the `isTextType`
function on Line 69 to include both the slash variants `/(javascript|json|xml)`
and the plus variants `\+(json|xml)` so that types with either separator are
correctly identified as text and not returned as binary Buffer.
🪄 Autofix (Beta)

Fix all unresolved CodeRabbit comments on this PR:

  • Push a commit to this branch (recommended)
  • Create a new PR with the fixes

ℹ️ Review info
⚙️ Run configuration

Configuration used: Organization UI

Review profile: CHILL

Plan: Pro

Run ID: d7dbf344-fb20-4eec-8ce2-b8ce200b0e1a

📥 Commits

Reviewing files that changed from the base of the PR and between a115713 and 3352e10.

📒 Files selected for processing (3)
  • src/presets/azure/runtime/_utils.ts
  • src/presets/azure/runtime/azure-swa.ts
  • test/unit/azure-swa.test.ts
🚧 Files skipped from review as they are similar to previous changes (1)
  • src/presets/azure/runtime/azure-swa.ts

Comment thread src/presets/azure/runtime/_utils.ts
…on content types and add corresponding unit test

@coderabbitai coderabbitai Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🧹 Nitpick comments (2)
src/build/info.ts (1)

106-128: 📐 Maintainability & Code Quality | 🔵 Trivial | 💤 Low value

Use an options object for the second parameter in multi-argument functions.

As per coding guidelines, multi-argument functions should use an options object as the second parameter. The internal helpers and their call site should be updated to adhere to this rule.

  • src/build/info.ts#L106-L128: Update resolveNitroServerEntryFileName and isNitroEntry signatures to accept an options object (e.g., { nitroEntry: string }) instead of a raw string.
  • src/build/info.ts#L51-L51: Update this call site to pass nitro.options.entry within an options object.
♻️ Proposed fix
--- src/build/info.ts
+++ src/build/info.ts
@@ -50,2 +50,2 @@
-  const serverEntryName = resolveNitroServerEntryFileName(output, nitro.options.entry);
+  const serverEntryName = resolveNitroServerEntryFileName(output, { nitroEntry: nitro.options.entry });
 
@@ -106,4 +106,4 @@
 function resolveNitroServerEntryFileName(
   output: RolldownOutput | RollupOutput | undefined,
-  nitroEntry: string
+  options: { nitroEntry: string }
 ): string | undefined {
@@ -113,3 +113,3 @@
       (entry) =>
-        entry.type === "chunk" && entry.isEntry && isNitroEntry(entry.facadeModuleId, nitroEntry)
+        entry.type === "chunk" && entry.isEntry && isNitroEntry(entry.facadeModuleId, { nitroEntry: options.nitroEntry })
     ) ??
@@ -122,5 +122,5 @@
 
-function isNitroEntry(facadeModuleId: string | null, nitroEntry: string): boolean {
+function isNitroEntry(facadeModuleId: string | null, options: { nitroEntry: string }): boolean {
   if (!facadeModuleId) return false;
-  if (facadeModuleId === nitroEntry) return true;
+  if (facadeModuleId === options.nitroEntry) return true;
   const extension = extname(facadeModuleId);
-  return !!extension && facadeModuleId.slice(0, -extension.length) === nitroEntry;
+  return !!extension && facadeModuleId.slice(0, -extension.length) === options.nitroEntry;
 }
🤖 Prompt for AI Agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

In `@src/build/info.ts` around lines 106 - 128, Update src/build/info.ts lines
106-128 so resolveNitroServerEntryFileName and isNitroEntry accept an options
object containing nitroEntry, and update src/build/info.ts line 51 to pass
nitro.options.entry through that object; preserve the existing entry-resolution
behavior.

Source: Coding guidelines

test/unit/build-info.test.ts (1)

58-91: 📐 Maintainability & Code Quality | 🔵 Trivial | 💤 Low value

Consider adding tests for fallback entry selection logic.

The current tests verify the primary logic where the chunk's facadeModuleId matches the Nitro entry. To ensure comprehensive coverage, consider adding test cases for the fallback behaviors in resolveNitroServerEntryFileName:

  1. When no facadeModuleId matches, but a chunk has fileName: "index.mjs".
  2. When neither a facadeModuleId match nor an "index.mjs" chunk exists, falling back to the first available isEntry chunk.
🤖 Prompt for AI Agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

In `@test/unit/build-info.test.ts` around lines 58 - 91, Add unit tests covering
the fallback branches of resolveNitroServerEntryFileName: first verify that an
isEntry chunk named index.mjs is selected when no facadeModuleId matches, then
verify that the first available isEntry chunk is selected when neither a
matching facadeModuleId nor index.mjs exists. Assert the resulting
buildInfo.serverEntry values.
🤖 Prompt for all review comments with AI agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

Nitpick comments:
In `@src/build/info.ts`:
- Around line 106-128: Update src/build/info.ts lines 106-128 so
resolveNitroServerEntryFileName and isNitroEntry accept an options object
containing nitroEntry, and update src/build/info.ts line 51 to pass
nitro.options.entry through that object; preserve the existing entry-resolution
behavior.

In `@test/unit/build-info.test.ts`:
- Around line 58-91: Add unit tests covering the fallback branches of
resolveNitroServerEntryFileName: first verify that an isEntry chunk named
index.mjs is selected when no facadeModuleId matches, then verify that the first
available isEntry chunk is selected when neither a matching facadeModuleId nor
index.mjs exists. Assert the resulting buildInfo.serverEntry values.

ℹ️ Review info
⚙️ Run configuration

Configuration used: Organization UI

Review profile: CHILL

Plan: Pro

Run ID: 75965414-eddf-46fb-a9b2-47c334c2ece6

📥 Commits

Reviewing files that changed from the base of the PR and between 48a014c and c62d262.

📒 Files selected for processing (3)
  • src/build/info.ts
  • test/unit/azure-swa.test.ts
  • test/unit/build-info.test.ts
🚧 Files skipped from review as they are similar to previous changes (1)
  • test/unit/azure-swa.test.ts

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant