fix(server): use ufo for query parsing to fix h3 v2 compatibility#493
Conversation
…ix h3 v2 compatibility `getQuery(event)` in the `getKey` callback throws `ERR_INVALID_URL` under h3 v2 because it calls `new URL(event.req.url)` with a relative path. Use `getRequestURL` which already handles this correctly and is used in the handler body.
|
Note Reviews pausedIt looks like this branch is under active development. To avoid overwhelming you with review comments due to an influx of new commits, CodeRabbit has automatically paused this review. You can configure this behavior by changing the Use the following commands to manage reviews:
Use the checkboxes below for quick actions:
📝 WalkthroughWalkthroughThe PR replaces h3 query helpers by parsing event.path with ufo's parsePath/parseQuery, normalizes the parsed icons query into a comma-split icons array in the cached icon handler, updates the fallback apiUrl to ./.json?icons=..., and changes SWR getKey to hash icons.join(','). package.json dependencies add ufo and remove pathe from the dependencies block. Estimated code review effort🎯 3 (Moderate) | ⏱️ ~20 minutes 🚥 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 |
commit: |
`getRequestURL` throws `ERR_INVALID_URL` under h3 v2 because `event.req.url` is a relative path with no base. Parse `event.path` (relative in both h3 v1 and v2) against a dummy base instead, so query params can be read safely.
getQuery with getRequestURL in getKey to fix h3 v2 compatibilityThere was a problem hiding this comment.
Actionable comments posted: 1
Caution
Some comments are outside the diff and can’t be posted inline due to platform limitations.
⚠️ Outside diff range comments (1)
src/runtime/server/api.ts (1)
19-21:⚠️ Potential issue | 🔴 Critical | ⚡ Quick winWire the parsed URL into the handler.
getRequestURLis never called, so CI already fails on the unused helper, and the fallback branch still readsurl.pathname/url.searchat Line 64 even thoughurlis no longer defined. Please initialize the parsed URL once in the handler and reuse it there.Proposed fix
export default defineCachedEventHandler(async (event: H3Event) => { + const url = getRequestURL(event) const options = useAppConfig().icon as NuxtIconRuntimeOptions const collectionName = event.context.params?.collection?.replace(/\.json$/, '') const collection = collectionName ? await collections[collectionName]?.() : null🤖 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/runtime/server/api.ts` around lines 19 - 21, The handler never initializes the parsed URL and getRequestURL is unused, so call getRequestURL(event) at the start of the request handler (assign to a const named url) and replace any direct references to event.path/event.url or the undefined url variable with this parsed URL (use url.pathname and url.search when building the request target); ensure getRequestURL(event) is the single source of truth for URL parsing and remove any duplicate parsing/fallbacks that cause the unused-helper or undefined-url issues.
🤖 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/runtime/server/api.ts`:
- Around line 39-40: The icons query parsing currently yields [''] for
missing/empty values causing getIcons() and getKey() to see a bogus empty icon;
update the parsing logic around the iconsQuery/icons variables to normalize
empty or missing values into an empty array by splitting then filtering out
blank segments (e.g., remove empty strings and whitespace-only entries) so icons
becomes [] when no real icons are supplied; apply the same normalization to the
other occurrence referenced (lines ~88-90) so both places produce consistent
cache keys and handler behavior for empty icon queries.
---
Outside diff comments:
In `@src/runtime/server/api.ts`:
- Around line 19-21: The handler never initializes the parsed URL and
getRequestURL is unused, so call getRequestURL(event) at the start of the
request handler (assign to a const named url) and replace any direct references
to event.path/event.url or the undefined url variable with this parsed URL (use
url.pathname and url.search when building the request target); ensure
getRequestURL(event) is the single source of truth for URL parsing and remove
any duplicate parsing/fallbacks that cause the unused-helper or undefined-url
issues.
🪄 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: bdf5a196-10d0-40b4-bd95-2d3c1fae80fa
⛔ Files ignored due to path filters (1)
pnpm-lock.yamlis excluded by!**/pnpm-lock.yaml
📒 Files selected for processing (2)
package.jsonsrc/runtime/server/api.ts
✅ Files skipped from review due to trivial changes (1)
- package.json
ufo for query parsing to fix h3 v2 compatibility
Summary
getQuery/getRequestURLin the icon API handler andgetKey. Both throwERR_INVALID_URLunder h3 v2.event.pathusingufoparsePathandparseQueryfor h3 v1 and v2 compatibility