Skip to content

Commit 6176112

Browse files
authored
Merge pull request #37 from 01-binary/issue#35
refactor: profile image to use Notion URL directly and remove API end…
2 parents 8785e1e + deb9fae commit 6176112

File tree

11 files changed

+56
-47
lines changed

11 files changed

+56
-47
lines changed

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@
1717
"lodash": "^4.17.21",
1818
"next": "^13.4.4",
1919
"notion-to-jsx": "^1.2.8",
20-
"notion-to-utils": "^1.1.0",
20+
"notion-to-utils": "^1.1.1",
2121
"p-map": "^7.0.2",
2222
"plaiceholder": "^3.0.0",
2323
"prismjs": "^1.30.0",

pages/about.tsx

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,24 @@
11
import { GetStaticProps } from 'next';
22
import { NotionBlock, Renderer } from 'notion-to-jsx';
33

4-
import { notionClient } from '@/utils';
4+
import { fetchNotionProfileUrl, notionClient } from '@/utils';
55

66
import PageHead from '@/components/common/PageHead';
77

88
import { REVALIDATE_TIME } from '@/assets/constants';
99

1010
interface Props {
1111
blocks: NotionBlock[];
12+
profileUrl: string;
1213
}
1314

14-
const AboutPage = ({ blocks }: Props) => {
15+
const AboutPage = ({ blocks, profileUrl }: Props) => {
1516
return (
1617
<>
17-
<PageHead title="About" />
18+
<PageHead
19+
title="About"
20+
image={profileUrl}
21+
/>
1822
<Renderer blocks={blocks} />
1923
</>
2024
);
@@ -26,10 +30,11 @@ export const getStaticProps: GetStaticProps<Props> = async () => {
2630
const aboutId = process.env.NOTION_ABOUT_ID;
2731

2832
if (!aboutId) throw new Error('NOTION_ABOUT_ID is not defined');
29-
33+
const profileUrl = await fetchNotionProfileUrl();
3034
const blocks = (await notionClient.getPageBlocks(aboutId)) as NotionBlock[];
3135
return {
3236
props: {
37+
profileUrl,
3338
blocks,
3439
},
3540
revalidate: REVALIDATE_TIME,

pages/api/profile-image.ts

Lines changed: 0 additions & 30 deletions
This file was deleted.

pages/index.tsx

Lines changed: 19 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,28 +3,40 @@ import { GetStaticProps } from 'next';
33

44
import { categoriesAtom } from '@/atoms/categories';
55
import { postsAtom } from '@/atoms/posts';
6+
import { profileImageAtom } from '@/atoms/profile';
67
import Home from '@/features/home';
78
import { Category, PostMeta } from '@/interfaces';
8-
import { getPostsMeta, fetchNotionPostsMeta, getCategories, getBlurImage } from '@/utils';
9+
import {
10+
getPostsMeta,
11+
fetchNotionPostsMeta,
12+
getCategories,
13+
getBlurImage,
14+
fetchNotionProfileUrl,
15+
} from '@/utils';
916
import { siteConfig } from 'site.config';
1017

1118
import PageHead from '@/components/common/PageHead';
1219

1320
import { REVALIDATE_TIME } from '@/assets/constants';
1421

1522
interface Props {
23+
profileUrl: string;
1624
posts: PostMeta[];
1725
categories: Category[];
1826
}
19-
const HomePage = ({ posts, categories }: Props) => {
27+
const HomePage = ({ profileUrl, posts, categories }: Props) => {
2028
useHydrateAtoms([
29+
[profileImageAtom, profileUrl],
2130
[postsAtom, posts],
2231
[categoriesAtom, categories],
2332
]);
2433

2534
return (
2635
<>
27-
<PageHead title={siteConfig.homeTitle} />
36+
<PageHead
37+
title={siteConfig.homeTitle}
38+
image={profileUrl}
39+
/>
2840
<Home />
2941
</>
3042
);
@@ -36,6 +48,9 @@ export const getStaticProps: GetStaticProps<Props> = async () => {
3648
if (!process.env.NOTION_POST_DATABASE_ID)
3749
throw new Error('NOTION_POST_DATABASE_ID is not defined');
3850

51+
if (!process.env.NOTION_PROFILE_ID) throw new Error('NOTION_PROFILE_ID is not defined');
52+
53+
const profileUrl = await fetchNotionProfileUrl();
3954
const notionPostsResponse = await fetchNotionPostsMeta(process.env.NOTION_POST_DATABASE_ID);
4055
const posts = await Promise.all(
4156
getPostsMeta(notionPostsResponse).map(async (post) => ({
@@ -47,6 +62,7 @@ export const getStaticProps: GetStaticProps<Props> = async () => {
4762

4863
return {
4964
props: {
65+
profileUrl,
5066
posts,
5167
categories,
5268
},

src/assets/constants.ts

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,6 @@ export const INITIAL_CATEGORY = 'All';
1818
export const DEFAULT_CATEGORY_COLOR: SelectColor = 'default';
1919

2020
export const REVALIDATE_TIME = 60;
21-
export const IMAGE_MAX_AGE = 60 * 60;
2221

2322
export const DEFAULT_BLUR_BASE64 =
2423
'data:image/svg+xml;base64,PHN2ZyB4bWxucz0iaHR0cDovL3d3dy53My5vcmcvMjAwMC9zdmciIHdpZHRoPSIxIiBoZWlnaHQ9IjEiPjxyZWN0IHdpZHRoPSIxIiBoZWlnaHQ9IjEiIGZpbGw9IiNGM0Y0RjYiLz48L3N2Zz4=';

src/atoms/profile/index.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
import { atom } from 'jotai';
2+
3+
export const profileImageAtom = atom<string>('');

src/components/common/PageHead.tsx

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,10 +16,11 @@ interface PageHeadProps {
1616

1717
const PageHead = ({ title, description, image, keywords }: PageHeadProps) => {
1818
const { asPath } = useRouter();
19+
1920
const pageTitle = title ? `${title} | ${siteConfig.blogName}` : siteConfig.blogName;
2021
const pageDescription = description || siteConfig.seoDefaultDesc;
2122
const pageKeywords = keywords || '';
22-
const pageImage = `${image || `${siteConfig.url}/api/profile-image`}`;
23+
const pageImage = `${image}`;
2324
const pageUrl = `${siteConfig.url}${asPath}`;
2425

2526
return (

src/features/home/Intro/index.tsx

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,25 @@
11
import { memo } from 'react';
22

3+
import { useAtomValue } from 'jotai';
34
import Image from 'next/image';
45
import Link from 'next/link';
56
import { AiOutlineGithub, AiFillLinkedin } from 'react-icons/ai';
67
import { HiMail } from 'react-icons/hi';
78

9+
import { profileImageAtom } from '@/atoms/profile';
810
import { siteConfig } from 'site.config';
911

1012
import { DEFAULT_BLUR_BASE64 } from '@/assets/constants';
1113

12-
const profileImage = `/api/profile-image`;
13-
1414
const Intro = () => {
15+
const profileUrl = useAtomValue(profileImageAtom);
16+
1517
return (
1618
<section>
1719
<article className="flex gap-4 md:gap-6">
1820
<Image
1921
className="h-[110px] w-[110px] rounded-full object-cover"
20-
src={profileImage}
22+
src={profileUrl}
2123
width={110}
2224
height={110}
2325
alt={'Intro Picture'}

src/utils/fetchNotionProfileUrl.ts

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
import { formatNotionImageUrl } from 'notion-to-utils';
2+
3+
import notionClient from '@/utils/notionClient';
4+
5+
const fetchNotionProfileUrl = async () => {
6+
if (!process.env.NOTION_PROFILE_ID) throw new Error('NOTION_PROFILE_ID is not defined');
7+
const url = await notionClient.getFileUrl(process.env.NOTION_PROFILE_ID, 'media');
8+
const formattedUrl = formatNotionImageUrl(url, process.env.NOTION_PROFILE_ID);
9+
return formattedUrl;
10+
};
11+
12+
export default fetchNotionProfileUrl;

src/utils/index.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,3 +5,4 @@ export { default as getCategories } from './getCategories';
55
export { default as getSlugs } from './getSlugs';
66
export { default as getPostsMeta } from './getPostsMeta';
77
export { default as getBlurImage } from './getBlurImage';
8+
export { default as fetchNotionProfileUrl } from './fetchNotionProfileUrl';

0 commit comments

Comments
 (0)