Skip to content

docs: fix event.path usage and misleading middleware path in examples#4384

Closed
zhsj0089944 wants to merge 1 commit into
nitrojs:mainfrom
zhsj0089944:fix/docs-event-path
Closed

docs: fix event.path usage and misleading middleware path in examples#4384
zhsj0089944 wants to merge 1 commit into
nitrojs:mainfrom
zhsj0089944:fix/docs-event-path

Conversation

@zhsj0089944

Copy link
Copy Markdown

Changes

1. lifecycle.md: Fix event.pathevent.url.pathname

The request hook receives an HTTPEvent (from h3), not an H3Event. The HTTPEvent interface does not have a path property — the correct way to access the path is event.url.pathname.

-    console.log(`Incoming request on ${event.path}`);
+    console.log(`Incoming request on ${event.url.pathname}`);

Fixes #4375

2. routing.md: Fix misleading middleware handler path

The route-scoped middleware example used ./server/middleware/api-auth.ts as the handler path. Since files in the middleware/ directory are automatically registered as global middleware (matching /**), this:

  • Makes the route: "/api/**" config completely redundant
  • Causes the middleware to execute twice (once globally, once via the route config)

Changed to ./server/handlers/api-auth.ts to avoid the conflict.

Fixes #4354

1. lifecycle.md: event is an HTTPEvent, not H3Event, so event.path
   does not exist. Use event.url.pathname instead.

2. routing.md: The route-scoped middleware example used
   ./server/middleware/api-auth.ts as the handler path. Since files in
   the middleware/ directory are automatically registered as global
   middleware, this makes the route: '/api/**' config redundant and
   causes double-execution. Change to ./server/handlers/api-auth.ts
   to avoid the conflict.

Fixes nitrojs#4375, Fixes nitrojs#4354
@zhsj0089944
zhsj0089944 requested a review from pi0 as a code owner June 26, 2026 05:31
@vercel

vercel Bot commented Jun 26, 2026

Copy link
Copy Markdown

Someone 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 26, 2026

Copy link
Copy Markdown
Contributor

Review Change Stack

📝 Walkthrough

Walkthrough

This PR updates two documentation examples in the routing guide to use ./server/handlers/api-auth.ts, and changes the lifecycle request hook example to log event.url.pathname instead of event.path.

Changes

Routing documentation examples

Layer / File(s) Summary
Middleware handler path examples
docs/1.docs/5.routing.md
Two routing examples update the documented handler path for api/** and /api/** middleware entries to ./server/handlers/api-auth.ts.

Lifecycle request hook example

Layer / File(s) Summary
Request hook pathname example
docs/1.docs/50.lifecycle.md
The request hook sample changes its logged request target from event.path to event.url.pathname.

Estimated code review effort

🎯 1 (Trivial) | ⏱️ ~2 minutes

🚥 Pre-merge checks | ✅ 5
✅ Passed checks (5 passed)
Check name Status Explanation
Title check ✅ Passed The title follows conventional commits and clearly summarizes the two documentation fixes.
Description check ✅ Passed The description matches the documentation changes and explains both fixes with linked issue references.
Linked Issues check ✅ Passed The PR addresses both linked issues by replacing the invalid HTTPEvent path example and moving the middleware example out of server/middleware/.
Out of Scope Changes check ✅ Passed The changes stay within the documented routing and lifecycle examples and do not introduce unrelated code changes.
Docstring Coverage ✅ Passed No functions found in the changed files to evaluate docstring coverage. Skipping docstring coverage check.
✨ 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.

Caution

Some comments are outside the diff and can’t be posted inline due to platform limitations.

⚠️ Outside diff range comments (1)
docs/1.docs/50.lifecycle.md (1)

124-128: 🎯 Functional Correctness | 🟡 Minor | ⚡ Quick win

Update response hook example for consistency.

The response hook example still uses event.path, but the hooks reference table types both request and response hooks with event: HTTPEvent. If path is not available on HTTPEvent, this example should also use event.url.pathname to match the request hook fix and avoid reader confusion.

-    console.log(`Response ${res.status} for ${event.path}`);
+    console.log(`Response ${res.status} for ${event.url.pathname}`);
🤖 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 `@docs/1.docs/50.lifecycle.md` around lines 124 - 128, The `response` hook
example in `definePlugin` is still using `event.path`, which is inconsistent
with the `HTTPEvent`-based hooks reference and may not exist on that object.
Update the example inside the `nitroApp.hooks.hook("response", ...)` callback to
use `event.url.pathname`, matching the `request` hook example and keeping the
docs consistent.
🤖 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.

Outside diff comments:
In `@docs/1.docs/50.lifecycle.md`:
- Around line 124-128: The `response` hook example in `definePlugin` is still
using `event.path`, which is inconsistent with the `HTTPEvent`-based hooks
reference and may not exist on that object. Update the example inside the
`nitroApp.hooks.hook("response", ...)` callback to use `event.url.pathname`,
matching the `request` hook example and keeping the docs consistent.

ℹ️ Review info
⚙️ Run configuration

Configuration used: Organization UI

Review profile: CHILL

Plan: Pro

Run ID: 90896e77-764d-453d-b562-ee4a3f8d1731

📥 Commits

Reviewing files that changed from the base of the PR and between 37db9ee and dc73a3a.

📒 Files selected for processing (2)
  • docs/1.docs/5.routing.md
  • docs/1.docs/50.lifecycle.md

@pi0

pi0 commented Jun 26, 2026

Copy link
Copy Markdown
Member

Since files in the middleware/ directory are automatically registered as global middleware

only when serverDir is set

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.

docs: misleading directory path in route-scoped middleware handlers example doc: Usage .path in HTTPEvent is not correct

2 participants