Skip to content

Commit a17439d

Browse files
committed
pagespeed优化
1 parent 20a34a1 commit a17439d

4 files changed

Lines changed: 35 additions & 27 deletions

File tree

packages/pure/components/pages/PostPreview.astro

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,9 +10,10 @@ type Props<Tag extends HTMLTag> = Polymorphic<{ as: Tag }> & {
1010
post: CollectionEntry<CollectionKey>
1111
detailed?: boolean
1212
class?: string
13+
lazyLoadImage?: boolean
1314
}
1415
15-
const { as: Tag = 'div', post, detailed = false, class: className } = Astro.props
16+
const { as: Tag = 'div', post, detailed = false, class: className, lazyLoadImage = true } = Astro.props
1617
1718
const { data } = post
1819
@@ -45,7 +46,7 @@ const postDate = data.updatedDate ?? data.publishDate
4546
<Image
4647
alt={data.heroImage.alt || data.title}
4748
class='cover-image absolute end-0 top-0 z-0 h-2/3 w-full rounded-2xl object-cover opacity-50 transition-opacity duration-300 group-hover/card:opacity-70 md:h-full md:w-3/5'
48-
loading='eager'
49+
loading={lazyLoadImage ? 'lazy' : 'eager'}
4950
{...data.heroImage}
5051
/>
5152
)

public/styles/global.css

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
@import url("https://fontsapi.zeoseven.com/89/main/result.css");
1+
/* External font loading - Will be loaded via JavaScript */
22

33
/* Homepage specific styles, triggered by a class added via script */
44
body.hide-scrollbar {

src/components/BaseHead.astro

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,13 @@ const socialImageURL = new URL(ogImage ? ogImage : '/images/social-card.png', As
3030

3131
{/* Font Preload */}
3232
<link rel="preload" href="/fonts/LinBiolinum.woff2" as="font" type="font/woff2" crossorigin />
33+
34+
{/* External Font - Deferred Loading */}
3335
<link rel="preconnect" href="https://fontsapi.zeoseven.com" crossorigin />
36+
<link rel="dns-prefetch" href="https://fontsapi.zeoseven.com" />
37+
38+
{/* DNS prefetch for performance */}
39+
<link rel="dns-prefetch" href="https://vercel.live" />
3440

3541
{/* Canonical URL */}
3642
<link rel='canonical' href={canonicalURL} />
@@ -88,6 +94,17 @@ const socialImageURL = new URL(ogImage ? ogImage : '/images/social-card.png', As
8894
{/* Styles */}
8995
<link rel='stylesheet' href='/styles/global.css' />
9096

97+
{/* Async font loading */}
98+
<script is:inline>
99+
// Load external font after page load to improve initial render
100+
window.addEventListener('load', function() {
101+
const link = document.createElement('link');
102+
link.rel = 'stylesheet';
103+
link.href = 'https://fontsapi.zeoseven.com/89/main/result.css';
104+
document.head.appendChild(link);
105+
});
106+
</script>
107+
91108
{
92109
/* User script*/
93110
prod && (

src/pages/index.astro

Lines changed: 14 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -110,8 +110,8 @@ const currentReading = bookshelfData.find((item: any) => item.status === 'readin
110110
<a href='/blog' class='text-sm text-primary hover:underline group-hover:translate-x-1 transition-transform inline-block'>查看更多 →</a>
111111
</div>
112112
<ul class='flex flex-col gap-y-3'>
113-
{allPostsByDate.map((post) => (
114-
<PostPreview post={post} />
113+
{allPostsByDate.map((post, index) => (
114+
<PostPreview post={post} lazyLoadImage={index > 0} />
115115
))}
116116
</ul>
117117
</div>
@@ -236,9 +236,11 @@ const currentReading = bookshelfData.find((item: any) => item.status === 'readin
236236
</div>
237237

238238
<script is:inline>
239-
// Force light theme on homepage
240-
localStorage.setItem('theme', 'light');
241-
document.documentElement.dataset.theme = 'light';
239+
// Force light theme on homepage - Execute immediately
240+
// (function() {
241+
// localStorage.setItem('theme', 'light');
242+
// document.documentElement.dataset.theme = 'light';
243+
// })();
242244

243245
// 数字滚动动画
244246
function animateCounter(element, targetValue, duration = 2000) {
@@ -266,18 +268,6 @@ const currentReading = bookshelfData.find((item: any) => item.status === 'readin
266268
requestAnimationFrame(updateCounter);
267269
}
268270

269-
// 邮箱混淆 - 防止爬虫抓取
270-
function obfuscateEmail() {
271-
// 将邮箱地址拆分成多个部分,避免明文出现
272-
const parts = ['1020082805', '@', 'qq', '.', 'com'];
273-
const email = parts.join('');
274-
275-
const emailLink = document.getElementById('email-link');
276-
if (emailLink) {
277-
emailLink.href = 'mailto:' + email;
278-
}
279-
}
280-
281271
// 更新总浏览量显示和进度条
282272
function updateViewsDisplay(viewsElement, progressElement, newValue, hasAnimated = false) {
283273
if (viewsElement && progressElement && newValue && newValue !== '-') {
@@ -372,15 +362,15 @@ const currentReading = bookshelfData.find((item: any) => item.status === 'readin
372362
<style>
373363
/* 卡片渐入动画 */
374364
.dashboard-card {
375-
animation: fadeInUp 0.6s ease-out;
376-
animation-fill-mode: both;
365+
opacity: 0;
366+
animation: fadeInUp 0.6s ease-out forwards;
377367
}
378368

379-
.dashboard-card:nth-child(1) { animation-delay: 0.1s; }
380-
.dashboard-card:nth-child(2) { animation-delay: 0.2s; }
381-
.dashboard-card:nth-child(3) { animation-delay: 0.3s; }
382-
.dashboard-card:nth-child(4) { animation-delay: 0.4s; }
383-
.dashboard-card:nth-child(5) { animation-delay: 0.5s; }
369+
.reading-card { animation-delay: 0.1s; }
370+
.posts-card { animation-delay: 0.2s; }
371+
.avatar-card { animation-delay: 0.3s; }
372+
.stats-card { animation-delay: 0.4s; }
373+
.social-card { animation-delay: 0.5s; }
384374

385375
@keyframes fadeInUp {
386376
from {

0 commit comments

Comments
 (0)