diff --git a/.changeset/curly-apricots-stare.md b/.changeset/curly-apricots-stare.md new file mode 100644 index 000000000..1c9c622b5 --- /dev/null +++ b/.changeset/curly-apricots-stare.md @@ -0,0 +1,5 @@ +--- +"@headstartwp/next": patch +--- + +Fix: only convertUrls if url starts with sourceUrl diff --git a/packages/next/src/components/Yoast.tsx b/packages/next/src/components/Yoast.tsx index ce348c3a3..ea6aa258e 100644 --- a/packages/next/src/components/Yoast.tsx +++ b/packages/next/src/components/Yoast.tsx @@ -12,8 +12,15 @@ import { import { useSettings } from '@headstartwp/core/react'; import Head from 'next/head'; -function convertUrl(url: string, hostUrl: string, sourceUrl: string) { - return `${hostUrl}${removeSourceUrl({ link: url, backendUrl: sourceUrl })}`; +export function convertUrl(url: string, hostUrl: string, sourceUrl: string) { + if (!url.startsWith(sourceUrl)) { + return url; + } + + return `${hostUrl}${removeSourceUrl({ + link: url.replace(/\/?$/, '/'), + backendUrl: sourceUrl, + })}`; } type Props = { diff --git a/packages/next/src/components/__tests__/Yoast.tsx b/packages/next/src/components/__tests__/Yoast.tsx new file mode 100644 index 000000000..2405e164e --- /dev/null +++ b/packages/next/src/components/__tests__/Yoast.tsx @@ -0,0 +1,25 @@ +import { convertUrl } from '../Yoast'; + +describe('convertUrl', () => { + it('root works without trailing slash', () => { + expect( + convertUrl('https://test.com/test', 'https://test.test.com', 'https://test.com/test'), + ).toBe('https://test.test.com/'); + }); + + it('root works with trailing slash', () => { + expect( + convertUrl('https://test.com/test/', 'https://test.test.com', 'https://test.com/test'), + ).toBe('https://test.test.com/'); + }); + + it('external url returns external url', () => { + expect( + convertUrl( + 'https://external.com/test', + 'https://test.test.com', + 'https://test.com/test', + ), + ).toBe('https://external.com/test'); + }); +});