Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allowing components to be SSR-friendly by changing pseudo class ":first-child" to ":first-of-type" #158

Closed
cheton opened this issue Jun 11, 2020 · 4 comments
Labels
👷 refactor (improvement) Improvements in the code base

Comments

@cheton
Copy link
Member

cheton commented Jun 11, 2020

A warning message reported by Next.js that the pseudo class :first-child is potentially unsafe when doing server-side rendering. Try changing it to :first-of-type.

image

Here are the component list that use the pseudo class ":first-child". It's suggested that we replace it with ":first-of-type" to make components to be SSR-friendly.

src/Checkbox/styles.js:      '& > :first-child': {
src/Input/styles.js:    '&:not(:first-child)': {
src/InputGroupAppend/styles.js:    '& > *:first-child': notFirstChildStyle,
src/InputGroupAppend/styles.js:    '&:not(:last-child) > *:first-child': notLastChildStyle,
src/InputGroupPrepend/styles.js:    '& > *:first-child': notLastChildStyle,
src/InputGroupPrepend/styles.js:    '&:not(:first-child) > *:first-child': notFirstChildStyle,
src/Modal/ModalFooter.js:        '&:first-child': {
src/Modal/ModalBody.js:        '&:first-child': {
@cheton cheton changed the title Change pseudo class ":first-child" to ":first-of-type" for SSR friendly Change pseudo class ":first-child" to ":first-of-type" that allows components to be SSR-friendly Jun 11, 2020
@cheton cheton added the 👷 refactor (improvement) Improvements in the code base label Jun 11, 2020
@cheton cheton changed the title Change pseudo class ":first-child" to ":first-of-type" that allows components to be SSR-friendly Allowing components to be SSR-friendly by changing pseudo class ":first-child" to ":first-of-type" Jun 13, 2020
@albertshala
Copy link

albertshala commented Oct 29, 2020

Hi @cheton, In Narnia, we have also seen this issue happen when using checkbox and modal components as well, for the modal component in particular, the above error happens only the first time the click event to open the modal occurs, all subsequent events to open or close the modal do not produce any error.

Note: The error is hidden when build in production mode via webpack.

Screen Shot 2020-10-29 at 5 48 44 PM

issues-with-tonic-modal

@cheton
Copy link
Member Author

cheton commented Nov 5, 2020

It will be fixed in PR #253

@cheton
Copy link
Member Author

cheton commented Nov 6, 2020

@cheton cheton closed this as completed Nov 6, 2020
@cheton
Copy link
Member Author

cheton commented Jul 14, 2021

See emotion-js/emotion#1105 (comment) for details.

Add emotion-disable-server-rendering-unsafe-selector-warning-please-do-not-use-this-the-warning-exists-for-a-reason to ignore warning messages (packages/cache/src/stylis-plugins.js)

Option 1

css`
  :first-child /* emotion-disable-server-rendering-unsafe-selector-warning-please-do-not-use-this-the-warning-exists-for-a-reason */ {
    color: hotpink;
  }
`

Option 2

import { CacheProvider } from '@emotion/core'
import createCache from '@emotion/cache'

const myCache = createCache()
myCache.compat = true

<CacheProvider value={myCache}>
  <App/>
</CacheProvider>

Option 3

import { cache } from 'emotion'

<CacheProvider value={cache}>
  <App/>
</CacheProvider>

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
👷 refactor (improvement) Improvements in the code base
Projects
None yet
Development

No branches or pull requests

2 participants