Skip to content

Commit

Permalink
feat(seoFallback): migrate from defaultSeo to seoFallback
Browse files Browse the repository at this point in the history
  • Loading branch information
mathiazom committed Sep 13, 2024
1 parent 8ca6ad9 commit 660abd3
Show file tree
Hide file tree
Showing 5 changed files with 30 additions and 18 deletions.
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

0 comments on commit 660abd3

Please sign in to comment.