question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

preact rendering <style> tag inside <noscript>, whereas react does not

See original GitHub issue

I’m not sure if this matters to the library maintainers, or how much compatibility/interop with React is a goal, but I noticed a subtle difference between React and Preact that caused a fairly big blocker trying to switch a Gatsby site over to preact.

Basically, this jsx:

<noscript>
  <style dangerouslySetInnerHTML={{ __html: "body { background-color: pink }" }} />
</noscript>

Renders differently between React and preact. In react, you get an empty <noscript> tag. Preact renders out the <style> tag inside the noscript. Codepens below.

The use-case here is that I’m trying to use gatsby-background-image which adds styles to noscript like the example above. With react, nothing renders, but with preact, I get extra style tags which load in un-optimized images, destroying any performance benefit gained from switching to preact.

Reproduction

Codepen with React: https://codepen.io/jaredh159/pen/NWrGyPq?editors=1010

Codepan with Preact: https://codepen.io/jaredh159/pen/mdEeXJg?editors=1010

Note: you’ll have to use the dev tools to inspect to see the <noscript> contents, as it’s not visible, of course.

Steps to reproduce

See codepens above ^^^

Expected Behavior

I would have hoped that preact matched the behavior of react.

Actual Behavior

preact rendered the contents of the <noscript> tag, unlike React.

Issue Analytics

  • State:closed
  • Created 3 years ago
  • Comments:5 (3 by maintainers)

github_iconTop GitHub Comments

2reactions
developitcommented, Oct 22, 2020

Here’s a patch that makes Preact skip noscript contents on the client only:

import { options } from 'preact';
const CLIENT = typeof document !== 'undefined';
const old = options.vnode;
options.vnode = vnode => {
  if (vnode.type === 'noscript' && CLIENT) {
    vnode.props.children = null;
  }
  if (old) old(vnode);
};
0reactions
developitcommented, Oct 22, 2020

@jaredh159 yep! Just have it run before your component code.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Why is there a <noscript> tag in React? - Stack Overflow
Anything within <noscript></noscript> tags will render only when JavaScript is disabled or the browser doesn't support JavaScript.
Read more >
Differences to React - Preact
The main difference between Preact and React is that Preact does not implement a synthetic event system for size and performance reasons. Preact...
Read more >
Using the HTML style Tag to Style a React Component
Let's see how to style a React component in an unconventional way by adopting the HTML.
Read more >
LitElement To Do App. And, how it compares to React as well…
The style import exports a TemplateResult with a full `<style/>` element for application in any other TemplateResult. Above you have the styles ......
Read more >
MERN-Stack Setup: Building a Reading List Web App with ...
Tagged with typescript, showdev, tailwindcss, javascript. ... In the past, I had worked with legacy code or did not take the time for...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found