fix(build,azure-swa): prefer index.mjs serverEntry; fix Azure SWA request URL and body#4352
fix(build,azure-swa): prefer index.mjs serverEntry; fix Azure SWA request URL and body#4352tonoizer wants to merge 3 commits into
Conversation
…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 is attempting to deploy a commit to the Nitro Team on Vercel. A member of the Team first needs to authorize it. |
|
No actionable comments were generated in the recent review. 🎉 ℹ️ Recent review info⚙️ Run configurationConfiguration used: Organization UI Review profile: CHILL Plan: Pro Run ID: 📒 Files selected for processing (2)
🚧 Files skipped from review as they are similar to previous changes (2)
📝 WalkthroughWalkthroughTwo independent helpers are extracted and exported with unit tests. ChangesServer Entry Filename Resolution
Azure SWA URL Resolution and Body Serialization
Estimated code review effort🎯 2 (Simple) | ⏱️ ~10 minutes Possibly related PRs
🚥 Pre-merge checks | ✅ 4 | ❌ 1❌ Failed checks (1 warning)
✅ Passed checks (4 passed)
✏️ Tip: You can configure your own custom pre-merge checks in the settings. ✨ Finishing Touches🧪 Generate unit tests (beta)
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. Comment |
There was a problem hiding this comment.
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
📒 Files selected for processing (4)
src/build/info.tssrc/presets/azure/runtime/azure-swa.tstest/unit/azure-swa.test.tstest/unit/build-info.test.ts
There was a problem hiding this comment.
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
📒 Files selected for processing (3)
src/presets/azure/runtime/_utils.tssrc/presets/azure/runtime/azure-swa.tstest/unit/azure-swa.test.ts
🚧 Files skipped from review as they are similar to previous changes (1)
- src/presets/azure/runtime/azure-swa.ts
…on content types and add corresponding unit test
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 previewissue:pnpm devworks — only production preview is broken because it relies on.output/nitro.json.1. Wrong
serverEntryinnitro.jsonwriteBuildInfoused.find()to pick the firstisEntrychunk from Rollup output. Vite Module Federation marks expose chunks as entry points too, so when they appear beforeserver/index.mjsin the output array,nitro.jsonpoints at e.g.server/_chunks/app.mjsinstead of the server bundle.vite previewthen loads a file with nofetchhandler → 404 everywhere.2. Azure SWA: relative URL passed to
new Request()azure-swa.tsconstructed a relative path (/api/...orpathname + search) and passed it directly tonew 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.bodywas set incorrectly for Azure Functions v3 (which expectsstring | Buffer). Text responses could be empty/wrong; binary responses were corrupted when unconditionally calling.text().Fix
resolveNitroServerEntryFileName— when multipleisEntrychunks exist, prefer the one matching/(^|\/)index\.mjs$/(Nitro's server entry convention) instead of taking array-first.resolveAzureSwaRequestUrl— usex-ms-original-urlas-is (it's already an absolute URL) or constructnew URL(path, "http://nitro.local").hreffor direct/api/*calls.azureResponseBody— inspectcontent-typeand return UTF-8 string for text types orBufferfor binary (same pattern as AWS Lambda preset).Test plan
pnpm vitest run test/unit/build-info.test.ts test/unit/azure-swa.test.tspnpm build && pnpm checkpasses;pnpm previewserves SPA +/api/hello/api/*routes return real JSON/HTML; binary routes return correct bytes