Skip to content

Commit

Permalink
feat: handle docs URL edge cases
Browse files Browse the repository at this point in the history
  • Loading branch information
tobySolutions committed Oct 16, 2024
1 parent b790467 commit 461c2d2
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 3 deletions.
2 changes: 1 addition & 1 deletion src/layouts/DocsHtml.astro
Original file line number Diff line number Diff line change
Expand Up @@ -109,7 +109,7 @@ const canonicalUrl = generateCanonicalUrl(baseUrl, docsPageSlug);
<!-- PostHog -->
<PostHog />
</head>
<body
<body
class="relative flex flex-col bg-black font-sans text-gray-dark-11 selection:bg-yellow-dark-9 selection:text-black"
>
<!-- Google Tag Manager (noscript version) -->
Expand Down
12 changes: 10 additions & 2 deletions src/utils/generateCanonicalUrl.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,13 @@
export function generateCanonicalUrl(baseUrl: string, slug?: string) {
if (!slug) return `${baseUrl}/`;
const cleanBaseUrl = baseUrl.replace(/\/$/, '');

return `${baseUrl}/${slug.replace(/^\/|\/$/g, '')}/`;
if (!slug) return `${cleanBaseUrl}/`;

const sanitizedSlug = slug
.replace(/^\/|\/$/g, '')
.split('/')
.map((segment) => encodeURIComponent(segment))
.join('/');

return `${cleanBaseUrl}/${sanitizedSlug}/`;
}

0 comments on commit 461c2d2

Please sign in to comment.