Skip to content

Commit 6a5c0a9

Browse files
committed
fix: harden Notion fetch for Vercel builds
Avoid failing the whole production build on Notion 429s by returning null blocks, empty static paths for subpages, and safer slug path generation.
1 parent a6542e4 commit 6a5c0a9

3 files changed

Lines changed: 27 additions & 39 deletions

File tree

‎lib/notion/getPostBlocks.js‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,10 +14,11 @@ export async function getPostBlocks(id) {
1414
}
1515
return pageBlock
1616
} catch (error) {
17+
// Don't fail the whole Next build on Notion rate limits / transient errors.
1718
console.error(`Failed to get page blocks for ${id}:`, error.message)
1819
if (error.response?.statusCode === 400) {
1920
console.error('This might be due to missing environment variables or incorrect Notion page ID')
2021
}
21-
throw error
22+
return null
2223
}
2324
}

‎pages/[slug].js‎

Lines changed: 18 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -21,19 +21,31 @@ const Post = ({ post, blockMap }) => {
2121
}
2222

2323
export async function getStaticPaths() {
24-
const posts = await getAllPosts({ onlyNewsletter: false })
25-
return {
26-
paths: posts.map((row) => `${BLOG.path}/${row.slug}`),
27-
fallback: true
24+
try {
25+
const posts = await getAllPosts({ onlyNewsletter: false })
26+
return {
27+
paths: (posts || []).map((row) => `${BLOG.path}/${row.slug}`),
28+
fallback: true
29+
}
30+
} catch (err) {
31+
console.error('getStaticPaths failed:', err)
32+
return { paths: [], fallback: true }
2833
}
2934
}
3035

3136
export async function getStaticProps({ params: { slug } }) {
3237
const posts = await getAllPosts({ onlyNewsletter: false })
3338
const post = posts.find((t) => t.slug === slug)
3439

40+
if (!post?.id) {
41+
return { props: { post: null, blockMap: null }, revalidate: 60 }
42+
}
43+
3544
try {
3645
const blockMap = await getPostBlocks(post.id)
46+
if (!blockMap) {
47+
return { props: { post, blockMap: null }, revalidate: 60 }
48+
}
3749
return {
3850
props: {
3951
post,
@@ -47,7 +59,8 @@ export async function getStaticProps({ params: { slug } }) {
4759
props: {
4860
post: null,
4961
blockMap: null
50-
}
62+
},
63+
revalidate: 60
5164
}
5265
}
5366
}

‎pages/s/[subpage].js‎

Lines changed: 7 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -25,40 +25,11 @@ const Post = ({ post, blockMap }) => {
2525
}
2626

2727
export async function getStaticPaths() {
28-
const mapPageUrl = defaultMapPageUrl(BLOG.notionPageId)
29-
30-
const pages = await getAllPagesInSpace(
31-
BLOG.notionPageId,
32-
BLOG.notionSpacesId,
33-
getPostBlocks,
34-
{
35-
traverseCollections: false
36-
}
37-
)
38-
39-
const subpageIds = Object.keys(pages)
40-
.map((pageId) => '/s' + mapPageUrl(pageId))
41-
.filter((path) => path && path !== '/s/')
42-
43-
// Remove post id
44-
const posts = await getAllPosts({ onlyNewsletter: false })
45-
const postIds = Object.values(posts)
46-
.map((postId) => '/s' + mapPageUrl(postId.id))
47-
const noPostsIds = subpageIds.concat(postIds).filter(v => !subpageIds.includes(v) || !postIds.includes(v))
48-
49-
const heros = await getAllPosts({ onlyHidden: true })
50-
const heroIds = Object.values(heros)
51-
.map((heroId) => '/s' + mapPageUrl(heroId.id))
52-
const paths = noPostsIds.concat(heroIds).filter(v => !noPostsIds.includes(v) || !heroIds.includes(v))
53-
28+
// Avoid hammering Notion at build time (429). Pages are rendered on demand via ISR.
5429
return {
55-
paths,
30+
paths: [],
5631
fallback: true
5732
}
58-
// return {
59-
// paths: [],
60-
// fallback: true
61-
// }
6233
}
6334

6435
export async function getStaticProps({ params: { subpage } }) {
@@ -67,6 +38,9 @@ export async function getStaticProps({ params: { subpage } }) {
6738
let blockMap, post
6839
try {
6940
blockMap = await getPostBlocks(subpage)
41+
if (!blockMap) {
42+
return { props: { post: null, blockMap: null }, revalidate: 60 }
43+
}
7044
const id = idToUuid(subpage)
7145

7246
const breadcrumbs = getPageBreadcrumbs(blockMap, id)
@@ -81,7 +55,7 @@ export async function getStaticProps({ params: { subpage } }) {
8155
// console.log("debug: ", breadcrumbs, post)
8256
} catch (err) {
8357
console.error(err)
84-
return { props: { post: null, blockMap: null } }
58+
return { props: { post: null, blockMap: null }, revalidate: 60 }
8559
}
8660

8761
// Allow only pages in your own space
@@ -98,7 +72,7 @@ export async function getStaticProps({ params: { subpage } }) {
9872
}
9973

10074
if (!pageAllowed(blockMap)) {
101-
return { props: { post: null, blockMap: null } }
75+
return { props: { post: null, blockMap: null }, revalidate: 60 }
10276
} else {
10377
return {
10478
props: { post, blockMap },

0 commit comments

Comments
 (0)