Skip to content

Commit 976a54c

Browse files
authored
feat: OG image from the CMS (#49)
1 parent bb65c88 commit 976a54c

7 files changed

Lines changed: 40 additions & 10 deletions

File tree

app/composables/useSeoHead.ts

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,10 @@
1-
export const useSeoHead = (title: string | undefined) => {
2-
const url = useRequestURL();
3-
1+
export const useSeoHead = (title: string | undefined, image?: string) => {
42
useHead({
53
title,
64
meta: [
75
{
86
property: "og:image",
9-
content: `${url.origin}/img/bench-og.jpg`,
7+
content: image,
108
},
119
{
1210
property: "og:title",

app/pages/login.vue

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,11 @@ definePageMeta({
3535
layout: "minimal",
3636
});
3737
38-
useSeoHead("Login");
38+
const { data } = await useFetch("/api/cms/seo");
39+
useSeoHead(
40+
data.value?.title ?? undefined,
41+
data.value?.ogImage?.url ?? undefined,
42+
);
3943
4044
const { loggedIn, fetch: refreshSession } = useUserSession();
4145
const route = useRoute();

nuxt.config.ts

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -12,11 +12,7 @@ export default defineNuxtConfig({
1212
routeRules: {
1313
"/admin": { redirect: "/admin/dashboard" },
1414
"/admin/login": { prerender: true },
15-
"/login": {
16-
isr: true,
17-
// required to get correct hostname in meta tags
18-
cache: { varies: ["host", "x-forwarded-host"] },
19-
},
15+
"/login": { isr: 3600 },
2016
},
2117
nitro: {
2218
prerender: {

public/img/bench-og.jpg

-38.2 KB
Binary file not shown.

server/api/cms/seo.get.ts

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
import { fetchContentful } from "~~/server/service/contentful";
2+
import type { SeoQuery, SeoQueryVariables } from "~~/shared/types/graphql";
3+
import { SEO_QUERY } from "~~/server/graphql/queries/seo.query";
4+
5+
export default defineCachedEventHandler(
6+
async () => {
7+
const data = await fetchContentful<SeoQuery, SeoQueryVariables>(SEO_QUERY);
8+
return data.componentSeo;
9+
},
10+
{ maxAge: 60, swr: false },
11+
);
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
import { gql } from "graphql-request";
2+
3+
export const SEO_FRAGMENT = gql`
4+
fragment SEO on ComponentSeo {
5+
title
6+
ogImage {
7+
url
8+
}
9+
}
10+
`;
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
import { gql } from "graphql-request";
2+
import { SEO_FRAGMENT } from "../fragments/seo.fragment";
3+
4+
export const SEO_QUERY = gql`
5+
query SEO {
6+
componentSeo(id: "1j7SAAi0Di80JVqvW1QT67") {
7+
...SEO
8+
}
9+
}
10+
${SEO_FRAGMENT}
11+
`;

0 commit comments

Comments
 (0)