Conversation
📝 WalkthroughWalkthroughThe custom-domain interception middleware in Express was refined to only apply to Changes
Estimated code review effort🎯 2 (Simple) | ⏱️ ~8 minutes Poem
🚥 Pre-merge checks | ✅ 3✅ Passed checks (3 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 the current code and only fix it if needed.
Inline comments:
In `@src/server/app.ts`:
- Line 129: The root-path check in the middleware uses req.originalUrl (in the
conditional that starts with "if (req.method === 'GET' && req.originalUrl ===
'/' && req.accepts('html'))") which includes query strings and therefore misses
requests like "/?ref=..."; change the condition to use req.path (e.g., "req.path
=== '/'") so the check reliably matches only the root path regardless of query
params, leaving the rest of the predicate (req.method === 'GET' and
req.accepts('html')) unchanged.
🪄 Autofix (Beta)
Fix all unresolved CodeRabbit comments on this PR:
- Push a commit to this branch (recommended)
- Create a new PR with the fixes
| // Custom domain: only for root path so /p/:slug and /status/:slug are not intercepted | ||
| app.use('/*', async (req, res, next) => { | ||
| if (req.method === 'GET' && req.accepts('html')) { | ||
| if (req.method === 'GET' && req.originalUrl === '/' && req.accepts('html')) { |
There was a problem hiding this comment.
🧩 Analysis chain
🌐 Web query:
In Express.js, does req.originalUrlinclude the query string, and isreq.paththe recommended field to match only the URL pathname (e.g.,/)?
💡 Result:
Yes.
req.originalUrlincludes the query string (e.g. a request to/search?q=somethingyields"/search?q=something"). [1]req.pathis the field that contains only the URL pathname (no query string), so it’s the appropriate choice when you want to match just/,/users, etc. [1]
Note: when called from middleware mounted with app.use('/mount', ...), req.path does not include the mount point. [1]
Sources
[1] Express 5.x API Reference — req.originalUrl and req.path documentation (expressjs.com) (expressjs.com)
Citations:
🏁 Script executed:
# Check if file exists and get context around Line 129
if [ -f src/server/app.ts ]; then
echo "=== File found. Context around Line 129 ==="
sed -n '120,140p' src/server/app.ts
else
echo "File not found"
fiRepository: msgbyte/tianji
Length of output: 916
Use req.path instead of req.originalUrl for root matching.
Line 129 currently misses root requests that include query params (e.g. /?ref=...) because req.originalUrl includes the query string. This causes custom-domain rendering to be skipped unexpectedly, contradicting the middleware's stated intent to handle "only for root path."
Suggested fix
- if (req.method === 'GET' && req.originalUrl === '/' && req.accepts('html')) {
+ if (req.method === 'GET' && req.path === '/' && req.accepts('html')) {📝 Committable suggestion
‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.
| if (req.method === 'GET' && req.originalUrl === '/' && req.accepts('html')) { | |
| if (req.method === 'GET' && req.path === '/' && req.accepts('html')) { |
🤖 Prompt for AI Agents
Verify each finding against the current code and only fix it if needed.
In `@src/server/app.ts` at line 129, The root-path check in the middleware uses
req.originalUrl (in the conditional that starts with "if (req.method === 'GET'
&& req.originalUrl === '/' && req.accepts('html'))") which includes query
strings and therefore misses requests like "/?ref=..."; change the condition to
use req.path (e.g., "req.path === '/'") so the check reliably matches only the
root path regardless of query params, leaving the rest of the predicate
(req.method === 'GET' and req.accepts('html')) unchanged.
Fixed custom-domain routing to only trigger on the root path (/) for HTML GET requests.
This prevents the custom-domain middleware from intercepting other routes (such as status or static page paths), ensuring normal route handling and correct behavior after build.
Summary by CodeRabbit
/p/:slugand/status/:slugroutes work correctly on custom domains instead of being incorrectly intercepted.