Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .changeset/fix-cross-domain-redirect-skip.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@headstartwp/core": patch
---

Fix: shouldSkipRedirect incorrectly skipping cross-domain redirects when pathnames match. Fixes #941
14 changes: 14 additions & 0 deletions packages/core/src/utils/__tests__/fetchRedirect.ts
Original file line number Diff line number Diff line change
Expand Up @@ -51,4 +51,18 @@ describe('fetchRedirect', () => {

global.fetch = originalFetch;
});

it('handles cross-domain redirect with same pathname', async () => {
const result = await fetchRedirect('/recipe/my-recipe/', 'http://example.com/');

expect(result.location).toBe('https://www.external-domain.com/recipe/my-recipe/');
expect(result.status).toBe(302);
});

it('handles cross-domain redirect with different pathname', async () => {
const result = await fetchRedirect('/old-recipe/', 'http://example.com/');

expect(result.location).toBe('https://www.external-domain.com/new-recipe/');
expect(result.status).toBe(301);
});
});
5 changes: 5 additions & 0 deletions packages/core/src/utils/fetchRedirect.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,11 @@ function shouldSkipRedirect(link: string, redirect: string, sourceUrl: string) {
return true;
}

// Cross-domain redirects should never be skipped
if (linkURL.host !== redirectURL.host) {
return false;
}

const linkParams = linkURL.searchParams;
const redirectParams = redirectURL.searchParams;

Expand Down
10 changes: 10 additions & 0 deletions packages/core/test/server-handlers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,16 @@ const handlers = [
return res(redirect('http://example.com/redirect-test-missing-slash', 301));
}),

// Cross-domain redirect with same pathname
rest.head('http://example.com/recipe/my-recipe/', (req, res) => {
return res(redirect('https://www.external-domain.com/recipe/my-recipe/', 302));
}),

// Cross-domain redirect with different pathname
rest.head('http://example.com/old-recipe/', (req, res) => {
return res(redirect('https://www.external-domain.com/new-recipe/', 301));
}),

rest.get<DefaultRequestBody, TestEndpointResponse>(/\/test-endpoint/, (req, res, ctx) => {
return res(ctx.json({ ok: true }));
}),
Expand Down
Loading