_document.tsx 920 B

1234567891011121314151617181920212223242526272829303132333435
  1. /* eslint-disable react/no-danger */
  2. import { Html, Head, Main, NextScript } from 'next/document';
  3. import { readFileSync } from 'fs';
  4. import { join } from 'path';
  5. class InlineStylesHead extends Head {
  6. getCssLinks: Head['getCssLinks'] = ({ allFiles }) => {
  7. const { assetPrefix } = this.context;
  8. if (!allFiles || allFiles.length === 0) return null;
  9. return allFiles
  10. .filter((file: any) => /\.css$/.test(file))
  11. .map((file: any) => (
  12. <style
  13. key={file}
  14. nonce={this.props.nonce}
  15. data-href={`${assetPrefix}/_next/${file}`}
  16. dangerouslySetInnerHTML={{
  17. __html: readFileSync(join(process.cwd(), '.next', file), 'utf-8'),
  18. }}
  19. />
  20. ));
  21. };
  22. }
  23. export default function Document() {
  24. return (
  25. <Html lang="en">
  26. <InlineStylesHead />
  27. <body>
  28. <Main />
  29. <NextScript />
  30. </body>
  31. </Html>
  32. );
  33. }