I’m learning next.js with React with tailwind css and was looking to read html static files from the public/assets folder:
import React from 'react';
import fs from 'node:fs';
import parse from 'node-html-parser';
const HTMLComponent = ({ filename } : string) => {
try {
const htmlContent = fs.readFileSync(`./public/assets/html/${filename}`, 'utf-8');
return (
<div dangerouslySetInnerHTML={{ __html: htmlContent }} />
);
} catch (err) {
console.error(`Error reading file: ${err}`);
}
};
export default HTMLComponent;
I then use the component like this:
import HTMLComponent from '@/components/shared/HTMLFile';
export default function Page() {
return (
<div className='p-4'>
<HTMLComponent filename = "index.html" />
</div>
);
}
Even though the index.html is formatted with h1, p and so on; none of that css formatting shows up in the displayed page.
I suspect that dangerouslySetInnerHTML is my nemesis.
Is there a better way to do this and get properly formatted css’d html?
The page has a header (which should import tailwind css)
layout and footer. The included HTMLComponent is rendered on the page as all < p >s even though there are < h1 > tags present.
The generated css has classes like:
@media (min-width: 768px) {
.h1 {
font-size: 36px;
line-height: 44px;
}
}
Your answers are much appreciated!
3
Answers
dangerouslySetInnerHTML
only allows you to insert raw HTML into a component, it doesn’t automatically apply styling from external CSS to the inserted HTML content.Try parsing the HTML content using
node-html-parser
to extract specific elements and apply Tailwind classes directly in your React component:If the external HTML files contain their own CSS, import those css flies within your component using next/head:
Also consider rendering the HTML files on the server using Next.js’s SSR capabilities:
If you know what the class names will be, this is easy in your custom css file.
You can use tailwind as follows:
https://tailwindcss.com/docs/reusing-styles#extracting-components-and-partials
It is not a problem of
dangerouslySetInnerHTML
but the problem is that the tailwind has over ridden it.and to fix it you can use use an
iframe
rather than adiv
like this:This should fix the issue. I hope it helps 🙂