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.

Invalid HTML inside `dangerouslySetInnerHTML` breaks the page.

See original GitHub issue

Bug report

Describe the bug

If invalid HTML is added to dangerouslySetInnerHTML, Next.js will output a blank page without providing any feedback. This can be hard to track when working with a CMS provider or markdown files.

To Reproduce

Steps to reproduce the behavior, please provide code snippets or a repository:

  1. Clone https://github.com/lfades/nextjs-inner-html-bug
  2. Run yarn && yarn dev or npm i && npm run dev
  3. See that pages/index.js is a blank page with no errors

Expected behavior

Invalid HTML inside dangerouslySetInnerHTML should throw and/or let the user know that there’s something wrong.

The demo also has an index.html and index.js in the root directory that shows how the same code works in React alone, it doesn’t produce an error either, but it shows the content.

Issue Analytics

  • State:open
  • Created 3 years ago
  • Reactions:3
  • Comments:10 (1 by maintainers)

github_iconTop GitHub Comments

1reaction
Christopher-Steverscommented, Jan 19, 2022

I’ll take this one on.

1reaction
sannamarcommented, Sep 3, 2020

I don’t have time to continue on this issue, so if anyone else wants to continue - feel free to do so!

My idea is to validate the HTML with perhaps html-validator package and logging the error to the console. I think adding something similiar to this in Main component could work:

export function Main() {
  const { inAmpMode, html, docComponentsRendered } = useContext(
    DocumentComponentContext
  )
+  const [invalidHtmlError, setInvalidHtmlError] = useState()

  docComponentsRendered.Main = true

+  useEffect(() => {
+    checkHtml()
+  })

+  const checkHtml = async () => {
+   try {
+      await htmlValidator(html)
+    } catch (error) {
+      setInvalidHtmlError(error)
+    }
+  }

  if (inAmpMode) return <>{AMP_RENDER_TARGET}</>

+  if (invalidHtmlError) {
+    Log.error(
+      "There's  an error with your HTML",
+      JSON.stringify(invalidHtmlError)
+    )
+  }

  return <div id="__next" dangerouslySetInnerHTML={{ __html: html }} />
}
Read more comments on GitHub >

github_iconTop Results From Across the Web

Does dangerouslySetInnerHTML prevent invalid HTML from ...
I am trying to figure out whether a user might break their website by providing invalid HTML input, but have been unsuccessful so...
Read more >
Using dangerouslySetInnerHTML in a React application
Learn why you should use the dangerouslySetInnerHTML property in your React application and how to apply it safely within your code.
Read more >
Using dangerouslySetInnerHTML - Getting Started with DOM
Here is the conclusion: using the React dangerouslySetInnerHTML is great for rendering HTML markups on-page, especially dynamic ones that can be ...
Read more >
Dangerously Set innerHTML | React
The prop name dangerouslySetInnerHTML is intentionally chosen to be ... a new object containing only the key __html and your sanitized data as...
Read more >
warning: prop `dangerouslysetinnerhtml` did not match. - You.com ...
If it does not, then try to fix other invalid html markups. ... Link component it works fine almost every time but if...
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