Upgrade to Pro
— share decks privately, control downloads, hide ads and more …
Speaker Deck
Features
Speaker Deck
PRO
Sign in
Sign up for free
Search
Search
Notion APIと学ぶNext.js
Search
Sponsored
·
Ship Features Fearlessly
Turn features on and off without deploys. Used by thousands of Ruby developers.
→
ak2ie
May 27, 2023
Programming
590
0
Share
Notion APIと学ぶNext.js
「にかくほめる!マウントなしのLT会」での発表内容です。
NotionをCMSとしてブログ作成を例に、Next.js(ver. 13)の機能説明をしました。
ak2ie
May 27, 2023
More Decks by ak2ie
See All by ak2ie
Claude Codeはレガシー移行でどこまで使えるのか?
ak2ie
1
1.6k
SVG完全に理解してグラフ書いてみた
ak2ie
0
60
Go言語CLIツールで生産効率UPした話
ak2ie
0
120
Goではじめるバックエンド開発
ak2ie
0
82
NestJSのはじめ方
ak2ie
0
160
フロントエンドでDDDやってみた
ak2ie
0
86
初心者がシビックテックに参加してみた
ak2ie
0
130
Firebase についてとことん語りたい
ak2ie
0
130
D3.jsでグラフを描いてみた
ak2ie
0
120
Other Decks in Programming
See All in Programming
20年以上続くプロダクトでも使い続けられる静的解析ツールを求めて
matsuo_atsushi
0
150
検索設計から 推論設計への重心移動と Recall-First Retrieval
po3rin
5
1.7k
Agentic Elixir
whatyouhide
0
450
Augmenting AI with the Power of Jakarta EE
ivargrimstad
0
560
ビジネスモデルから紐解く、AI+型駆動開発
hirokiomote
0
380
密結合なバックエンドから TypeScript のコードを生成する
kemuridama
0
240
cloudnative conference 2026 flyle
azihsoyn
1
190
【ディップ|26年新卒研修資料】TDD実装演習
dip_tech
PRO
0
200
1人1案件のプロダクトエンジニア時代に、"プロセス監督"としてチャレンジしたこと
non0113
0
120
When benchmarks go bad - what I learned from measuring performance wrong
hollycummins
0
390
自動レビューエンジンの実装と運用 ~レビューのない世界へ~
kurukuru1999
0
100
決定論 vs 確率論:Gemini 3 FlashとTF-IDFを組み合わせた「法規判定エンジン」の構築
shukob
0
170
Featured
See All Featured
Jess Joyce - The Pitfalls of Following Frameworks
techseoconnect
PRO
1
150
Groundhog Day: Seeking Process in Gaming for Health
codingconduct
0
180
Tell your own story through comics
letsgokoyo
1
920
A Modern Web Designer's Workflow
chriscoyier
698
190k
The Straight Up "How To Draw Better" Workshop
denniskardys
239
140k
Skip the Path - Find Your Career Trail
mkilby
1
120
DBのスキルで生き残る技術 - AI時代におけるテーブル設計の勘所
soudai
PRO
65
54k
Public Speaking Without Barfing On Your Shoes - THAT 2023
reverentgeek
1
390
How to make the Groovebox
asonas
2
2.2k
Navigating Weather and Climate Data
rabernat
0
190
The Impact of AI in SEO - AI Overviews June 2024 Edition
aleyda
5
1.1k
The Web Performance Landscape in 2024 [PerfNow 2024]
tammyeverts
12
1.1k
Transcript
Notion APIと学ぶNext.js 2023/05/27
自己紹介 • 名前:ak2ie • 職業:Webエンジニア • 好きなもの:コーヒー
目次 1. Next.jsとは? 2. ページの作り方 a. URLパラメータ、ローディング、リンク 3. fetch API
a. データ取得 b. 再検証
Next.js • Reactをベースにしたフロントエンドフレームワーク • ルーティングが簡単 • スタイル設定もできる • ver. 13での動作を説明します
ブログ • 記事 ◦ Notionで書く ◦ URLは/articles/****で自由に設定可能 • 固定ページもある
Notion
App Router • ページはapp内にディレクトリを作り、 page.tsxを置く • https://example.com/articles • ver. 12以前はpagesディレクトリを使っていた
app page.tsx articles
URLパラメータの取得 • ディレクトリ名を”[“, “]”で囲む • propsからパラメータを取得できる https://example.com/articles/title const Articles =
async ({ params: { slug } }: Props) => { return <p>slug</p> } app page.tsx articles [slug]
ローディング • page.tsxと同じディレクトリに ”loading.tsx”を置くだけ • 読み込み中だけ表示される app/articles/loading.tsx export default function
Loading() { return <p className="p-2">Loading...</p> } app page.tsx articles loading.tsx
Not Found • app/not-found.tsxに書く export default function NotFound() { return
<p className="p-2">ページが存在しません</p> } • notFound()を呼んで、リダイレクトもできる import { notFound } from "next/navigation"; notFound(); app not-found.tsx
リンク • Next.js 13より、<Link>内に<a>タグは不要 ver. 13 <Link href="/about">About</Link> ver. 12以前
<Link href="/about"><a>About</a></Link>
データ取得 • fetch APIで取得できる • JavaScriptのAPIをNext.js が独自に拡張 const response =
await fetch( `https://example.com`, { headers: { Authorization: `Bearer ABC`, "Content-Type": "application/json", "Notion-Version": "2022-06-28", }, } );
Notion データベース • Notion内でデータを管理 する方法 • Viewを切り替えること で、見え方を変えられる
Notion Page • Notion内でテキストや画像をまとめる ところ • リンクやコードも書けて、総称して「ブ ロック」と呼ばれる ブロック ブロック
Notion API • Notion データベースやブロックを取得可能 ブロックを取得するコード例 const response = await
fetch( `https://api.notion.com/v1/blocks/ ${pageId}/children` , { headers: { Authorization: `Bearer ${process.env.NOTION_SECRET !}`, "Content-Type" : "application/json" , "Notion-Version" : "2022-06-28" , }, } );
再検証(revalidate) • fetchしたデータは、指定した時間まで キャッシュされる • 時間経過後のアクセス時に再取得される アクセス 指定時間後までキャッシュ キャッシュ アクセス
データ再取得 時間 キャッシュを返 す await fetch( next: { revalidate: 30, },
Server/Client Component • サーバーとクライアント(ブラウザ)どちらでレンダリングするか • サーバーの方が高速 • Server Component ◦
基本的にこちら • Client Component ◦ クリックイベントなどブラウザでしか扱えないもの
Next.jsまとめ • appディレクトリにページを作る ◦ ディレクトリを作って、page.tsxを置く ◦ app/[slug]/page.tsx => propsから取得可 ◦
app/not-found.tsx: Not Foundページ • fetch API ◦ JavaScript APIをNext.jsが拡張 ◦ revalidateで指定時間キャッシュしてくれる