Skip to content
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

fix: issue where canonical was missing trailing slash #567

Merged
merged 3 commits into from
Aug 1, 2023
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/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');
});
});
Loading