22sook00 logo
SookDev
🧳

블로그 마이그레이션하기 2 - 노션 API

tag
next.js
productivity
date
Jan 9, 2024

블로그에 노션 API 를 적용하기 위한 많은 시도들.

notion image
notion image

02_Next.js 13 Ver. App router

13 버전에서 변경된 사항들.

generateStaticParams

generateStaticProps → generateStaticParams

Route Groups

URL 경로에 영향을 주지 않고 라우트 구성URL에 영향을 주지 않고 라우트를 구성하려면 그룹을 만들어 관련 라우트를 함께 유지.
괄호 안의 폴더는 URL에서 생략된다.
연동하려는 페이지와 그룹으로 묶어 사용하기 위해서 다음과 같이 App 디렉토리 내부에 폴더를 생성하면 된다.
📦(about) ┣ 📂about ┃ ┗ 📜page.js ┣ 📂contact ┃ ┗ 📜page.js ┗ 📜layout.js
layout.js 에서 about 페이지와 contact 페이지에서만 보여줄 항목만 표현 할 수 있다.
const AboutLayout = ({ children }) => { return ( <main className="w-full flex flex-col items-center justify-between"> <InsightRoll insights={insights} /> {children} </main> ); };

Metadata

정적인 static metadata 세팅과 동적인 dynamic metadata 인 generateMetadata Function 적용 가능
import { Metadata, ResolvingMetadata } from 'next' type Props = { params: { id: string } searchParams: { [key: string]: string | string[] | undefined } } export async function generateMetadata( { params, searchParams }: Props, parent: ResolvingMetadata ): Promise<Metadata> { // read route params const id = params.id // fetch data const product = await fetch(`https://.../${id}`).then((res) => res.json()) // optionally access and extend (rather than replace) parent metadata const previousImages = (await parent).openGraph?.images || [] return { title: product.title, openGraph: { images: ['/some-specific-page-image.jpg', ...previousImages], }, } } export default function Page({ params, searchParams }: Props) {}

 

03_Supabase

View count 를 위한 데이터베이스 스키마 생성
--grant create on schema public to anon create or replace function increment(slug_text text) returns void as $$ declare view_id int; begin select id into view_id from views where slug = slug_text limit 1; if view_id is not null then update views set count = count + 1 where id = view_id; else insert into views (slug,count) values (slug_text, 1); end if; end; $$ language plpgsql
notion image
 
타겟 권한 설정
manage > policies 탭의 New Policy 버튼 클릭
policy name 을 everyone 에서 anon 으로 변경하고 target role 로 anon으로 변경한다.
Enable read access for all anon
그리고 해당 프로젝트에는 auth 인증이 별도로 없으므로
authentication policy 템플릿을 선택하여 anon 으로 변경해준다.
 
notion image
 

03_Meta tag for SEO

seo 최적화 하기

Next.js 에서 제공하는 metadata base
 

 

Next.js App router

생각보다 앱 라우터를 지원하지 않는 경우가 많아 애를 먹었다.
 

Notion API 선정

@notionhq/client

자꾸 token 을 못찾겠다고 징징댐. 그리고 느림

react-notion-x

공식문서를 보면 사용이 쉬워 보이고 @notionhq/client 보다 빨라 금방 사용할 줄 알았지만 의외의 복병으로,
App router 를 지원하지 않는다.
Next.js app 라우터 버전은 더이상 getStaticProps , getStaticPath 등을 지원하지 않는데 아직 react-notion-x 는 getStaticProps 에서만 사용해야 한다.
client 측에서 사용하려면 ‘use client’ 를 사용하여 해결 할 수 있다고는 하나,
SEO 가 중요한 블로그에서 적절한 선택이 아닌것 같아 고민에 빠졌다.
 
Are there plans to make the NotionRenderer compatible with the Next.js App Router?
Updated Feb 3, 2024