diff --git a/packages/next/client/index.tsx b/packages/next/client/index.tsx index 86bdc3f6a275b..bed5e20a7e3b7 100644 --- a/packages/next/client/index.tsx +++ b/packages/next/client/index.tsx @@ -2,6 +2,7 @@ import '@next/polyfill-module' import React from 'react' import ReactDOM from 'react-dom' +import { StyleRegistry } from 'styled-jsx' import { HeadManagerContext } from '../shared/lib/head-manager-context' import mitt, { MittEmitter } from '../shared/lib/mitt' import { RouterContext } from '../shared/lib/router-context' @@ -598,7 +599,7 @@ function AppContainer({ > - {children} + {children} diff --git a/packages/next/package.json b/packages/next/package.json index 405432af9b064..bf4f78ee48f18 100644 --- a/packages/next/package.json +++ b/packages/next/package.json @@ -106,7 +106,7 @@ "stream-browserify": "3.0.0", "stream-http": "3.1.1", "string_decoder": "1.3.0", - "styled-jsx": "4.0.1", + "styled-jsx": "5.0.0-beta.1", "timers-browserify": "2.0.12", "tty-browserify": "0.0.1", "use-subscription": "1.5.1", diff --git a/packages/next/pages/_document.tsx b/packages/next/pages/_document.tsx index cdb4095cee8f5..02aad2c8939e4 100644 --- a/packages/next/pages/_document.tsx +++ b/packages/next/pages/_document.tsx @@ -1,5 +1,4 @@ import React, { Component, ReactElement, ReactNode, useContext } from 'react' -import flush from 'styled-jsx/server' import { BODY_RENDER_TARGET, OPTIMIZED_FONT_PROVIDERS, @@ -166,16 +165,8 @@ export default class Document

extends Component { * `getInitialProps` hook returns the context object with the addition of `renderPage`. * `renderPage` callback executes `React` rendering logic synchronously to support server-rendering wrappers */ - static async getInitialProps( - ctx: DocumentContext - ): Promise { - const enhanceApp = (App: any) => { - return (props: any) => - } - - const { html, head } = await ctx.renderPage({ enhanceApp }) - const styles = [...flush()] - return { html, head, styles } + static getInitialProps(ctx: DocumentContext): Promise { + return ctx.defaultGetInitialProps(ctx) } render() { @@ -213,6 +204,50 @@ export function Html( ) } +function AmpStyles({ + styles, +}: { + styles?: React.ReactElement[] | React.ReactFragment +}) { + if (!styles) return null + + // try to parse styles from fragment for backwards compat + const curStyles: React.ReactElement[] = Array.isArray(styles) + ? (styles as React.ReactElement[]) + : [] + if ( + // @ts-ignore Property 'props' does not exist on type ReactElement + styles.props && + // @ts-ignore Property 'props' does not exist on type ReactElement + Array.isArray(styles.props.children) + ) { + const hasStyles = (el: React.ReactElement) => + el?.props?.dangerouslySetInnerHTML?.__html + // @ts-ignore Property 'props' does not exist on type ReactElement + styles.props.children.forEach((child: React.ReactElement) => { + if (Array.isArray(child)) { + child.forEach((el) => hasStyles(el) && curStyles.push(el)) + } else if (hasStyles(child)) { + curStyles.push(child) + } + }) + } + + /* Add custom styles before AMP styles to prevent accidental overrides */ + return ( +