forked from caching-tools/next-shared-cache
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcreate-pages-get-static-props.ts
42 lines (33 loc) · 1.22 KB
/
create-pages-get-static-props.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
import type { GetStaticPropsContext, GetStaticPropsResult } from 'next';
import type { CountBackendApiResponseJson, PageProps } from './types';
const revalidate = 5;
export function createPagesGetStaticProps(
path: string,
): ({ params }: GetStaticPropsContext) => Promise<GetStaticPropsResult<PageProps>> {
return async function getStaticProps({ params }: GetStaticPropsContext): Promise<GetStaticPropsResult<PageProps>> {
if (!params) {
throw new Error('no params');
}
const { slug } = params;
if (!slug || Array.isArray(slug)) {
throw new Error('no slug');
}
const pathAndTag = `/count/${path}/${slug}`;
const url = new URL(pathAndTag, 'http://localhost:8081');
const result = await fetch(url);
if (!result.ok) {
return { notFound: true, revalidate };
}
const parsedResult = (await result.json()) as CountBackendApiResponseJson;
//
return {
props: {
count: parsedResult.count,
time: parsedResult.unixTimeMs,
revalidateAfter: revalidate * 1000,
path,
},
revalidate,
};
};
}