Skip to content

Commit e087fdc

Browse files
committed
fix: improve magzine homepage loading state
1 parent a18e55c commit e087fdc

2 files changed

Lines changed: 76 additions & 3 deletions

File tree

themes/magzine/index.js

Lines changed: 38 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,38 @@ import TouchMeCard from './components/TouchMeCard'
3939
import CONFIG from './config'
4040
import { Style } from './style'
4141

42+
const IndexSkeleton = () => (
43+
<div className='pt-10 md:pt-18'>
44+
<div className='w-full bg-[#f6f6f1] dark:bg-black'>
45+
<div className='mx-auto w-full max-w-screen-3xl px-4 py-10 lg:px-0'>
46+
<div className='grid gap-10 xl:grid-cols-2'>
47+
<section className='space-y-5'>
48+
<div className='h-80 w-full animate-pulse bg-gray-200 dark:bg-gray-800' />
49+
<div className='h-4 w-24 animate-pulse bg-gray-200 dark:bg-gray-800' />
50+
<div className='h-10 w-4/5 animate-pulse bg-gray-200 dark:bg-gray-800' />
51+
<div className='h-4 w-2/3 animate-pulse bg-gray-200 dark:bg-gray-800' />
52+
</section>
53+
<section className='space-y-6'>
54+
<div className='h-48 w-full animate-pulse bg-gray-200 dark:bg-gray-800' />
55+
{[0, 1].map(item => (
56+
<div
57+
key={item}
58+
className='flex gap-6 border-t border-gray-300 pt-6 dark:border-gray-800'>
59+
<div className='min-w-0 flex-1 space-y-3'>
60+
<div className='h-4 w-20 animate-pulse bg-gray-200 dark:bg-gray-800' />
61+
<div className='h-6 w-4/5 animate-pulse bg-gray-200 dark:bg-gray-800' />
62+
<div className='h-4 w-2/3 animate-pulse bg-gray-200 dark:bg-gray-800' />
63+
</div>
64+
<div className='h-32 w-32 shrink-0 animate-pulse bg-gray-200 dark:bg-gray-800' />
65+
</div>
66+
))}
67+
</section>
68+
</div>
69+
</div>
70+
</div>
71+
</div>
72+
)
73+
4274
// 主题全局状态
4375
const ThemeGlobalMagzine = createContext()
4476
export const useMagzineGlobal = () => useContext(ThemeGlobalMagzine)
@@ -75,7 +107,7 @@ const LayoutBase = props => {
75107
<div
76108
id='main'
77109
role='main'
78-
className='flex-grow' // 这个类保证 main 区域填满剩余的空白
110+
className='flex-grow min-h-[60vh] bg-[#f6f6f1] dark:bg-black'
79111
>
80112
{children}
81113
</div>
@@ -100,7 +132,11 @@ const LayoutBase = props => {
100132
* @returns
101133
*/
102134
const LayoutIndex = props => {
103-
const { posts } = props
135+
const posts = Array.isArray(props?.posts) ? props.posts : []
136+
137+
if (posts.length === 0) {
138+
return <IndexSkeleton />
139+
}
104140

105141
// ===== 1. Hero区域 =====
106142
const heroTopPosts = posts.slice(0, 1)

themes/theme.js

Lines changed: 38 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,43 @@ const baseLayoutCache = new Map()
1010
const layoutByThemeCache = new Map()
1111
let domFixTimer = null
1212

13+
const MagzineLayoutLoading = () => (
14+
<div className='w-full bg-[#f6f6f1] dark:bg-black'>
15+
<div className='mx-auto w-full max-w-screen-3xl px-4 py-10 lg:px-0'>
16+
<div className='grid gap-10 xl:grid-cols-2'>
17+
<div className='space-y-5'>
18+
<div className='h-80 w-full animate-pulse bg-gray-200 dark:bg-gray-800' />
19+
<div className='h-4 w-28 animate-pulse bg-gray-200 dark:bg-gray-800' />
20+
<div className='h-10 w-4/5 animate-pulse bg-gray-200 dark:bg-gray-800' />
21+
<div className='h-4 w-2/3 animate-pulse bg-gray-200 dark:bg-gray-800' />
22+
</div>
23+
<div className='space-y-6'>
24+
<div className='h-48 w-full animate-pulse bg-gray-200 dark:bg-gray-800' />
25+
{[0, 1].map(item => (
26+
<div
27+
key={item}
28+
className='flex gap-6 border-t border-gray-300 pt-6 dark:border-gray-800'>
29+
<div className='min-w-0 flex-1 space-y-3'>
30+
<div className='h-4 w-24 animate-pulse bg-gray-200 dark:bg-gray-800' />
31+
<div className='h-6 w-4/5 animate-pulse bg-gray-200 dark:bg-gray-800' />
32+
<div className='h-4 w-2/3 animate-pulse bg-gray-200 dark:bg-gray-800' />
33+
</div>
34+
<div className='h-32 w-32 shrink-0 animate-pulse bg-gray-200 dark:bg-gray-800' />
35+
</div>
36+
))}
37+
</div>
38+
</div>
39+
</div>
40+
</div>
41+
)
42+
43+
const getLayoutLoading = (themeName, layoutName) => {
44+
if (themeName === 'magzine' && layoutName === 'LayoutIndex') {
45+
return MagzineLayoutLoading
46+
}
47+
return undefined
48+
}
49+
1350
const normalizeThemeName = themeValue => {
1451
if (!themeValue || typeof themeValue !== 'string') return BLOG.THEME
1552
const firstTheme = themeValue.split(',')[0].trim()
@@ -139,7 +176,7 @@ export const useLayoutByTheme = ({ layoutName, theme }) => {
139176
}
140177
return Selected
141178
}),
142-
{ ssr: true }
179+
{ ssr: true, loading: getLayoutLoading(themeQuery, layoutName) }
143180
)
144181
layoutByThemeCache.set(cacheKey, DynamicLayoutComponent)
145182
scheduleFixThemeDOM(themeQuery === BLOG.THEME ? 80 : 240)

0 commit comments

Comments
 (0)