-
-
Notifications
You must be signed in to change notification settings - Fork 1.7k
[WIP] feat(nextjs): Add URL to tags of server component and layout issues #16499
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
Closed
Closed
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,129 @@ | ||
import { parseStringToURLObject, getSanitizedUrlStringFromUrlObject } from '@sentry/core'; | ||
|
||
/** | ||
* Type definition for component route parameters | ||
*/ | ||
type ComponentRouteParams = Record<string, string> | undefined; | ||
|
||
/** | ||
* Type definition for headers dictionary | ||
*/ | ||
type HeadersDict = Record<string, string> | undefined; | ||
|
||
|
||
const HEADER_KEYS = { | ||
FORWARDED_PROTO: 'x-forwarded-proto', | ||
FORWARDED_HOST: 'x-forwarded-host', | ||
HOST: 'host', | ||
REFERER: 'referer', | ||
} as const; | ||
|
||
/** | ||
* Replaces route parameters in a path template with their values | ||
* @param path - The path template containing parameters in [paramName] format | ||
* @param params - Optional route parameters to replace in the template | ||
* @returns The path with parameters replaced | ||
*/ | ||
function substituteRouteParams(path: string, params?: ComponentRouteParams): string { | ||
if (!params || typeof params !== 'object') return path; | ||
|
||
for (const [key, value] of Object.entries(params)) { | ||
const regex = new RegExp(`\\[${key}\\]`, 'g'); | ||
path = path.replace(regex, encodeURIComponent(value)); | ||
} | ||
return path; | ||
} | ||
|
||
/** | ||
* Normalizes a path by removing route groups and multiple slashes | ||
* @param path - The path to normalize | ||
* @returns The normalized path | ||
*/ | ||
function sanitizeRoutePath(path: string): string { | ||
return path | ||
.replace(/\([^)]+\)/g, '') // Remove route groups | ||
.replace(/\/{2,}/g, '/') // Normalize multiple slashes | ||
.replace(/\/$/, '') // Remove trailing slash | ||
|| '/'; // Ensure root path is '/' | ||
} | ||
|
||
/** | ||
* Constructs a full URL from the component route, parameters, and headers. | ||
* | ||
* @param componentRoute - The route template to construct the URL from | ||
* @param params - Optional route parameters to replace in the template | ||
* @param headersDict - Optional headers containing protocol and host information | ||
* @param pathname - Optional pathname coming from parent span "http.target" | ||
* @returns A sanitized URL string | ||
*/ | ||
export function buildUrlFromComponentRoute( | ||
componentRoute: string, | ||
params?: ComponentRouteParams, | ||
headersDict?: HeadersDict, | ||
pathname?: string, | ||
): string { | ||
const parameterisedPath = substituteRouteParams(componentRoute, params); | ||
// pathname has precedence over the parameterised path if it exists | ||
// spans like generateMetadata and Server Component rendering, are normally direct children of the root http.server span | ||
const path = pathname ?? sanitizeRoutePath(parameterisedPath); | ||
|
||
const protocol = headersDict?.[HEADER_KEYS.FORWARDED_PROTO]; | ||
const host = headersDict?.[HEADER_KEYS.FORWARDED_HOST] || headersDict?.[HEADER_KEYS.HOST]; | ||
|
||
if (!protocol || !host) { | ||
return path; | ||
} | ||
|
||
const fullUrl = `${protocol}://${host}${path}`; | ||
|
||
const urlObject = parseStringToURLObject(fullUrl); | ||
if (!urlObject) { | ||
return path; | ||
} | ||
|
||
return getSanitizedUrlStringFromUrlObject(urlObject); | ||
} | ||
|
||
/** | ||
* Returns a sanitized URL string from the referer header if it exists and is valid. | ||
* | ||
* @param headersDict - Optional headers containing the referer | ||
* @returns A sanitized URL string or undefined if referer is missing/invalid | ||
*/ | ||
export function extractSanitizedUrlFromRefererHeader(headersDict?: HeadersDict): string | undefined { | ||
const referer = headersDict?.[HEADER_KEYS.REFERER]; | ||
if (!referer) { | ||
return undefined; | ||
} | ||
|
||
try { | ||
const refererUrl = new URL(referer); | ||
return getSanitizedUrlStringFromUrlObject(refererUrl); | ||
} catch (error) { | ||
return undefined; | ||
} | ||
} | ||
|
||
/** | ||
* Returns a sanitized URL string using the referer header if available, | ||
* otherwise constructs the URL from the component route, params, and headers. | ||
* | ||
* @param componentRoute - The route template to construct the URL from | ||
* @param params - Optional route parameters to replace in the template | ||
* @param headersDict - Optional headers containing protocol, host, and referer | ||
* @param pathname - Optional pathname coming from root span "http.target" | ||
* @returns A sanitized URL string | ||
*/ | ||
export function getSanitizedRequestUrl( | ||
componentRoute: string, | ||
params?: ComponentRouteParams, | ||
headersDict?: HeadersDict, | ||
pathname?: string, | ||
): string { | ||
const refererUrl = extractSanitizedUrlFromRefererHeader(headersDict); | ||
if (refererUrl) { | ||
return refererUrl; | ||
} | ||
|
||
return buildUrlFromComponentRoute(componentRoute, params, headersDict, pathname); | ||
} |
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,150 @@ | ||
|
||
import { describe, expect, it } from 'vitest'; | ||
import { | ||
buildUrlFromComponentRoute, | ||
extractSanitizedUrlFromRefererHeader, | ||
getSanitizedRequestUrl, | ||
} from '../../src/common/utils/urls'; | ||
|
||
describe('URL Utilities', () => { | ||
describe('buildUrlFromComponentRoute', () => { | ||
const mockHeaders = { | ||
'x-forwarded-proto': 'https', | ||
'x-forwarded-host': 'example.com', | ||
host: 'example.com', | ||
}; | ||
|
||
it('should build URL with protocol and host', () => { | ||
const result = buildUrlFromComponentRoute('/test', undefined, mockHeaders); | ||
expect(result).toBe('https://example.com/test'); | ||
}); | ||
|
||
it('should handle route parameters', () => { | ||
const result = buildUrlFromComponentRoute('/users/[id]/posts/[postId]', { id: '123', postId: '456' }, mockHeaders); | ||
expect(result).toBe('https://example.com/users/123/posts/456'); | ||
}); | ||
|
||
it('should handle multiple instances of the same parameter', () => { | ||
const result = buildUrlFromComponentRoute('/users/[id]/[id]/profile', { id: '123' }, mockHeaders); | ||
expect(result).toBe('https://example.com/users/123/123/profile'); | ||
}); | ||
|
||
it('should handle special characters in parameters', () => { | ||
const result = buildUrlFromComponentRoute('/search/[query]', { query: 'hello world' }, mockHeaders); | ||
expect(result).toBe('https://example.com/search/hello%20world'); | ||
}); | ||
|
||
it('should handle route groups', () => { | ||
const result = buildUrlFromComponentRoute('/(auth)/login', undefined, mockHeaders); | ||
expect(result).toBe('https://example.com/login'); | ||
}); | ||
|
||
it('should normalize multiple slashes', () => { | ||
const result = buildUrlFromComponentRoute('//users///profile', undefined, mockHeaders); | ||
expect(result).toBe('https://example.com/users/profile'); | ||
}); | ||
|
||
it('should handle trailing slashes', () => { | ||
const result = buildUrlFromComponentRoute('/users/', undefined, mockHeaders); | ||
expect(result).toBe('https://example.com/users'); | ||
}); | ||
|
||
it('should handle root path', () => { | ||
const result = buildUrlFromComponentRoute('', undefined, mockHeaders); | ||
expect(result).toBe('https://example.com/'); | ||
}); | ||
|
||
it('should use pathname if provided', () => { | ||
const result = buildUrlFromComponentRoute('/original', undefined, mockHeaders, '/override'); | ||
expect(result).toBe('https://example.com/override'); | ||
}); | ||
|
||
it('should return path only if protocol is missing', () => { | ||
const result = buildUrlFromComponentRoute('/test', undefined, { host: 'example.com' }); | ||
expect(result).toBe('/test'); | ||
}); | ||
|
||
it('should return path only if host is missing', () => { | ||
const result = buildUrlFromComponentRoute('/test', undefined, { 'x-forwarded-proto': 'https' }); | ||
expect(result).toBe('/test'); | ||
}); | ||
|
||
it('should handle invalid URL construction', () => { | ||
const result = buildUrlFromComponentRoute('/test', undefined, { | ||
'x-forwarded-proto': 'invalid://', | ||
host: 'example.com', | ||
}); | ||
expect(result).toBe('/test'); | ||
}); | ||
}); | ||
|
||
describe('extractSanitizedUrlFromRefererHeader', () => { | ||
it('should return undefined if referer is missing', () => { | ||
const result = extractSanitizedUrlFromRefererHeader({}); | ||
expect(result).toBeUndefined(); | ||
}); | ||
|
||
it('should return undefined if referer is invalid', () => { | ||
const result = extractSanitizedUrlFromRefererHeader({ referer: 'invalid-url' }); | ||
expect(result).toBeUndefined(); | ||
}); | ||
|
||
it('should handle referer with special characters', () => { | ||
const headers = { referer: 'https://example.com/path with spaces/ümlaut' }; | ||
const result = extractSanitizedUrlFromRefererHeader(headers); | ||
expect(result).toBe('https://example.com/path%20with%20spaces/%C3%BCmlaut'); | ||
}); | ||
}); | ||
|
||
describe('getSanitizedRequestUrl', () => { | ||
const mockHeaders = { | ||
'x-forwarded-proto': 'https', | ||
'x-forwarded-host': 'example.com', | ||
host: 'example.com', | ||
}; | ||
|
||
it('should use referer URL if available and valid', () => { | ||
const headers = { | ||
...mockHeaders, | ||
referer: 'https://example.com/referer-page', | ||
}; | ||
const result = getSanitizedRequestUrl('/original', undefined, headers); | ||
expect(result).toBe('https://example.com/referer-page'); | ||
}); | ||
|
||
it('should fall back to building URL if referer is invalid', () => { | ||
const headers = { | ||
...mockHeaders, | ||
referer: 'invalid-url', | ||
}; | ||
const result = getSanitizedRequestUrl('/fallback', undefined, headers); | ||
expect(result).toBe('https://example.com/fallback'); | ||
}); | ||
|
||
it('should fall back to building URL if referer is missing', () => { | ||
const result = getSanitizedRequestUrl('/fallback', undefined, mockHeaders); | ||
expect(result).toBe('https://example.com/fallback'); | ||
}); | ||
|
||
it('should handle route parameters in fallback URL', () => { | ||
const result = getSanitizedRequestUrl('/users/[id]', { id: '123' }, mockHeaders); | ||
expect(result).toBe('https://example.com/users/123'); | ||
}); | ||
|
||
it('should handle pathname override in fallback URL', () => { | ||
const result = getSanitizedRequestUrl('/original', undefined, mockHeaders, '/override'); | ||
expect(result).toBe('https://example.com/override'); | ||
}); | ||
|
||
it('should handle empty headers', () => { | ||
const result = getSanitizedRequestUrl('/test', undefined, {}); | ||
expect(result).toBe('/test'); | ||
}); | ||
|
||
it('should handle undefined headers', () => { | ||
const result = getSanitizedRequestUrl('/test', undefined, undefined); | ||
expect(result).toBe('/test'); | ||
}); | ||
}); | ||
}); | ||
|
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.