Skip to content

Duplicate styles using SSR in Next.js #43

Description

@Yukioru

Hello.

When configuring styles in SSR in Next.js, I encountered a problem.
After hydrating the styles - the client also inserts its own <style> tags. This results in duplication of styles.

As there is no SSR documentation yet, I implemented using the source code.

Screenshot: image

static async getInitialProps(ctx) {
  const originalRenderPage = ctx.renderPage;
  const renderer = createDOMRenderer();

  ctx.renderPage = () =>
    originalRenderPage({
      enhanceApp: (App) => {
        return function EnhancedApp(props) {
          return (
            <RendererProvider renderer={renderer}>
              <App {...props} />
            </RendererProvider>
          );
        };
      }
    });

  const documentProps = await NextDocument.getInitialProps(ctx);

  documentProps.styles = (
    <>
      {documentProps.styles}
      {renderToStyleElements(renderer)}
    </>
  );

  return documentProps;
}

Reproduced the problem in CodeSandbox.

Maybe this is not a bug and there is a way to disable client initialisation of already hydrated elements

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Fields

    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions