Skip to content
This repository was archived by the owner on Jan 30, 2025. It is now read-only.

Commit 802109f

Browse files
committed
Use SSG for chapters, groups, posts, and topics
1 parent 8dd2635 commit 802109f

File tree

14 files changed

+161
-63
lines changed

14 files changed

+161
-63
lines changed
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
11
export * from './addToActivity';
2+
export * from './rebuildPage';
23
export * from './updateParent';
34
export * from './updatePosts';
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
import * as functions from 'firebase-functions';
2+
import fetch from 'node-fetch';
3+
import { Chapter } from '@zoonk/models';
4+
5+
/**
6+
* Call a chapter page in the frontend to rebuild it (SSG).
7+
*/
8+
export const onUpdateChapterRebuildPage = functions.firestore
9+
.document('chapters/{id}')
10+
.onUpdate((change) => {
11+
const { language } = change.after.data() as Chapter.Response;
12+
const url = `https://${language}.zoonk.org/chapters/${change.after.id}`;
13+
return fetch(url);
14+
});
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
11
export * from './addToActivity';
2+
export * from './rebuildPage';
23
export * from './updateMyGroups';
34
export * from './updatePosts';
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
import * as functions from 'firebase-functions';
2+
import fetch from 'node-fetch';
3+
import { Group } from '@zoonk/models';
4+
5+
/**
6+
* Call a group page in the frontend to rebuild it (SSG).
7+
*/
8+
export const onUpdateGroupRebuildPage = functions.firestore
9+
.document('groups/{id}')
10+
.onUpdate((change) => {
11+
const { language } = change.after.data() as Group.Response;
12+
const url = `https://${language}.zoonk.org/groups/${change.after.id}`;
13+
return fetch(url);
14+
});
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,3 @@
11
export * from './addToActivity';
2+
export * from './rebuildPage';
23
export * from './updateGroup';
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
import * as functions from 'firebase-functions';
2+
import fetch from 'node-fetch';
3+
import { Post } from '@zoonk/models';
4+
5+
/**
6+
* Call a post page in the frontend to rebuild it (SSG).
7+
*/
8+
export const onUpdatePostRebuildPage = functions.firestore
9+
.document('posts/{id}')
10+
.onUpdate((change) => {
11+
const { language } = change.after.data() as Post.Response;
12+
const url = `https://${language}.zoonk.org/posts/${change.after.id}`;
13+
return fetch(url);
14+
});
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,3 @@
11
export * from './addToActivity';
2+
export * from './rebuildPage';
23
export * from './updateMyTopics';
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
import * as functions from 'firebase-functions';
2+
import fetch from 'node-fetch';
3+
import { Topic } from '@zoonk/models';
4+
5+
/**
6+
* Call a topic page in the frontend to rebuild it (SSG).
7+
*/
8+
export const onUpdateTopicRebuildPage = functions.firestore
9+
.document('topics/{id}')
10+
.onUpdate((change) => {
11+
const { language } = change.after.data() as Topic.Response;
12+
const url = `https://${language}.zoonk.org/topics/${change.after.id}`;
13+
return fetch(url);
14+
});

src/pages/chapters/[id]/index.tsx

Lines changed: 20 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
1-
import { NextPage } from 'next';
1+
import { GetStaticPaths, GetStaticProps, InferGetStaticPropsType } from 'next';
22
import Error from 'next/error';
3-
import { Container, Grid } from '@material-ui/core';
3+
import { useRouter } from 'next/router';
4+
import { CircularProgress, Container, Grid } from '@material-ui/core';
45
import ChapterDetails from '@zoonk/components/ChapterDetails';
56
import ChapterNav from '@zoonk/components/ChapterNav';
67
import LessonsCard from '@zoonk/components/LessonsCard';
@@ -9,18 +10,33 @@ import TopicsBreadcrumb from '@zoonk/components/TopicsBreadcrumb';
910
import useChapterProgress from '@zoonk/components/useChapterProgress';
1011
import { Chapter } from '@zoonk/models';
1112
import { getChapter } from '@zoonk/services';
12-
import { preRender } from '@zoonk/utils';
1313

1414
interface ChapterProps {
1515
data: Chapter.Get | null;
1616
}
1717

18-
const ChapterPage: NextPage<ChapterProps> = ({ data }) => {
18+
export const getStaticPaths: GetStaticPaths = async () => {
19+
return { paths: [], fallback: true };
20+
};
21+
22+
export const getStaticProps: GetStaticProps<ChapterProps> = async ({
23+
params,
24+
}) => {
25+
const id = String(params?.id);
26+
const data = await getChapter(id);
27+
return { props: { data }, revalidate: 1 };
28+
};
29+
30+
const ChapterPage = ({
31+
data,
32+
}: InferGetStaticPropsType<typeof getStaticProps>) => {
33+
const { isFallback } = useRouter();
1934
const { completed, progress } = useChapterProgress({
2035
chapter: data,
2136
chapterId: data?.id,
2237
});
2338

39+
if (!data && isFallback) return <CircularProgress />;
2440
if (!data) return <Error statusCode={404} />;
2541

2642
const {
@@ -73,11 +89,4 @@ const ChapterPage: NextPage<ChapterProps> = ({ data }) => {
7389
);
7490
};
7591

76-
ChapterPage.getInitialProps = async ({ query }) => {
77-
const id = String(query.id);
78-
const data = await getChapter(id);
79-
preRender();
80-
return { data };
81-
};
82-
8392
export default ChapterPage;

src/pages/groups/[id]/index.tsx

Lines changed: 28 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,16 @@
1-
import { NextPage } from 'next';
1+
import { GetStaticPaths, GetStaticProps, InferGetStaticPropsType } from 'next';
22
import Error from 'next/error';
3-
import { Container } from '@material-ui/core';
3+
import { useRouter } from 'next/router';
4+
import { CircularProgress, Container } from '@material-ui/core';
45
import GroupBase from '@zoonk/components/GroupBase';
56
import GroupsBreadcrumb from '@zoonk/components/GroupsBreadcrumb';
67
import GroupPinned from '@zoonk/components/GroupPinned';
78
import Meta from '@zoonk/components/Meta';
89
import PostShare from '@zoonk/components/PostShare';
910
import PostsList from '@zoonk/components/PostsList';
1011
import { Group, Post } from '@zoonk/models';
11-
import { getGroup, getPosts } from '@zoonk/services';
12-
import { appLanguage, preRender } from '@zoonk/utils';
12+
import { getGroup, getGroups, getPosts } from '@zoonk/services';
13+
import { appLanguage } from '@zoonk/utils';
1314

1415
interface GroupPageProps {
1516
group: Group.Get | null;
@@ -18,7 +19,29 @@ interface GroupPageProps {
1819

1920
const limit = 10;
2021

21-
const GroupPage: NextPage<GroupPageProps> = ({ group, posts }) => {
22+
export const getStaticPaths: GetStaticPaths = async () => {
23+
const groups = await getGroups({ limit: 20 });
24+
const paths = groups.map((group) => ({ params: { id: group.id } }));
25+
return { paths, fallback: true };
26+
};
27+
28+
export const getStaticProps: GetStaticProps<GroupPageProps> = async ({
29+
params,
30+
}) => {
31+
const groupId = String(params?.id);
32+
const groupReq = getGroup(groupId);
33+
const postsReq = getPosts({ groupId, limit });
34+
const [group, posts] = await Promise.all([groupReq, postsReq]);
35+
return { props: { group, posts }, revalidate: 1 };
36+
};
37+
38+
const GroupPage = ({
39+
group,
40+
posts,
41+
}: InferGetStaticPropsType<typeof getStaticProps>) => {
42+
const { isFallback } = useRouter();
43+
44+
if (!group && isFallback) return <CircularProgress />;
2245
if (!group) return <Error statusCode={404} />;
2346

2447
const { description, id, language, photo, title, topics } = group;
@@ -43,13 +66,4 @@ const GroupPage: NextPage<GroupPageProps> = ({ group, posts }) => {
4366
);
4467
};
4568

46-
GroupPage.getInitialProps = async ({ query }) => {
47-
const groupId = String(query.id);
48-
const groupReq = getGroup(groupId);
49-
const postsReq = getPosts({ groupId, limit });
50-
const [group, posts] = await Promise.all([groupReq, postsReq]);
51-
preRender();
52-
return { group, posts };
53-
};
54-
5569
export default GroupPage;

0 commit comments

Comments
 (0)