Skip to content

Commit

Permalink
theme async
Browse files Browse the repository at this point in the history
  • Loading branch information
tangly1024 committed Jun 4, 2023
1 parent 351878d commit 1513ab8
Show file tree
Hide file tree
Showing 15 changed files with 42 additions and 27 deletions.
Empty file added lib/memorize.js
Empty file.
10 changes: 10 additions & 0 deletions lib/utils.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,15 @@
// 封装异步加载资源的方法
import { memo } from 'react'

/**
* 组件持久化
*/
export const memorize = (Component) => {
const MemoizedComponent = (props) => {
return <Component {...props} />
}
return memo(MemoizedComponent)
}
/**
* 加载外部资源
* @param url 地址 例如 https://xx.com/xx.js
Expand Down
14 changes: 7 additions & 7 deletions pages/[...slug].js
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import { useGlobal } from '@/lib/global'
import { useEffect, useState } from 'react'
import { idToUuid } from 'notion-utils'
import { useRouter } from 'next/router'
import { isBrowser } from '@/lib/utils'
import { isBrowser, memorize } from '@/lib/utils'
import { getNotion } from '@/lib/notion/getNotion'
import { getPageTableOfContents } from '@/lib/notion/getPageTableOfContents'
import md5 from 'js-md5'
Expand All @@ -17,7 +17,7 @@ import Loading from '@/components/Loading'
* @param {*} props
* @returns
*/
const Slug = props => {
const Slug = memorize(props => {
const { theme, changeLoadingState } = useGlobal()
const { post, siteInfo } = props
const router = useRouter()
Expand Down Expand Up @@ -60,9 +60,9 @@ const Slug = props => {
}

/**
* 验证文章密码
* @param {*} result
*/
* 验证文章密码
* @param {*} result
*/
const validPassword = passInput => {
const encrypt = md5(post.slug + passInput)

Expand All @@ -86,9 +86,9 @@ const Slug = props => {
}

return (
<LayoutSlug {...props} showArticleInfo={true} meta={meta} />
<LayoutSlug {...props} showArticleInfo={true} meta={meta} />
)
}
})

export async function getStaticPaths() {
if (!BLOG.isProd) {
Expand Down
2 changes: 1 addition & 1 deletion pages/archive/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ const ArchiveIndex = props => {
type: 'website'
}

const LayoutArchive = dynamic(() => import(`@/themes/${theme}/LayoutArchive`).then(async (m) => { return m.LayoutArchive }), { ssr: true, loading: () => <Loading /> })
const LayoutArchive = dynamic(() => import(`@/themes/${theme}`).then(async (m) => { return m.LayoutArchive }), { ssr: true, loading: () => <Loading /> })
return <LayoutArchive {...props} meta={meta} />
}

Expand Down
4 changes: 2 additions & 2 deletions pages/category/[category]/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ export default function Category(props) {
const { siteInfo, posts } = props
const { locale } = useGlobal()
if (!posts) {
const Layout404 = dynamic(() => import(`@/themes/${theme}/Layout404`).then(async (m) => { return m.Layout404 }), { ssr: true, loading: () => <Loading /> })
const Layout404 = dynamic(() => import(`@/themes/${theme}`).then(async (m) => { return m.Layout404 }), { ssr: true, loading: () => <Loading /> })
return <Layout404 {...props} />
}
const meta = {
Expand All @@ -28,7 +28,7 @@ export default function Category(props) {
type: 'website'
}

const LayoutCategory = dynamic(() => import(`@/themes/${theme}/LayoutCategory`).then(async (m) => { return m.LayoutCategory }), { ssr: true, loading: () => <Loading /> })
const LayoutCategory = dynamic(() => import(`@/themes/${theme}`).then(async (m) => { return m.LayoutCategory }), { ssr: true, loading: () => <Loading /> })
return <LayoutCategory {...props} meta={meta} />
}

Expand Down
4 changes: 2 additions & 2 deletions pages/category/[category]/page/[page].js
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ export default function Category(props) {
const { siteInfo, posts } = props
const { locale } = useGlobal()
if (!posts) {
const Layout404 = dynamic(() => import(`@/themes/${theme}/Layout404`).then(async (m) => { return m.Layout404 }), { ssr: true, loading: () => <Loading /> })
const Layout404 = dynamic(() => import(`@/themes/${theme}`).then(async (m) => { return m.Layout404 }), { ssr: true, loading: () => <Loading /> })
return <Layout404 {...props} />
}
const meta = {
Expand All @@ -28,7 +28,7 @@ export default function Category(props) {
type: 'website'
}

const LayoutCategory = dynamic(() => import(`@/themes/${theme}/LayoutCategory`).then(async (m) => { return m.LayoutCategory }), { ssr: true, loading: () => <Loading /> })
const LayoutCategory = dynamic(() => import(`@/themes/${theme}`).then(async (m) => { return m.LayoutCategory }), { ssr: true, loading: () => <Loading /> })
return <LayoutCategory {...props} meta={meta} />
}

Expand Down
2 changes: 1 addition & 1 deletion pages/category/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ export default function Category(props) {
slug: 'category',
type: 'website'
}
const LayoutCategoryIndex = dynamic(() => import(`@/themes/${theme}/LayoutCategoryIndex`).then(async (m) => { return m.LayoutCategoryIndex }), { ssr: false })
const LayoutCategoryIndex = dynamic(() => import(`@/themes/${theme}`).then(async (m) => { return m.LayoutCategoryIndex }), { ssr: false })
return <LayoutCategoryIndex {...props} meta={meta} />
}

Expand Down
13 changes: 9 additions & 4 deletions pages/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,19 +6,24 @@ import { generateRss } from '@/lib/rss'
import { generateRobotsTxt } from '@/lib/robots.txt'
import dynamic from 'next/dynamic'
import Loading from '@/components/Loading'
import { memorize } from '@/lib/utils'

/**
* 首页布局
* @param {*} props
* @returns
*/
const Index = props => {
const Index = memorize(props => {
const { theme } = useGlobal()
const LayoutIndex = dynamic(() => import(`@/themes/${theme}`)
.then(async (m) => { return m.LayoutIndex }), { ssr: true, loading: () => <Loading /> }
const LayoutIndex = dynamic(
() =>
import(`@/themes/${theme}`).then((m) => {
return m.LayoutIndex
}),
{ ssr: true, loading: () => <Loading /> }
)
return <LayoutIndex {...props} />
}
})

/**
* SSG 获取数据
Expand Down
2 changes: 1 addition & 1 deletion pages/page/[page].js
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ const Page = props => {
type: 'website'
}

const LayoutPage = dynamic(() => import(`@/themes/${theme}/LayoutPage`).then(async (m) => { return m.LayoutPage }), { ssr: true, loading: () => <Loading /> })
const LayoutPage = dynamic(() => import(`@/themes/${theme}`).then(async (m) => { return m.LayoutPage }), { ssr: true, loading: () => <Loading /> })
return <LayoutPage {...props} meta={meta} />
}

Expand Down
2 changes: 1 addition & 1 deletion pages/search/[keyword]/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ const Index = props => {
}
const { theme } = useGlobal()

const LayoutSearch = dynamic(() => import(`@/themes/${theme}/LayoutSearch`).then(async (m) => { return m.LayoutSearch }), { ssr: true, loading: () => <Loading /> })
const LayoutSearch = dynamic(() => import(`@/themes/${theme}`).then(async (m) => { return m.LayoutSearch }), { ssr: true, loading: () => <Loading /> })
return <LayoutSearch {...props} currentSearch={keyword} meta={meta} />
}

Expand Down
2 changes: 1 addition & 1 deletion pages/search/[keyword]/page/[page].js
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ const Index = props => {
type: 'website'
}
const { theme } = useGlobal()
const LayoutSearch = dynamic(() => import(`@/themes/${theme}/LayoutSearch`).then(async (m) => { return m.LayoutSearch }), { ssr: true, loading: () => <Loading /> })
const LayoutSearch = dynamic(() => import(`@/themes/${theme}`).then(async (m) => { return m.LayoutSearch }), { ssr: true, loading: () => <Loading /> })
return <LayoutSearch {...props} currentSearch={keyword} meta={meta} />
}

Expand Down
2 changes: 1 addition & 1 deletion pages/search/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ const Search = props => {

const { theme } = useGlobal()

const LayoutSearch = dynamic(() => import(`@/themes/${theme}/LayoutSearch`).then(async (m) => { return m.LayoutSearch }), { ssr: true, loading: () => <Loading /> })
const LayoutSearch = dynamic(() => import(`@/themes/${theme}`).then(async (m) => { return m.LayoutSearch }), { ssr: true, loading: () => <Loading /> })
return <LayoutSearch {...props} posts={filteredPosts} currentSearch={keyword} meta={meta} />
}

Expand Down
6 changes: 3 additions & 3 deletions pages/tag/[tag]/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ const Tag = props => {
const { tag, siteInfo, posts } = props

if (!posts) {
const Layout404 = dynamic(() => import(`@/themes/${theme}/Layout404`).then(async (m) => { return m.Layout404 }), { ssr: true, loading: () => <Loading /> })
const Layout404 = dynamic(() => import(`@/themes/${theme}`).then(async (m) => { return m.Layout404 }), { ssr: true, loading: () => <Loading /> })
return <Layout404 {...props}/>
}

Expand All @@ -21,8 +21,8 @@ const Tag = props => {
slug: 'tag/' + tag,
type: 'website'
}
const LayoutTagIndex = dynamic(() => import(`@/themes/${theme}/LayoutTagIndex`).then(async (m) => { return m.LayoutTagIndex }), { ssr: true, loading: () => <Loading /> })
return <LayoutTagIndex {...props} meta={meta} />
const LayoutTag = dynamic(() => import(`@/themes/${theme}`).then(async (m) => { return m.LayoutTag }), { ssr: true, loading: () => <Loading /> })
return <LayoutTag {...props} meta={meta} />
}

export async function getStaticProps({ params: { tag } }) {
Expand Down
4 changes: 2 additions & 2 deletions pages/tag/[tag]/page/[page].js
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ const Tag = props => {
const { tag, siteInfo, posts } = props

if (!posts) {
const Layout404 = dynamic(() => import(`@/themes/${theme}/Layout404`).then(async (m) => { return m.Layout404 }), { ssr: true, loading: () => <Loading /> })
const Layout404 = dynamic(() => import(`@/themes/${theme}`).then(async (m) => { return m.Layout404 }), { ssr: true, loading: () => <Loading /> })
return <Layout404 {...props}/>
}

Expand All @@ -21,7 +21,7 @@ const Tag = props => {
slug: 'tag/' + tag,
type: 'website'
}
const LayoutTag = dynamic(() => import(`@/themes/${theme}/LayoutTag`).then(async (m) => { return m.LayoutTag }), { ssr: true, loading: () => <Loading /> })
const LayoutTag = dynamic(() => import(`@/themes/${theme}`).then(async (m) => { return m.LayoutTag }), { ssr: true, loading: () => <Loading /> })
return <LayoutTag {...props} meta={meta} />
}

Expand Down
2 changes: 1 addition & 1 deletion pages/tag/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ const TagIndex = props => {
slug: 'tag',
type: 'website'
}
const LayoutTagIndex = dynamic(() => import(`@/themes/${theme}/LayoutTagIndex`).then(async (m) => { return m.LayoutTagIndex }), { ssr: true, loading: () => <Loading /> })
const LayoutTagIndex = dynamic(() => import(`@/themes/${theme}`).then(async (m) => { return m.LayoutTagIndex }), { ssr: true, loading: () => <Loading /> })
return <LayoutTagIndex {...props} meta={meta} />
}

Expand Down

0 comments on commit 1513ab8

Please sign in to comment.