Skip to content

Commit 93c2fe5

Browse files
committed
Use import instead fetch
1 parent 2893a1e commit 93c2fe5

File tree

1 file changed

+4
-11
lines changed

1 file changed

+4
-11
lines changed

utils/posts.js

+4-11
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,15 @@
1-
const IS_PROD = process.env.NODE_ENV === 'production'
2-
const BASE_URL = IS_PROD ? 'https://reactjs.wiki/' : 'http://localhost:3000'
3-
4-
let posts = []
1+
import posts from '../public/content/index.json'
52

63
export const readIndex = async () => {
7-
if (posts.length) return posts
8-
9-
const res = await fetch(`${BASE_URL}/content/index.json`)
10-
posts = await res.json()
11-
124
return posts
135
}
146

157
export const fetchPost = async (slug) => {
168
const posts = await readIndex()
179

18-
const res = await fetch(`${BASE_URL}/content/${slug}.json`)
19-
const { content, level, title, id } = await res.json()
10+
const post = await import(`../public/content/${slug}.json`)
11+
console.log(post)
12+
const { content, level, title, id } = post
2013

2114
const currentIndex = posts.findIndex(post => post.id === id)
2215
const prev = currentIndex > 0 ? posts[currentIndex - 1] : null

0 commit comments

Comments
 (0)