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

v3 - migrate default seo #643

Merged
merged 3 commits into from
Sep 16, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
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: 7 additions & 4 deletions src/utils/seo.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ import { urlFor } from "studio/lib/image";
import { COMPANY_INFO_QUERY } from "studio/lib/queries/companyDetails";
import { loadQuery } from "studio/lib/store";
import { PortableTextBlock } from "src/components/richText/RichText";
import { FALLBACK_SEO_QUERY } from "../../studio/lib/queries/seo";
import { SeoFallback } from "../../studio/lib/payloads/seoFallback";

type SeoData = {
title: string;
Expand All @@ -26,7 +28,6 @@ type CompanyInfo = {
brandAssets: {
favicon: string;
};
defaultSEO: SeoData;
};

export const OPEN_GRAPH_IMAGE_DIMENSIONS = {
Expand Down Expand Up @@ -84,14 +85,16 @@ export async function fetchCompanyInfo(): Promise<CompanyInfo | null> {
export async function generateMetadataFromSeo(
seo: SeoData | null,
): Promise<Metadata> {
const { data: fallbackSeo } =
await loadQuery<SeoFallback>(FALLBACK_SEO_QUERY);
const companyInfo = await fetchCompanyInfo();

const title =
seo?.title ||
companyInfo?.defaultSEO?.title ||
fallbackSeo?.seo?.seoTitle ||
companyInfo?.siteMetadata?.siteName ||
"Variant";
const description = seo?.description || companyInfo?.defaultSEO?.description;
const description = seo?.description || fallbackSeo.seo?.seoDescription;
const keywords = seo?.keywords || "";

const favicon = companyInfo?.brandAssets?.favicon;
Expand All @@ -105,7 +108,7 @@ export async function generateMetadataFromSeo(
title: title,
...(description ? { description: description } : {}),
})}`;
const sanityImageUrl = seo?.imageUrl || companyInfo?.defaultSEO?.imageUrl;
const sanityImageUrl = seo?.imageUrl || fallbackSeo.seo?.seoImageUrl;
const sanityImageParams = `?${new URLSearchParams({
w: OPEN_GRAPH_IMAGE_DIMENSIONS.width.toString(),
h: OPEN_GRAPH_IMAGE_DIMENSIONS.height.toString(),
Expand Down
13 changes: 13 additions & 0 deletions studio/lib/payloads/seoFallback.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
export type SeoFallback = {
_id: string;
_type: "seoFallback";
_createdAt: string;
_updatedAt: string;
_rev: string;
seo?: {
seoTitle?: string;
seoDescription?: string;
seoKeywords?: string;
seoImageUrl?: string;
};
};
6 changes: 0 additions & 6 deletions studio/lib/queries/companyDetails.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,6 @@ import { groq } from "next-sanity";
export const COMPANY_INFO_QUERY = groq`*[_type == "companyInfo"]{
brandAssets,
siteMetadata,
defaultSEO {
"title": seoTitle,
"description": seoDescription,
"keywords": seoKeywords,
"imageUrl": seoImage.asset->url
},
legalPages,
}[0]`;

Expand Down
10 changes: 10 additions & 0 deletions studio/lib/queries/seo.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
import { groq } from "next-sanity";

export const FALLBACK_SEO_QUERY = groq`*[_type == "seoFallback"]{
seo {
seoTitle,
seoDescription,
seoKeywords,
"seoImageUrl": seoImage.asset->url
}
}[0]`;
8 changes: 0 additions & 8 deletions studio/schemas/documents/companyInfo.ts
Original file line number Diff line number Diff line change
Expand Up @@ -79,14 +79,6 @@ const companyInfo = defineType({
}),
],
},
{
name: "defaultSEO",
type: "object",
title: "Default SEO Settings",
description:
"If page-specific SEO settings are not provided, these settings will be applied as default.",
fields: seo.fields,
},
],
preview: {
prepare() {
Expand Down