Skip to content

Commit

Permalink
Add Open Graph and Twitter metadata to website
Browse files Browse the repository at this point in the history
  • Loading branch information
fabian-hiller committed Jan 11, 2024
1 parent be95d36 commit 141d0be
Show file tree
Hide file tree
Showing 6 changed files with 115 additions and 23 deletions.
14 changes: 0 additions & 14 deletions website/src/components/Head.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -29,20 +29,6 @@ export const Head = component$(() => {
<link rel="apple-touch-icon" sizes="180x180" href="/icon-180px.jpg" />
<link rel="manifest" href="/manifest.json" />

<meta
property="og:image"
content={
location.url.pathname === '/'
? '/og-image'
: `/og-image?title=${encodeURIComponent(
head.title
)}&description=${encodeURIComponent(
head.meta.find((item) => item.name === 'description')
?.content || ''
)}&path=${location.url.pathname.split('/')[1]}`
}
/>

<script
async
src="https://umami.valibot.dev/script.js"
Expand Down
4 changes: 4 additions & 0 deletions website/src/routes/api/layout.tsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
import { component$, Slot } from '@builder.io/qwik';
import type { DocumentHead } from '@builder.io/qwik-city';
import { DocsLayout } from '~/components';
import { addArticleMetadata } from '~/utils';

export const head: DocumentHead = addArticleMetadata;

export default component$(() => (
<DocsLayout>
Expand Down
4 changes: 4 additions & 0 deletions website/src/routes/guides/layout.tsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
import { component$, Slot } from '@builder.io/qwik';
import type { DocumentHead } from '@builder.io/qwik-city';
import { DocsLayout } from '~/components';
import { addArticleMetadata } from '~/utils';

export const head: DocumentHead = addArticleMetadata;

export default component$(() => (
<DocsLayout>
Expand Down
43 changes: 34 additions & 9 deletions website/src/routes/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -11,15 +11,40 @@ import { ActionButton, ButtonGroup, Expandable, TextLink } from '~/components';
import { PlusIcon } from '~/icons';
import { blurredCodeDarkUrl, blurredCodeLightUrl } from '~/images';

export const head: DocumentHead = {
title: 'Valibot: The modular and type safe schema library',
meta: [
{
name: 'description',
content:
'Validate unknown data with Valibot, the open source schema library with bundle size, type safety and developer experience in mind.',
},
],
export const head: DocumentHead = ({ url }) => {
const title = 'Valibot: The modular and type safe schema library';
const description =
'Validate unknown data with Valibot, the open source schema library with bundle size, type safety and developer experience in mind.';

return {
title,
meta: [
{
name: 'description',
content: description,
},
{
name: 'og:type',
content: 'website',
},
{
name: 'og:url',
content: url.href,
},
{
name: 'og:title',
content: title,
},
{
name: 'og:description',
content: description,
},
{
name: 'og:image',
content: `${url.origin}/og-image`,
},
],
};
};

/**
Expand Down
72 changes: 72 additions & 0 deletions website/src/utils/addArticleMetadata.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
import type {
DocumentHeadProps,
DocumentHeadValue,
} from '@builder.io/qwik-city';

/**
* Adds Open Graph and Twitter metadata to the document head.
*
* @param props The current head properties.
*
* @returns Open Graph and Twitter metadata.
*/
export function addArticleMetadata({
url,
head,
}: DocumentHeadProps): DocumentHeadValue {
// Get description from metadata
const description =
head.meta.find((item) => item.name === 'description')?.content || '';

// Create Open Graph image URL
const imageUrl = `${url.origin}/og-image?title=${encodeURIComponent(
head.title
)}&description=${encodeURIComponent(description)}&path=${
url.pathname.split('/')[1]
}`;

// Return Open Graph and Twitter metadata
return {
meta: [
// Open Graph
{
name: 'og:type',
content: 'article',
},
{
name: 'og:url',
content: url.href,
},
{
name: 'og:title',
content: head.title,
},
{
name: 'og:description',
content: description,
},
{
name: 'og:image',
content: imageUrl,
},

// Twitter
{
name: 'twitter:card',
content: 'summary_large_image',
},
{
name: 'twitter:title',
content: head.title,
},
{
name: 'twitter:description',
content: description,
},
{
name: 'twitter:image',
content: imageUrl,
},
],
};
}
1 change: 1 addition & 0 deletions website/src/utils/index.ts
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
export * from './disableTransitions';
export * from './addArticleMetadata';
export * from './trackEvent';

0 comments on commit 141d0be

Please sign in to comment.