Skip to content

Commit ea3bd33

Browse files
committed
♻️ Make config injectable
* New hook: useConfig * Avoid sending private fields from config file to client
1 parent 2f15a92 commit ea3bd33

33 files changed

+278
-190
lines changed

components/BlogPost.js

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,10 @@
11
import Link from 'next/link'
2-
import BLOG from '@/blog.config'
2+
import { useConfig } from '@/lib/config'
33
import FormattedDate from '@/components/FormattedDate'
44

55
const BlogPost = ({ post }) => {
6+
const BLOG = useConfig()
7+
68
return (
79
<Link href={`${BLOG.path}/${post.slug}`}>
810
<article key={post.id} className="mb-6 md:mb-8">

components/Comments.js

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ import { useRouter } from 'next/router'
33
import dynamic from 'next/dynamic'
44
import cn from 'classnames'
55
import { fetchCusdisLang } from '@/lib/cusdisLang'
6-
import BLOG from '@/blog.config'
6+
import { useConfig } from '@/lib/config'
77

88
const GitalkComponent = dynamic(
99
() => {
@@ -26,6 +26,7 @@ const CusdisComponent = dynamic(
2626

2727
const Comments = ({ frontMatter }) => {
2828
const router = useRouter()
29+
const BLOG = useConfig()
2930

3031
const fullWidth = frontMatter.fullWidth ?? false
3132

@@ -55,7 +56,7 @@ const Comments = ({ frontMatter }) => {
5556
)}
5657
{BLOG.comment && BLOG.comment.provider === 'cusdis' && (
5758
<CusdisComponent
58-
lang={fetchCusdisLang()}
59+
lang={fetchCusdisLang(BLOG.lang)}
5960
attrs={{
6061
host: BLOG.comment.cusdisConfig.host,
6162
appId: BLOG.comment.cusdisConfig.appId,

components/Container.js

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,14 @@
11
import Header from '@/components/Header'
22
import Footer from '@/components/Footer'
3-
import BLOG from '@/blog.config'
3+
import { useConfig } from '@/lib/config'
44
import Head from 'next/head'
55
import PropTypes from 'prop-types'
66
import cn from 'classnames'
77
// import BlogPost from './BlogPost'
88

99
const Container = ({ children, layout, fullWidth, ...customMeta }) => {
10+
const BLOG = useConfig()
11+
1012
const url = BLOG.path.length ? `${BLOG.link}/${BLOG.path}` : BLOG.link
1113
const meta = {
1214
title: BLOG.title,

components/Footer.js

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
1-
import BLOG from '@/blog.config'
1+
import { useConfig } from '@/lib/config'
22
import Vercel from '@/components/Vercel'
33
const Footer = ({ fullWidth }) => {
4+
const BLOG = useConfig()
5+
46
const d = new Date()
57
const y = d.getFullYear()
68
const from = +BLOG.since

components/FormattedDate.js

Lines changed: 20 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,29 @@
1-
'use client'
2-
1+
import { useEffect, useState } from 'react'
32
import dayjs from 'dayjs'
43
import localizedFormat from 'dayjs/plugin/localizedFormat'
5-
import BLOG from '@/blog.config'
4+
import { useConfig } from '@/lib/config'
65

76
dayjs.extend(localizedFormat)
87

9-
const lang = BLOG.lang.slice(0, 2)
10-
import(`dayjs/locale/${lang}`)
11-
.then(() => {
12-
dayjs.locale(BLOG.lang.slice(0, 2))
13-
})
14-
.catch(() => console.warn(`dayjs locale \`${lang}\` not found`))
8+
const loaded = {}
159

1610
export default function FormattedDate ({ date }) {
11+
const lang = useConfig().lang.slice(0, 2)
12+
const [isLocaleLoaded, setIsLocaleLoaded] = useState(loaded[lang] === true)
13+
14+
useEffect(() => {
15+
if (!isLocaleLoaded) {
16+
loaded[lang] ??= import(`dayjs/locale/${lang}`).then(
17+
() => {
18+
loaded[lang] = true
19+
dayjs.locale(lang)
20+
},
21+
() => console.warn(`dayjs locale \`${lang}\` not found`)
22+
)
23+
loaded[lang].then(() => setIsLocaleLoaded(true))
24+
}
25+
26+
}, [isLocaleLoaded, lang])
27+
1728
return <span>{dayjs(date).format('ll')}</span>
1829
}

components/Gtag.js

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,20 @@
11
import { useEffect } from 'react'
22
import { useRouter } from 'next/router'
3+
import { useConfig } from '@/lib/config'
34
import * as gtag from '@/lib/gtag'
45

56
const Gtag = () => {
7+
const config = useConfig()
68
const router = useRouter()
79
useEffect(() => {
810
const handleRouteChange = url => {
9-
gtag.pageview(url)
11+
gtag.pageview(config.analytics.gaConfig.measurementId, url)
1012
}
1113
router.events.on('routeChangeComplete', handleRouteChange)
1214
return () => {
1315
router.events.off('routeChangeComplete', handleRouteChange)
1416
}
15-
}, [router.events])
17+
}, [config, router.events])
1618
return null
1719
}
1820
export default Gtag

components/Header.js

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,12 @@
11
import { forwardRef, useCallback, useEffect, useRef, useState } from 'react'
22
import Link from 'next/link'
33
import Image from 'next/image'
4-
import BLOG from '@/blog.config'
4+
import { useConfig } from '@/lib/config'
55
import { useLocale } from '@/lib/locale'
66
import useTheme from '@/lib/theme'
77

88
const NavBar = () => {
9+
const BLOG = useConfig()
910
const locale = useLocale()
1011
const links = [
1112
{ id: 0, name: locale.NAV.INDEX, to: BLOG.path || '/', show: true },
@@ -33,6 +34,7 @@ const NavBar = () => {
3334
}
3435

3536
export default function Header ({ navBarTitle, fullWidth }) {
37+
const BLOG = useConfig()
3638
const { dark } = useTheme()
3739

3840
// Favicon

components/NotionRenderer.js

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,8 @@ import { createElement as h } from 'react'
22
import dynamic from 'next/dynamic'
33
import { NotionRenderer as Renderer } from 'react-notion-x'
44
import { getTextContent } from 'notion-utils'
5-
import BLOG from '@/blog.config'
6-
import tailwindConfig from '@/tailwind.config'
5+
import { FONTS_SANS, FONTS_SERIF } from '@/consts'
6+
import { useConfig } from '@/lib/config'
77
import Toggle from '@/components/notion-blocks/Toggle'
88

99
// Lazy-load some heavy components & override the renderers of some block types
@@ -106,10 +106,12 @@ const mapPageUrl = id => `https://www.notion.so/${id.replace(/-/g, '')}`
106106
* @param props - Anything that react-notion-x/NotionRenderer supports
107107
*/
108108
export default function NotionRenderer (props) {
109+
const config = useConfig()
110+
109111
const font = {
110-
'sans-serif': tailwindConfig.theme.extend.fontFamily.sans,
111-
'serif': tailwindConfig.theme.extend.fontFamily.serif
112-
}[BLOG.font]
112+
'sans-serif': FONTS_SANS,
113+
'serif': FONTS_SERIF
114+
}[config.font]
113115

114116
// Mark block types to be custom rendered by appending a suffix
115117
if (props.recordMap) {

components/Pagination.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,9 @@
11
import Link from 'next/link'
2-
import BLOG from '@/blog.config'
2+
import { useConfig } from '@/lib/config'
33
import { useLocale } from '@/lib/locale'
44

55
const Pagination = ({ page, showNext }) => {
6+
const BLOG = useConfig()
67
const locale = useLocale()
78
const currentPage = +page
89
let additionalClassName = 'justify-between'

components/Post.js

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import PropTypes from 'prop-types'
22
import Image from 'next/image'
33
import cn from 'classnames'
4-
import BLOG from '@/blog.config'
4+
import { useConfig } from '@/lib/config'
55
import useTheme from '@/lib/theme'
66
import FormattedDate from '@/components/FormattedDate'
77
import TagItem from '@/components/TagItem'
@@ -19,7 +19,8 @@ import TableOfContents from '@/components/TableOfContents'
1919
* @prop {string} emailHash - Author email hash (for Gravatar)
2020
* @prop {boolean} [fullWidth] - Whether in full-width mode
2121
*/
22-
export default function Post(props) {
22+
export default function Post (props) {
23+
const BLOG = useConfig()
2324
const { post, blockMap, emailHash, fullWidth = false } = props
2425
const { dark } = useTheme()
2526

0 commit comments

Comments
 (0)