-
Notifications
You must be signed in to change notification settings - Fork 30.7k
perf(server): avoid URL construction for pathname/query extraction #91561
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
benfavre
wants to merge
2
commits into
vercel:canary
Choose a base branch
from
benfavre:perf/avoid-url-construction
base: canary
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from 1 commit
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,86 @@ | ||
| import { getPathnameFromUrl, getQueryParamFromUrl } from './url-string-utils' | ||
|
|
||
| describe('getPathnameFromUrl', () => { | ||
| it('returns pathname from a simple path', () => { | ||
| expect(getPathnameFromUrl('/foo/bar')).toBe('/foo/bar') | ||
| }) | ||
|
|
||
| it('strips query string', () => { | ||
| expect(getPathnameFromUrl('/foo?a=1&b=2')).toBe('/foo') | ||
| }) | ||
|
|
||
| it('strips hash fragment', () => { | ||
| expect(getPathnameFromUrl('/foo#section')).toBe('/foo') | ||
| }) | ||
|
|
||
| it('strips both query and hash', () => { | ||
| expect(getPathnameFromUrl('/foo?a=1#section')).toBe('/foo') | ||
| }) | ||
|
|
||
| it('handles hash before query', () => { | ||
| expect(getPathnameFromUrl('/foo#section?a=1')).toBe('/foo') | ||
| }) | ||
|
|
||
| it('returns "/" for empty string', () => { | ||
| expect(getPathnameFromUrl('')).toBe('/') | ||
| }) | ||
|
|
||
| it('returns "/" for undefined', () => { | ||
| expect(getPathnameFromUrl(undefined)).toBe('/') | ||
| }) | ||
|
|
||
| it('returns "/" for root path', () => { | ||
| expect(getPathnameFromUrl('/')).toBe('/') | ||
| }) | ||
|
|
||
| it('handles root with query', () => { | ||
| expect(getPathnameFromUrl('/?_rsc=abc')).toBe('/') | ||
| }) | ||
| }) | ||
|
|
||
| describe('getQueryParamFromUrl', () => { | ||
| it('returns the value of a query parameter', () => { | ||
| expect(getQueryParamFromUrl('/path?_rsc=abc123', '_rsc')).toBe('abc123') | ||
| }) | ||
|
|
||
| it('returns the first occurrence when param appears multiple times', () => { | ||
| expect(getQueryParamFromUrl('/path?a=1&a=2', 'a')).toBe('1') | ||
| }) | ||
|
|
||
| it('returns null when param is absent', () => { | ||
| expect(getQueryParamFromUrl('/path?other=1', '_rsc')).toBeNull() | ||
| }) | ||
|
|
||
| it('returns null when there is no query string', () => { | ||
| expect(getQueryParamFromUrl('/path', '_rsc')).toBeNull() | ||
| }) | ||
|
|
||
| it('returns null for undefined url', () => { | ||
| expect(getQueryParamFromUrl(undefined, '_rsc')).toBeNull() | ||
| }) | ||
|
|
||
| it('returns empty string for empty value', () => { | ||
| expect(getQueryParamFromUrl('/path?_rsc=', '_rsc')).toBe('') | ||
| }) | ||
|
|
||
| it('handles param at the end of query string', () => { | ||
| expect(getQueryParamFromUrl('/path?a=1&_rsc=xyz', '_rsc')).toBe('xyz') | ||
| }) | ||
|
|
||
| it('does not match partial param names', () => { | ||
| // "x_rsc=bad" should not match "_rsc" | ||
| expect(getQueryParamFromUrl('/path?x_rsc=bad&_rsc=good', '_rsc')).toBe( | ||
| 'good' | ||
| ) | ||
| }) | ||
|
|
||
| it('decodes percent-encoded values', () => { | ||
| expect(getQueryParamFromUrl('/path?q=hello%20world', 'q')).toBe( | ||
| 'hello world' | ||
| ) | ||
| }) | ||
|
|
||
| it('stops at hash fragment', () => { | ||
| expect(getQueryParamFromUrl('/path?a=1#hash', 'a')).toBe('1') | ||
| }) | ||
| }) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,75 @@ | ||
| /** | ||
| * Lightweight URL string utilities that avoid `new URL()` construction. | ||
| * | ||
| * In hot server paths, `new URL(req.url, 'http://n')` shows up as significant | ||
| * self-time because it must parse a full spec-compliant URL. When we only need | ||
| * the pathname or a single query-parameter from a *relative* URL such as | ||
| * `/path?query=1`, simple string operations are 5-10x cheaper. | ||
| * | ||
| * Reference: https://tanstack.com/blog/tanstack-start-5x-faster-ssr | ||
| */ | ||
|
|
||
| /** | ||
| * Extract the pathname from a relative URL string (e.g. `/path?q=1` -> `/path`). | ||
| * Returns `'/'` for empty/undefined input. | ||
| */ | ||
| export function getPathnameFromUrl(url: string | undefined): string { | ||
| if (!url) return '/' | ||
| const qIdx = url.indexOf('?') | ||
| const hIdx = url.indexOf('#') | ||
| const end = | ||
| qIdx >= 0 | ||
| ? hIdx >= 0 | ||
| ? Math.min(qIdx, hIdx) | ||
| : qIdx | ||
| : hIdx >= 0 | ||
| ? hIdx | ||
| : url.length | ||
| return url.substring(0, end) || '/' | ||
| } | ||
|
|
||
| /** | ||
| * Get the value of a single query parameter from a URL string without | ||
| * constructing a URL or URLSearchParams object. | ||
| * | ||
| * Only returns the *first* occurrence. Returns `null` when the param is absent. | ||
| */ | ||
| export function getQueryParamFromUrl( | ||
| url: string | undefined, | ||
| param: string | ||
| ): string | null { | ||
| if (!url) return null | ||
| const qIdx = url.indexOf('?') | ||
| if (qIdx < 0) return null | ||
|
|
||
| const search = url.substring(qIdx + 1) | ||
| const target = param + '=' | ||
|
|
||
| // Walk through the search string looking for the param | ||
| let start = 0 | ||
| while (start <= search.length) { | ||
| const idx = search.indexOf(target, start) | ||
| if (idx < 0) return null | ||
|
|
||
| // Make sure we matched at a parameter boundary (start of string or after '&') | ||
| if (idx === 0 || search.charCodeAt(idx - 1) === 38 /* '&' */) { | ||
| const valueStart = idx + target.length | ||
| const ampIdx = search.indexOf('&', valueStart) | ||
| const hashIdx = search.indexOf('#', valueStart) | ||
| const end = | ||
| ampIdx >= 0 | ||
| ? hashIdx >= 0 | ||
| ? Math.min(ampIdx, hashIdx) | ||
| : ampIdx | ||
| : hashIdx >= 0 | ||
| ? hashIdx | ||
| : search.length | ||
| return decodeURIComponent(search.substring(valueStart, end)) | ||
| } | ||
|
|
||
| // Not at a boundary, keep searching | ||
| start = idx + 1 | ||
| } | ||
|
|
||
| return null | ||
| } | ||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.