Download react-page/examples/pages/_document.tsx from Devendra174/react-code-dataset: direct link, hf CLI and curl.
- Browser
- Download file 2.01 kB
-
https://huggingface.co/datasets/Devendra174/react-code-dataset/resolve/main/react-page/examples/pages/_document.tsx
- Command line
-
hf download hf://datasets/Devendra174/react-code-dataset/react-page/examples/pages/_document.tsx
-
curl -L -o _document.tsx https://huggingface.co/datasets/Devendra174/react-code-dataset/resolve/main/react-page/examples/pages/_document.tsx
2.01 kB
| import React from 'react'; | |
| import type { DocumentContext } from 'next/document'; | |
| import Document, { Html, Head, Main, NextScript } from 'next/document'; | |
| import createEmotionServer from '@emotion/server/create-instance'; | |
| import { ServerStyleSheet } from 'styled-components'; | |
| import createEmotionCache from '../utils/createEmotionCache'; | |
| export default class MyDocument extends Document { | |
| static async getInitialProps(ctx: DocumentContext) { | |
| const styledComponentsSheet = new ServerStyleSheet(); | |
| const originalRenderPage = ctx.renderPage; | |
| const cache = createEmotionCache(); | |
| const { extractCriticalToChunks } = createEmotionServer(cache); | |
| try { | |
| ctx.renderPage = () => | |
| originalRenderPage({ | |
| // eslint-disable-next-line @typescript-eslint/no-explicit-any | |
| enhanceApp: (App: any) => (props: any) => | |
| styledComponentsSheet.collectStyles( | |
| <App emotionCache={cache} {...props} /> | |
| ), | |
| }); | |
| const initialProps = await Document.getInitialProps(ctx); | |
| const emotionStyles = extractCriticalToChunks(initialProps.html); | |
| const emotionStyleTags = emotionStyles.styles.map((style) => ( | |
| <style | |
| data-emotion={`${style.key} ${style.ids.join(' ')}`} | |
| key={style.key} | |
| // eslint-disable-next-line react/no-danger | |
| dangerouslySetInnerHTML={{ __html: style.css }} | |
| /> | |
| )); | |
| return { | |
| ...initialProps, | |
| styles: [ | |
| ...React.Children.toArray(initialProps.styles), | |
| styledComponentsSheet.getStyleElement(), | |
| ...emotionStyleTags, | |
| ], | |
| }; | |
| } finally { | |
| styledComponentsSheet.seal(); | |
| } | |
| } | |
| render() { | |
| return ( | |
| <Html> | |
| <Head> | |
| <link | |
| href="https://fonts.googleapis.com/css?family=Open+Sans" | |
| rel="stylesheet" | |
| /> | |
| </Head> | |
| <body> | |
| <Main /> | |
| <NextScript /> | |
| </body> | |
| </Html> | |
| ); | |
| } | |
| } | |