Skip to content

Commit 07745ed

Browse files
authored
Merge pull request #169 from Alpha4615/fix/relative-uri-in-redirect-issue-168
Correctly parse relative URIs in manually handled redirects #168
2 parents 2bf5bbf + c98ece1 commit 07745ed

File tree

1 file changed

+7
-1
lines changed

1 file changed

+7
-1
lines changed

index.ts

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -432,7 +432,13 @@ export async function getLinkPreview(
432432
fetchOptions.redirect === `manual` &&
433433
options?.handleRedirects
434434
) {
435-
const forwardedUrl = response.headers.get(`location`) || ``;
435+
const locationHeader = response.headers.get(`location`) || ``;
436+
const isAbsoluteURI = locationHeader.startsWith('http://') || locationHeader.startsWith('https://');
437+
438+
// Resolve the URL, handling both absolute and relative URLs
439+
const forwardedUrl = isAbsoluteURI
440+
? locationHeader
441+
: urlObj.resolve(fetchUrl, locationHeader);
436442

437443
if (!options.handleRedirects(fetchUrl, forwardedUrl)) {
438444
throw new Error(`link-preview-js could not handle redirect`);

0 commit comments

Comments
 (0)