Skip to content
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
67 changes: 67 additions & 0 deletions packages/webapp/pages/sources/[source].tsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import type {
GetStaticPropsContext,
GetStaticPropsResult,
} from 'next';
import Head from 'next/head';
import type { ParsedUrlQuery } from 'querystring';
import type { ReactElement } from 'react';
import React, { useContext, useMemo } from 'react';
Expand Down Expand Up @@ -62,6 +63,9 @@ import { mainFeedLayoutProps } from '../../components/layouts/MainFeedPage';
import { getLayout } from '../../components/layouts/FeedLayout';
import { SourceActions } from '../../../shared/src/components/sources/SourceActions';
import type { DynamicSeoProps } from '../../components/common';
import { getAppOrigin } from '../../lib/seo';

const appOrigin = getAppOrigin();

interface SourcePageProps extends DynamicSeoProps {
source?: Source;
Expand Down Expand Up @@ -122,6 +126,61 @@ const SimilarSources = ({ sourceId }: SourceIdProps) => {
);
};

const getSourcePageJsonLd = (source: Source): string => {
const sourceHandle = source.handle || source.id;
const sourcePageUrl = `${appOrigin}/sources/${encodeURIComponent(
sourceHandle,
)}`;
const sourceUrl = source.permalink || sourcePageUrl;

return JSON.stringify({
'@context': 'https://schema.org',
'@graph': [
{
'@type': 'Organization',
'@id': `${sourcePageUrl}#organization`,
name: source.name,
url: sourceUrl,
...(source.image && {
logo: { '@type': 'ImageObject', url: source.image },
}),
...(source.description && { description: source.description }),
},
{
'@type': 'CollectionPage',
'@id': `${sourcePageUrl}#page`,
url: sourcePageUrl,
name: `${source.name} posts on daily.dev`,
...(source.description && { description: source.description }),
about: { '@id': `${sourcePageUrl}#organization` },
isPartOf: { '@type': 'WebSite', url: appOrigin },
},
{
'@type': 'BreadcrumbList',
itemListElement: [
{
'@type': 'ListItem',
position: 1,
name: 'Home',
item: appOrigin,
},
{
'@type': 'ListItem',
position: 2,
name: 'Sources',
item: `${appOrigin}/sources`,
},
{
'@type': 'ListItem',
position: 3,
name: source.name,
},
],
},
],
});
};

const SourcePage = ({ source }: SourcePageProps): ReactElement => {
const isLaptop = useViewSize(ViewSize.Laptop);
const { shouldShowAuthBanner } = useOnboardingActions();
Expand Down Expand Up @@ -161,8 +220,16 @@ const SourcePage = ({ source }: SourcePageProps): ReactElement => {
return <Custom404 />;
}

const jsonLd = getSourcePageJsonLd(source);

return (
<FeedPageLayoutComponent className="overflow-x-hidden">
<Head>
<script
type="application/ld+json"
dangerouslySetInnerHTML={{ __html: jsonLd }}
/>
</Head>
<PageInfoHeader
className={shouldUseListFeedLayout ? 'mx-4 !w-auto' : undefined}
>
Expand Down
83 changes: 83 additions & 0 deletions packages/webapp/pages/tags/[tag].tsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import type {
GetStaticPropsContext,
GetStaticPropsResult,
} from 'next';
import Head from 'next/head';
import type { ParsedUrlQuery } from 'querystring';
import type { ReactElement } from 'react';
import React, { useContext, useMemo } from 'react';
Expand Down Expand Up @@ -72,6 +73,9 @@ import { getLayout } from '../../components/layouts/FeedLayout';
import { mainFeedLayoutProps } from '../../components/layouts/MainFeedPage';
import type { DynamicSeoProps } from '../../components/common';
import { defaultOpenGraph, defaultSeo } from '../../next-seo';
import { getAppOrigin } from '../../lib/seo';

const appOrigin = getAppOrigin();

interface TagPageProps extends DynamicSeoProps {
tag: string;
Expand Down Expand Up @@ -161,6 +165,74 @@ const TagTopSources = ({ tag }: { tag: string }) => {
);
};

const getTagPageJsonLd = ({
tag,
initialData,
topPosts,
}: {
tag: string;
initialData: Keyword;
topPosts: TagTopPost[];
}): string => {
const encodedTag = encodeURIComponent(tag);
const tagTitle = initialData.flags?.title || tag;
const tagDescription =
initialData.flags?.description ||
`Find all the recent posts, videos, updates and discussions about ${tagTitle}`;
const tagUrl = `${appOrigin}/tags/${encodedTag}`;

return JSON.stringify({
'@context': 'https://schema.org',
'@graph': [
{
'@type': 'CollectionPage',
'@id': `${tagUrl}#page`,
url: tagUrl,
name: `${tagTitle} posts on daily.dev`,
description: tagDescription,
isPartOf: { '@type': 'WebSite', url: appOrigin },
},
...(topPosts.length
? [
{
'@type': 'ItemList',
'@id': `${tagUrl}#items`,
numberOfItems: topPosts.length,
itemListElement: topPosts.map((post, index) => ({
'@type': 'ListItem',
position: index + 1,
url: `${appOrigin}/posts/${post.slug || post.id}`,
name: post.title || '',
})),
},
]
: []),
{
'@type': 'BreadcrumbList',
itemListElement: [
{
'@type': 'ListItem',
position: 1,
name: 'Home',
item: appOrigin,
},
{
'@type': 'ListItem',
position: 2,
name: 'Tags',
item: `${appOrigin}/tags`,
},
{
'@type': 'ListItem',
position: 3,
name: tagTitle,
},
],
},
],
});
};

const TagPage = ({
tag,
initialData,
Expand Down Expand Up @@ -207,6 +279,9 @@ const TagPage = ({
const { onFollowTags, onUnfollowTags, onBlockTags, onUnblockTags } =
useTagAndSource({ origin: Origin.TagPage });
const title = initialData?.flags?.title || tag;
const jsonLd = initialData
? getTagPageJsonLd({ tag, initialData, topPosts })
: null;
const { follow, unfollow } = useContentPreference({
showToastOnSuccess: false,
});
Expand Down Expand Up @@ -265,6 +340,14 @@ const TagPage = ({

return (
<FeedPageLayoutComponent>
{jsonLd && (
<Head>
<script
type="application/ld+json"
dangerouslySetInnerHTML={{ __html: jsonLd }}
/>
</Head>
)}
<PageInfoHeader className="mx-4 !w-auto">
<div className="flex items-center font-bold">
<HashtagIcon size={IconSize.XXLarge} />
Expand Down