Parses a URL to read one part
This is how the comment reads inline, under the flagged line.
Why it matters
new URL() allocates, parses and validates the whole string. On a hot path, repeated parsing adds up, and often the framework already gives you the parsed piece.
What to do
Use the parsed value you already have (req.nextUrl, request.path), or parse once and pass it down.
Examples
const path = new URL(req.url).pathname;
const path = req.nextUrl.pathname;
What it catches
It reads a pattern on each line, aware of strings and comments. Languages: TypeScript and JavaScript. Scope: All files. These cases are the rule's own tests: the first set must be flagged and the second must not.
Flagged
const p = new URL(req.url).pathname;
const u = new URL(request.url);
Left alone
const u = new URL(base, href);
Research
`new URL(x)` built only to read one part. Full parsing on a hot path adds up, and the framework often has the part already.
- no outside source yet
Turn it off or change it
In a config file for the CLI, or in the API's rules field:
{
"rules": {
"disable": [
"new-url-parse"
],
"severity": {
"new-url-parse": "medium"
}
}
}
new URL(req.url).pathnamebuilds a full URL object to read one piece.