Skip to content

Commit f10049a

Browse files
committed
feat(KTL-2844): Enable interactive faster than hydrated
1 parent 6f5db63 commit f10049a

File tree

4 files changed

+22
-4
lines changed

4 files changed

+22
-4
lines changed

components/hydration-event.tsx

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
import { useEffect } from "react";
2+
3+
export default function HydrationEvent() {
4+
useEffect(() => {
5+
document.documentElement.classList.add("hydrated");
6+
window.dispatchEvent(new Event('react:hydrated'));
7+
}, []);
8+
return null;
9+
}

pages/_app.jsx

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import React, { useEffect } from 'react';
22
import Head from 'next/head';
33
import Script from 'next/script';
4+
import dynamic from 'next/dynamic';
45

56
import 'normalize.css';
67
import './global.css';
@@ -9,10 +10,16 @@ import '@jetbrains/kotlin-web-site-ui/out/components/typography/index.css';
910
import '@jetbrains/kotlin-web-site-ui/out/components/grid/index.css';
1011
import { Favicon } from '../components/favicon/favicon';
1112

13+
const HydrationEvent = dynamic(() => import('../components/hydration-event'), {
14+
ssr: false
15+
});
16+
1217
// This default export is required in a new `pages/_app.js` file.
1318
export default function MyApp({ Component, pageProps }) {
1419
useEffect(() => {
15-
document.body.className = pageProps.isDarkTheme ? 'dark-theme' : '';
20+
if(pageProps.isDarkTheme) {
21+
document.documentElement.classList.add('dark-theme');
22+
}
1623
});
1724

1825
return (
@@ -22,6 +29,9 @@ export default function MyApp({ Component, pageProps }) {
2229
<meta name="viewport" content="width=device-width, initial-scale=1" />
2330
</Head>
2431

32+
<HydrationEvent/>
33+
34+
<Script src={'https://cdn.optimizely.com/js/26633200186.js'} strategy={"beforeInteractive"}/>
2535
<Script
2636
id={'google-tag-manager-inline-script'}
2737
strategy="afterInteractive"

pages/_document.jsx

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,13 @@
11
import Document, { Html, Head, Main, NextScript } from 'next/document';
2+
import cn from 'classnames';
23

34
export default class MyDocument extends Document {
45
render() {
56
const pageProps = this.props?.__NEXT_DATA__?.props?.pageProps;
67
return (
78
<Html lang="en">
89
<Head />
9-
<body className={pageProps.isDarkTheme ? 'dark-theme' : ''}>
10+
<body className={cn(pageProps.isDarkTheme ? 'dark-theme' : '', 'nextjs')}>
1011
<Main />
1112
<NextScript />
1213
</body>

pages/index.tsx

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -256,8 +256,6 @@ function Index() {
256256
<GlobalFooter />
257257
</div>
258258
</ThemeProvider>
259-
260-
<Script src={'https://cdn.optimizely.com/js/26633200186.js'} strategy={"afterInteractive"}/>
261259
</Layout>
262260
);
263261
}

0 commit comments

Comments
 (0)