Skip to content

Commit

Permalink
fix: issue where canonical was missing trailing slash (#567)
Browse files Browse the repository at this point in the history
Co-authored-by: Nícholas Oliveira <[email protected]>
  • Loading branch information
tobeycodes and nicholasio authored Aug 1, 2023
1 parent 1494a33 commit 232f4e6
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 2 deletions.
5 changes: 5 additions & 0 deletions .changeset/curly-apricots-stare.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@headstartwp/next": patch
---

Fix: only convertUrls if url starts with sourceUrl
11 changes: 9 additions & 2 deletions packages/next/src/components/Yoast.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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 = {
Expand Down
25 changes: 25 additions & 0 deletions packages/next/src/components/__tests__/Yoast.tsx
Original file line number Diff line number Diff line change
@@ -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');
});
});

1 comment on commit 232f4e6

@vercel
Copy link

@vercel vercel bot commented on 232f4e6 Aug 1, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please sign in to comment.