Skip to content

Commit

Permalink
Add ignore flag for ssr unreliable selectors warning
Browse files Browse the repository at this point in the history
Co-Authored-By: Brent Ertz <[email protected]>
  • Loading branch information
Kyle Gach and Brent Ertz committed Feb 21, 2019
1 parent a7dfb87 commit 601eeae
Show file tree
Hide file tree
Showing 3 changed files with 524 additions and 39 deletions.
35 changes: 22 additions & 13 deletions packages/cache/src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -201,21 +201,30 @@ let createCache = (options?: Options): EmotionCache => {

stylis.use((context, content, selectors) => {
switch (context) {
case 2: {
for (let i = 0, len = selectors.length; len > i; i++) {
// :last-child isn't included here since it's safe
// because a style element will never be the last element
let match = selectors[i].match(/:(first|nth|nth-last)-child/)
if (match !== null) {
console.error(
`The pseudo class "${
match[0]
}" is potentially unsafe when doing server-side rendering. Try changing it to "${
match[1]
}-of-type"`
case -1: {
const flag =
'emotion-disable-server-rendering-unsafe-selector-warning-please-do-not-use-this-the-warning-exists-for-a-reason'
const unsafePseudoClasses = content.match(
/(:first|:nth|:nth-last)-child/g
)

if (unsafePseudoClasses) {
unsafePseudoClasses.forEach(unsafePseudoClass => {
const ignoreRegExp = new RegExp(
`${unsafePseudoClass}(\\(\\d\\))? \\/\\* ${flag} \\*\\/`
)
}
const ignore = ignoreRegExp.test(content)

if (unsafePseudoClass && !ignore) {
console.error(
`The pseudo class "${unsafePseudoClass}" is potentially unsafe when doing server-side rendering. Try changing it to "${
unsafePseudoClass.split('-child')[0]
}-of-type".`
)
}
})
}

break
}
}
Expand Down
Loading

0 comments on commit 601eeae

Please sign in to comment.