Skip to content

Commit 7eb68d4

Browse files
authored
Merge pull request #4157 from udecode/home/perf
Load iframe only when visible
2 parents 4cdde1b + d111e05 commit 7eb68d4

File tree

3 files changed

+101
-14
lines changed

3 files changed

+101
-14
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,94 @@
1+
'use client';
2+
3+
import { useEffect, useRef, useState } from 'react';
4+
5+
import { useSearchParams } from 'next/navigation';
6+
7+
import { BlockViewer } from '@/components/block-viewer';
8+
import { siteConfig } from '@/config/site';
9+
10+
const i18n = {
11+
cn: {
12+
buildYourRichTextEditor: '构建你的富文本编辑器',
13+
description: '框架 · 插件 · 组件 · 主题',
14+
getStarted: '开始使用',
15+
github: 'GitHub',
16+
potionDescription: '一个类似 Notion 的 AI 模板。',
17+
},
18+
en: {
19+
buildYourRichTextEditor: 'Build your rich-text editor',
20+
description: 'Framework · Plugins · Components · Themes',
21+
getStarted: 'Get Started',
22+
github: 'GitHub',
23+
potionDescription: 'A Notion-like AI template.',
24+
},
25+
};
26+
27+
export function PotionLazyBlock() {
28+
const searchParams = useSearchParams();
29+
30+
const [locale, setLocale] = useState<keyof typeof i18n>('en');
31+
const ref = useRef<HTMLDivElement>(null);
32+
const [shouldRender, setShouldRender] = useState(false);
33+
34+
useEffect(() => {
35+
const getLocale = async () => {
36+
const resolvedLocale = (searchParams?.get('locale') ||
37+
'en') as keyof typeof i18n;
38+
setLocale(resolvedLocale);
39+
};
40+
41+
void getLocale();
42+
}, [searchParams]);
43+
44+
useEffect(() => {
45+
const handleScroll = () => {
46+
// scroll home page down over 100px and render the block
47+
if (window.scrollY > 100 && !shouldRender) {
48+
setShouldRender(true);
49+
}
50+
};
51+
52+
window.addEventListener('scroll', handleScroll);
53+
54+
// Check initial scroll position
55+
handleScroll();
56+
57+
return () => {
58+
window.removeEventListener('scroll', handleScroll);
59+
};
60+
}, [shouldRender]);
61+
62+
const content = i18n[locale];
63+
64+
const block = {
65+
description: content.potionDescription,
66+
descriptionSrc: siteConfig.links.potionTemplate,
67+
isPro: true,
68+
meta: {
69+
iframeHeight: 800,
70+
},
71+
name: 'potion',
72+
src: siteConfig.links.potionIframe,
73+
};
74+
75+
return (
76+
<div ref={ref}>
77+
{shouldRender && (
78+
<BlockViewer
79+
dependencies={[]}
80+
highlightedFiles={[]}
81+
isPro={block.isPro}
82+
item={{
83+
description: block.description,
84+
meta: block.meta,
85+
name: block.name,
86+
src: block.src,
87+
type: 'registry:block',
88+
}}
89+
tree={[]}
90+
/>
91+
)}
92+
</div>
93+
);
94+
}

apps/www/src/app/(app)/page.tsx

+6-13
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@ import dynamic from 'next/dynamic';
66
import Link from 'next/link';
77

88
import HomeTabs from '@/app/(app)/_components/home-tabs';
9-
import { BlockDisplay } from '@/components/block-display';
109
import {
1110
PageHeader,
1211
PageHeaderDescription,
@@ -18,13 +17,18 @@ import { siteConfig } from '@/config/site';
1817
import { Button } from '@/registry/default/plate-ui/button';
1918

2019
import { AnnouncementButton } from './_components/announcement-button';
20+
import { PotionLazyBlock } from './_components/potion-lazy-block';
2121

2222
import '../../../public/r/themes.css';
2323

2424
const CustomizerDrawer = dynamic(
2525
() => import('@/components/customizer-drawer')
2626
);
2727

28+
const BlockDisplay = dynamic(() =>
29+
import('@/components/block-display').then((mod) => mod.BlockDisplay)
30+
);
31+
2832
const i18n = {
2933
cn: {
3034
buildYourRichTextEditor: '构建你的富文本编辑器',
@@ -84,17 +88,6 @@ export default async function IndexPage({
8488
const locale = ((await searchParams).locale || 'en') as keyof typeof i18n;
8589
const content = i18n[locale];
8690

87-
const block = {
88-
description: content.potionDescription,
89-
descriptionSrc: siteConfig.links.potionTemplate,
90-
isPro: true,
91-
meta: {
92-
iframeHeight: 800,
93-
},
94-
name: 'potion',
95-
src: siteConfig.links.potionIframe,
96-
};
97-
9891
return (
9992
<>
10093
<div className="relative">
@@ -136,7 +129,7 @@ export default async function IndexPage({
136129
</section>
137130

138131
<div className="relative mt-12 scroll-m-16 pb-48 md:mt-24 lg:mt-36">
139-
<BlockDisplay {...block} />
132+
<PotionLazyBlock />
140133
</div>
141134
</div>
142135
</div>

apps/www/src/components/block-display.tsx

+1-1
Original file line numberDiff line numberDiff line change
@@ -56,4 +56,4 @@ export async function BlockDisplay({
5656
tree={tree}
5757
/>
5858
);
59-
}
59+
}

0 commit comments

Comments
 (0)