Skip to content

Commit 18e8714

Browse files
committed
fix: use client.getPages intead of client.getPageSnapshot
1 parent 31f6cfe commit 18e8714

File tree

2 files changed

+66
-5
lines changed

2 files changed

+66
-5
lines changed

core/lib/makeswift/api.ts

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
import { getSiteVersion } from '@makeswift/runtime/next/server';
2+
import { strict } from 'assert';
3+
import { z } from 'zod';
4+
5+
import { defaultLocale } from '~/i18n/locales';
6+
7+
strict(process.env.MAKESWIFT_SITE_API_KEY, 'MAKESWIFT_SITE_API_KEY is required');
8+
9+
const MAKESWIFT_SITE_API_KEY = process.env.MAKESWIFT_SITE_API_KEY;
10+
11+
const MAKESWIFT_API_ORIGIN =
12+
process.env.NEXT_PUBLIC_MAKESWIFT_API_ORIGIN ??
13+
process.env.MAKESWIFT_API_ORIGIN ??
14+
'https://api.makeswift.com';
15+
16+
const MakeswiftPageResponseSchema = z.object({
17+
title: z.string().nullable(),
18+
description: z.string().nullable(),
19+
});
20+
21+
function normalizeLocale(locale: string): string | undefined {
22+
return locale === defaultLocale ? undefined : locale;
23+
}
24+
25+
export async function getMakeswiftPageMetadata({ path, locale }: { path: string; locale: string }) {
26+
const siteVersion = await getSiteVersion();
27+
28+
const versionRef = siteVersion?.version === 'Live' ? 'ref:live' : 'ref:draft';
29+
const normalizedLocale = normalizeLocale(locale);
30+
31+
const url = new URL(`/v6/pages${path}`, MAKESWIFT_API_ORIGIN);
32+
33+
url.searchParams.set('versionRef', versionRef);
34+
url.searchParams.set('siteId', 'MISSING SITE ID');
35+
36+
if (normalizedLocale) {
37+
url.searchParams.set('locale', normalizedLocale);
38+
}
39+
40+
const response = await fetch(url, {
41+
headers: {
42+
'x-api-key': MAKESWIFT_SITE_API_KEY,
43+
},
44+
});
45+
46+
if (!response.ok) {
47+
return null;
48+
}
49+
50+
const data: unknown = await response.json();
51+
const page = MakeswiftPageResponseSchema.parse(data);
52+
53+
return {
54+
...(page.title && { title: page.title }),
55+
...(page.description && { description: page.description }),
56+
};
57+
}

core/lib/makeswift/client.ts

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -34,16 +34,20 @@ function normalizeLocale(locale: string): string | undefined {
3434
}
3535

3636
export async function getMakeswiftPageMetadata({ path, locale }: { path: string; locale: string }) {
37-
const snapshot = await getPageSnapshot({ path, locale });
37+
const { data: pages } = await client.getPages({
38+
pathPrefix: path,
39+
locale: normalizeLocale(locale),
40+
siteVersion: await getSiteVersion(),
41+
});
3842

39-
if (snapshot == null) {
43+
if (pages.length === 0 || !pages[0]) {
4044
return null;
4145
}
4246

43-
const { meta } = snapshot.document;
47+
const { title, description } = pages[0];
4448

4549
return {
46-
...(meta.title && { title: meta.title }),
47-
...(meta.description && { description: meta.description }),
50+
...(title && { title }),
51+
...(description && { description }),
4852
};
4953
}

0 commit comments

Comments
 (0)