next.js icon indicating copy to clipboard operation
next.js copied to clipboard

Using styled-jsx inside of 'next/head' fails on first request

Open FrancosLab opened this issue 9 years ago • 2 comments

Using styled-jsx in the following nature does not render properly on first render, they're basically missing on first initial request but present on following requests (hitting refresh or visiting it again)

yarn run dev -> running on localhost:3000 -> first visit = styles missing from rendered content -> any consecutive visit = styles are present

yarn run build & yarn run start -> first visit = styles missing from rendered content -> any consecutive visit = styles are present

Omitted styles to prevent long post.

import Head from 'next/head'

export default () => {
  return (
      <Head>
        <title>Title</title>
        <meta name="viewport" content="initial-scale=1.0, width=device-width" />
        <style jsx global>
        {`...styles`}
        </style>
      </Head>
  )

However this works fine and although a solution, still seems like styled-jsx should work here.

import Head from 'next/head'

export default () => {
  return (
      <Head>
        <title>title</title>
        <meta name="viewport" content="initial-scale=1.0, width=device-width" />
        <style dangerouslySetInnerHTML={globalStyles()}>
        </style>
      </Head>
  )
}

Note: I am using the master branch but can verify bug present in latest beta.

FrancosLab avatar Dec 29 '16 05:12 FrancosLab

Hi @FrancosLab, this is due to the way the <Head> component is handled. It's not a usual React component since it doesn't renders its children but instead injects them into the head.

The way styled-jsx works is by injecting the styles on render of the component.

Haven't tested, but this should work for you:

import Head from 'next/head'

export default () => {
  return (
      <div>
        <Head>
          <title>SkysLimo - San Jose Limo Service</title>
          <meta name="viewport" content="initial-scale=1.0, width=device-width" />
        </Head>
        <style jsx global>
          {`...styles`}
        </style>
      </div>
  )
}

timneutkens avatar Dec 29 '16 08:12 timneutkens

Exactly. I'll close it for now but feel free to reopen for more questions

impronunciable avatar Dec 29 '16 20:12 impronunciable