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

feat: meta tag component #66

Merged
merged 3 commits into from
Apr 14, 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
11 changes: 8 additions & 3 deletions components/Head.tsx
Original file line number Diff line number Diff line change
@@ -1,18 +1,23 @@
// Copyright 2023 the Deno authors. All rights reserved. MIT license.
import { Head as _Head } from "$fresh/runtime.ts";
import { SITE_DESCRIPTION, SITE_NAME } from "@/constants.ts";
import Meta from "@/components/Meta.tsx";

interface HeadProps {
title?: string;
description?: string;
href?: string;
}

export default function Head({ title, description }: HeadProps) {
export default function Head({ title, description, href }: HeadProps) {
return (
<_Head>
<title>{title ?? SITE_NAME}</title>
<Meta
title={title ?? SITE_NAME}
description={description ?? SITE_DESCRIPTION}
href={href}
/>
<link rel="icon" href="/favicon.ico" sizes="32x32" />
<meta name="description" content={description ?? SITE_DESCRIPTION} />
</_Head>
);
}
83 changes: 83 additions & 0 deletions components/Meta.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
// Copyright 2023 the Deno authors. All rights reserved. MIT license.
import { SITE_DESCRIPTION, SITE_NAME } from "@/constants.ts";

interface MetaProps {
title?: string;
description?: string;
imageUrl?: string;
href?: string;
}

export default function Meta(props: MetaProps) {
const { title, description, imageUrl, href } = props;

return (
<>
{/* HTML Meta Tags */}
<title>{title || SITE_NAME}</title>
<meta
name="description"
content={description || SITE_DESCRIPTION}
/>

{/* Google / Search Engine Tags */}
<meta
itemProp="name"
content={title || SITE_NAME}
/>
<meta
itemProp="description"
content={description || SITE_DESCRIPTION}
/>
{imageUrl && (
<meta
itemProp="image"
content={imageUrl}
/>
)}

{/* Facebook Meta Tags */}
<meta property="og:type" content="website" />
<meta property="og:site_name" content={SITE_NAME} />
<meta property="og:locale" content="en" />
<meta property="og:type" content="website" />
<meta
property="og:title"
content={title || SITE_NAME}
/>
<meta
property="og:description"
content={description || SITE_DESCRIPTION}
/>
{href && (
<meta
property="og:url"
content={href}
/>
)}
{imageUrl && (
<meta
property="og:image"
content={imageUrl}
/>
)}

{/* Twitter Meta Tags */}
<meta name="twitter:card" content="summary_large_image" />
<meta
name="twitter:title"
content={title || SITE_NAME}
/>
<meta
name="twitter:description"
content={description || SITE_DESCRIPTION}
/>
{imageUrl && (
<meta
name="twitter:image"
content={imageUrl}
/>
)}
</>
);
}
19 changes: 6 additions & 13 deletions routes/blog/[slug].tsx
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,12 @@ import { Head } from "$fresh/runtime.ts";
import { getPost, Post } from "@/utils/posts.ts";
import Nav from "@/components/Nav.tsx";
import Header from "@/components/Header.tsx";
import { SITE_NAME } from "@/constants.ts";
import {
BlogFooterNavItems,
BlogHeaderNavItems,
} from "@/routes/blog/index.tsx";
import Footer from "@/components/Footer.tsx";
import Meta from "@/components/Meta.tsx";

export const handler: Handlers<Post> = {
async GET(_req, ctx) {
Expand All @@ -33,18 +33,11 @@ export default function PostPage(props: PageProps<Post>) {
<>
<Head>
<style dangerouslySetInnerHTML={{ __html: CSS }} />
<title>{post.title}</title>
<meta property="og:type" content="website" />
<meta property="og:site_name" content={SITE_NAME} />
<meta property="og:locale" content="en" />
<meta property="og:title" content={post.title} />
<meta property="og:description" content={post.summary} />
<meta property="og:url" content={props.url.origin} />
<meta name="twitter:title" content={post.title} />
<meta name="twitter:description" content={post.summary} />
<meta name="description" content={post.summary} />
<meta itemProp="name" content={post.title} />
<meta itemProp="description" content={post.summary} />
<Meta
title={post.title}
description={post.summary}
href={props.url.href}
/>
</Head>
<Header>
<Nav items={BlogHeaderNavItems} />
Expand Down
30 changes: 6 additions & 24 deletions routes/blog/index.tsx
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
// Copyright 2023 the Deno authors. All rights reserved. MIT license.
import { Handlers } from "$fresh/server.ts";
import { PageProps } from "$fresh/server.ts";
import { Head } from "$fresh/runtime.ts";
import Header from "@/components/Header.tsx";
import Nav from "@/components/Nav.tsx";
import PostCard from "@/components/PostCard.tsx";
import { getPosts, Post } from "@/utils/posts.ts";
import { SITE_NAME } from "@/constants.ts";
import Footer from "@/components/Footer.tsx";
import Head from "@/components/Head.tsx";

export const BlogHeaderNavItems = [
{
Expand Down Expand Up @@ -35,29 +35,11 @@ export default function BlogIndexPage(props: PageProps<Post[]>) {
const posts = props.data;
return (
<>
<Head>
<title>Blog</title>
<meta property="og:type" content="website" />
<meta property="og:site_name" content={SITE_NAME} />
<meta property="og:locale" content="en" />
<meta property="og:title" content={`Blog | ${SITE_NAME}`} />
<meta
property="og:description"
content="This is the blog for Deno SaaSKit"
/>
<meta property="og:url" content={props.url.origin} />
<meta name="twitter:title" content={`Blog | ${SITE_NAME}`} />
<meta
name="twitter:description"
content="This is the blog for Deno SaaSKit"
/>
<meta name="description" content="This is the blog for Deno SaaSKit" />
<meta itemProp="name" content={`Blog | ${SITE_NAME}`} />
<meta
itemProp="description"
content="This is the blog for Deno SaaSKit"
/>
</Head>
<Head
title={`Blog | ${SITE_NAME}`}
description="This is the blog for Deno SaaSKit"
href={props.url.href}
/>
<Header>
<Nav items={BlogHeaderNavItems} />
</Header>
Expand Down
2 changes: 1 addition & 1 deletion routes/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -261,7 +261,7 @@ export const handler: Handlers<Stripe.Product[]> = {
export default function HomePage(props: PageProps<Stripe.Product[]>) {
return (
<>
<Head />
<Head href={props.url.href} />
<TopSection />
<FeaturesSection />
<PricingSection products={props.data} />
Expand Down