Skip to content

Commit 45e60d5

Browse files
authored
Merge pull request #1487 from scroll-tech/blog-from-server
fix: fetch blog content from server
2 parents 447374c + 9df0ff0 commit 45e60d5

File tree

4 files changed

+102
-95
lines changed

4 files changed

+102
-95
lines changed

src/app/blog/[blogId]/MoreBlogs.tsx

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
import { Pagination } from "swiper/modules"
2+
import { Swiper, SwiperSlide } from "swiper/react"
3+
4+
import { Box, Stack, Typography } from "@mui/material"
5+
6+
import ArticleCard from "@/components/ArticleCard"
7+
8+
const MoreBlogs = props => {
9+
const { blogs, title, ...restProps } = props
10+
11+
return (
12+
<Box {...restProps}>
13+
<Typography
14+
variant="h1"
15+
sx={{
16+
textAlign: "center",
17+
mt: ["3rem", "5rem"],
18+
mb: ["2rem", "3rem"],
19+
}}
20+
>
21+
{title}
22+
</Typography>
23+
24+
<Swiper
25+
slidesPerView={1}
26+
spaceBetween={0}
27+
centeredSlides
28+
pagination={{
29+
clickable: true,
30+
}}
31+
modules={[Pagination]}
32+
className="!pb-[16px] !sm:pb-[28px]"
33+
>
34+
{props.blogs.map(blog => (
35+
<SwiperSlide key={blog.title}>
36+
<Stack
37+
direction="row"
38+
sx={{
39+
justifyContent: ["center", "center", "space-between"],
40+
}}
41+
className="wrapper"
42+
>
43+
<ArticleCard blog={blog} />
44+
</Stack>
45+
</SwiperSlide>
46+
))}
47+
</Swiper>
48+
</Box>
49+
)
50+
}
51+
52+
export default MoreBlogs

src/app/blog/[blogId]/actions.ts

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
"use server"
2+
3+
import { fetchBlogDetailURL } from "@/apis/blog"
4+
5+
export const fetchBlogContent = async (blogId: string) => {
6+
const response = await fetch(fetchBlogDetailURL(blogId))
7+
8+
if (response.ok) {
9+
const blogContent = await response.text()
10+
if (!blogContent) {
11+
throw new Error("Not found")
12+
}
13+
return blogContent
14+
} else {
15+
throw new Error("Failed to fetch blog content")
16+
}
17+
}

src/app/blog/[blogId]/articles.tsx

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

src/app/blog/[blogId]/detail.tsx

Lines changed: 33 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -9,18 +9,18 @@ import rehypeRaw from "rehype-raw"
99
import remarkGfm from "remark-gfm"
1010
import remarkMath from "remark-math"
1111

12-
import { Box, Typography } from "@mui/material"
12+
import { Box } from "@mui/material"
1313
import { styled } from "@mui/system"
1414

15-
import { fetchBlogDetailURL } from "@/apis/blog"
1615
import blogSource from "@/assets/blog/main.data.json"
1716
import LoadingPage from "@/components/LoadingPage"
1817
import { LANGUAGE_MAP } from "@/constants"
1918
import useCheckViewport from "@/hooks/useCheckViewport"
2019
import useUserLanguage from "@/hooks/useUserLanguage"
2120
import { filterBlogsByLanguage } from "@/utils"
2221

23-
import Articles from "./articles"
22+
import MoreBlogs from "./MoreBlogs"
23+
import { fetchBlogContent } from "./actions"
2424
import TOC from "./components/tableOfContents"
2525

2626
const BlogContainer = styled(Box)(
@@ -61,36 +61,37 @@ const BlogDetail = props => {
6161
const blogsWithLang = useMemo(() => filterBlogsByLanguage(blogSource, language), [blogSource, language])
6262

6363
useEffect(() => {
64-
const regex = /([^_]*?)_lang_[^_]+/g
65-
const blogIdMatch = blogId?.match(regex)
66-
67-
const blogItemWithLang = blogSource.find(item => item.id === `${blogId}_lang_${language}`)
68-
69-
if ((!blogIdMatch && language === "en") || (blogIdMatch && language !== "en") || (!blogIdMatch && language !== "en" && !blogItemWithLang)) {
70-
let anchors = [...document.querySelectorAll("a")]
71-
anchors.map(anchor => {
72-
if (anchor.href.includes("/Content/")) {
73-
anchor.setAttribute("target", "")
64+
async function fetchCurrentBlog() {
65+
const regex = /([^_]*?)_lang_[^_]+/g
66+
const blogIdMatch = blogId?.match(regex)
67+
68+
const blogItemWithLang = blogSource.find(item => item.id === `${blogId}_lang_${language}`)
69+
70+
if ((!blogIdMatch && language === "en") || (blogIdMatch && language !== "en") || (!blogIdMatch && language !== "en" && !blogItemWithLang)) {
71+
let anchors = [...document.querySelectorAll("a")]
72+
anchors.map(anchor => {
73+
if (anchor.href.includes("/Content/")) {
74+
anchor.setAttribute("target", "")
75+
}
76+
return anchor
77+
})
78+
try {
79+
setLoading(true)
80+
const text = await fetchBlogContent(blogId)
81+
setBlogContent(text)
82+
} catch (_error) {
83+
router.push("/404")
84+
} finally {
85+
setLoading(false)
7486
}
75-
return anchor
76-
})
77-
try {
78-
setLoading(true)
79-
fetch(fetchBlogDetailURL(blogId))
80-
.then(response => response.text())
81-
.then(text => {
82-
setLoading(false)
83-
setBlogContent(text)
84-
})
85-
} catch (_error) {
86-
router.push("/404")
87+
} else if (blogIdMatch && language === "en") {
88+
const nextBlogId = blogId.replace(regex, "$1")
89+
router.push(`/blog/${nextBlogId}`)
90+
} else if (blogItemWithLang) {
91+
router.push(`/blog/${blogId}_lang_${language}`)
8792
}
88-
} else if (blogIdMatch && language === "en") {
89-
const nextBlogId = blogId.replace(regex, "$1")
90-
router.push(`/blog/${nextBlogId}`)
91-
} else if (blogItemWithLang) {
92-
router.push(`/blog/${blogId}_lang_${language}`)
9393
}
94+
fetchCurrentBlog()
9495
}, [blogId, language])
9596

9697
useEffect(() => {
@@ -119,21 +120,8 @@ const BlogDetail = props => {
119120
className="markdown-body"
120121
/>
121122
</BlogContainer>
122-
{isPortrait ? (
123-
<Box sx={{ paddingBottom: "10rem" }}>
124-
<Typography
125-
variant="h1"
126-
sx={{
127-
textAlign: "center",
128-
mt: ["3rem", "5rem"],
129-
mb: ["2rem", "3rem"],
130-
}}
131-
>
132-
{LANGUAGE_MAP[language].more_articles}
133-
</Typography>
134-
<Articles blogs={moreBlog} />
135-
</Box>
136-
) : null}
123+
124+
{!!isPortrait && <MoreBlogs sx={{ pb: "10rem" }} blogs={moreBlog} title={LANGUAGE_MAP[language].more_articles}></MoreBlogs>}
137125
</Box>
138126
)}
139127
</Box>

0 commit comments

Comments
 (0)