mirror of
https://github.com/zoriya/yoshiki.git
synced 2026-06-07 04:05:11 +00:00
Add SSR support
This commit is contained in:
@@ -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;
|
||||
Reference in New Issue
Block a user