Add SSR support

This commit is contained in:
Zoe Roux
2022-11-11 00:39:18 +09:00
parent 3c114f0da5
commit ca503e9db1
9 changed files with 159 additions and 14 deletions
+9 -6
View File
@@ -3,12 +3,15 @@
// Licensed under the MIT license. See LICENSE file in the project root for details.
//
import { StyleRegistryProvider } from "@yoshiki/react/src/registry";
import type { AppProps } from "next/app";
import { useLayoutEffect, useState } from "react";
export default function App({ Component, pageProps }: AppProps) {
const [show, showApp] = useState(false);
const App = ({ Component, pageProps }: AppProps) => {
return (
<StyleRegistryProvider>
<Component {...pageProps} />
</StyleRegistryProvider>
);
};
useLayoutEffect(() => showApp(true));
return show ? <Component {...pageProps} /> : null;
}
export default App;
@@ -0,0 +1,40 @@
//
// Copyright (c) Zoe Roux and contributors. All rights reserved.
// Licensed under the MIT license. See LICENSE file in the project root for details.
//
import {
StyleRegistryProvider,
createStyleRegistry,
useStyleRegistry,
} from "@yoshiki/react/src/registry";
import Document, { DocumentContext } from "next/document";
Document.getInitialProps = async (ctx: DocumentContext) => {
const renderPage = ctx.renderPage;
const registry = createStyleRegistry();
ctx.renderPage = () =>
renderPage({
enhanceApp: (App) => (props) => {
return (
<StyleRegistryProvider registry={registry}>
<App {...props} />
</StyleRegistryProvider>
);
},
});
const props = await ctx.defaultGetInitialProps(ctx);
return {
...props,
styles: (
<>
{props.styles}
{registry.flushToComponent()}
</>
),
};
};
export default Document;