Skip to content

Commit 394de0f

Browse files
Attempt to improve LCP
1 parent 701277c commit 394de0f

File tree

3 files changed

+83
-6
lines changed

3 files changed

+83
-6
lines changed

src/components/Dots.astro

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
11
<div class="absolute top-0 left-0 flex h-12 w-full justify-center p-2">
2-
<div class="bg-dots-light dark:bg-dots-dark h-12 w-full bg-repeat-x"></div>
2+
<div class="bg-dots-light dark:bg-dots-dark h-12 w-full bg-repeat-x" style="content-visibility: auto;"></div>
33
</div>

src/components/ShowArtwork.astro

Lines changed: 15 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -33,9 +33,20 @@ const { image } = Astro.props;
3333
</a>
3434

3535
<script>
36-
import Atropos from 'atropos';
36+
// Defer Atropos initialization to after LCP
37+
function initAtropos() {
38+
import('atropos').then((module) => {
39+
const Atropos = module.default;
40+
Atropos({
41+
el: '.show-art'
42+
});
43+
});
44+
}
3745

38-
Atropos({
39-
el: '.show-art'
40-
});
46+
// Initialize after page load to avoid blocking LCP
47+
if (document.readyState === 'complete') {
48+
initAtropos();
49+
} else {
50+
window.addEventListener('load', initAtropos);
51+
}
4152
</script>

src/layouts/Layout.astro

Lines changed: 67 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,35 @@ const { imageUrl, title } = Astro.props;
3434
const canonicalURL =
3535
Astro.props.canonicalURL ?? new URL(Astro.url.pathname, Astro.site);
3636
const description = Astro.props.description ?? starpodConfig.description;
37+
38+
function extractDomain(url: string): string | null {
39+
try {
40+
return new URL(url).hostname;
41+
} catch {
42+
return null;
43+
}
44+
}
45+
46+
function getExternalDomains(): string[] {
47+
const domains = new Set<string>();
48+
const siteHostname = Astro.site?.hostname;
49+
50+
// Add RSS feed domain
51+
const feedDomain = extractDomain(starpodConfig.rssFeed);
52+
if (feedDomain && feedDomain !== siteHostname) {
53+
domains.add(feedDomain);
54+
}
55+
56+
// Add image domain if it's external
57+
const imageDomain = extractDomain(show.image);
58+
if (imageDomain && imageDomain !== siteHostname) {
59+
domains.add(imageDomain);
60+
}
61+
62+
return Array.from(domains);
63+
}
64+
65+
const externalDomains = getExternalDomains();
3766
---
3867

3968
<!doctype html>
@@ -91,7 +120,8 @@ const description = Astro.props.description ?? starpodConfig.description;
91120

92121
<meta property="og:site_name" content={show.title} />
93122

94-
<link rel="preload" as="image" href={show.image} />
123+
<!-- Preload critical resources for faster LCP -->
124+
<link rel="preload" as="image" href={show.image} fetchpriority="high" />
95125

96126
<link
97127
crossorigin
@@ -101,6 +131,40 @@ const description = Astro.props.description ?? starpodConfig.description;
101131
type="font/woff2"
102132
/>
103133

134+
<!-- DNS prefetch for external resources -->
135+
{
136+
externalDomains.map((domain) => (
137+
<>
138+
<link rel="dns-prefetch" href={`//${domain}`} />
139+
<link rel="preconnect" href={`https://${domain}`} crossorigin />
140+
</>
141+
))
142+
}
143+
144+
<!-- Prefetch for Vercel image optimization -->
145+
{Astro.site && <link rel="preconnect" href={Astro.site.origin} />}
146+
147+
<!-- Critical CSS for LCP optimization -->
148+
<style>
149+
/* Critical styles for above-the-fold content */
150+
body {
151+
font-family: 'Inter', system-ui, sans-serif;
152+
margin: 0;
153+
}
154+
155+
.atropos-inner img {
156+
max-width: 100%;
157+
height: auto;
158+
display: block;
159+
}
160+
161+
/* Ensure LCP image renders quickly */
162+
.show-art img {
163+
content-visibility: auto;
164+
contain: layout style paint;
165+
}
166+
</style>
167+
104168
<Schema
105169
slot="head"
106170
item={{
@@ -138,6 +202,7 @@ const description = Astro.props.description ?? starpodConfig.description;
138202
<div class="relative z-10 mx-auto lg:min-h-full lg:flex-auto">
139203
<div
140204
class="bg-light-card dark:bg-dark-card m-2 rounded-lg pt-10 pb-4 lg:pt-16 lg:pb-12"
205+
style="contain: layout style;"
141206
>
142207
<ShowArtwork image={show.image} />
143208

@@ -172,6 +237,7 @@ const description = Astro.props.description ?? starpodConfig.description;
172237
<div class="relative mt-2 pt-16">
173238
<div
174239
class="bg-gradient-light dark:bg-gradient-dark absolute top-0 right-0 left-0 z-0 h-80 w-full opacity-30"
240+
style="content-visibility: auto;"
175241
>
176242
</div>
177243

0 commit comments

Comments
 (0)