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

Fix server error by updating Next.js Webpack config #2

Merged
merged 3 commits into from
Jun 29, 2023

Conversation

remarkablemark
Copy link
Owner

@remarkablemark remarkablemark commented Jun 29, 2023

What is the motivation for this pull request?

Fix server error by updating Next.js Webpack config

Fixes #1

Relates to remarkablemark/html-react-parser#736 (comment)

What is the current behavior?

Server error: http://localhost:3000/api/og

image

What is the new behavior?

Page renders: http://localhost:3000/api/og

Screen Shot 2023-06-28 at 10 49 09 PM

Checklist:

```
Error: Expected <div> to have explicit "display: flex" or "display: none" if it has more than one child node.
```
@remarkablemark remarkablemark added the bug Something isn't working label Jun 29, 2023
@remarkablemark remarkablemark self-assigned this Jun 29, 2023
@remarkablemark remarkablemark merged commit 0035178 into main Jun 29, 2023
@remarkablemark remarkablemark deleted the fix/error branch June 29, 2023 02:50
Comment on lines +10 to +15
test: /html-react-parser\/index\.js$/,
resolve: {
alias: {
'html-dom-parser': path.join(path.dirname(require.resolve('html-dom-parser')), 'lib/server/html-to-dom.js'),
},
},
Copy link
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Without setting this alias, Vercel/Webpack/PNPM tries to resolve the html-dom-parser client module (probably due to the browser field), which seems like a bug on their end

@@ -11,6 +11,7 @@
},
"dependencies": {
"@vercel/og": "latest",
"html-dom-parser": "^4.0.0",
Copy link
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Although not necessary, I installed html-dom-parser explicitly so I could do require.resolve('html-dom-parser') to get the module path

Alternatively, you can get the path with something like but it's less deterministic and can break when version is changed:

path.join(__dirname, 'node_modules/.pnpm/[email protected]/html-dom-parser/lib/server/html-to-dom.js')


export const config = {
runtime: 'edge',
}

export default async function handler() {
const parsedText = parse('<div><p>Hello, World</p></div>')
const parsedText = parse('<div style="display: flex;"><p>Hello, World</p></div>')
Copy link
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I fixed the error:

Expected <div> to have explicit "display: flex" or "display: none" if it has more than one child node.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Error: This browser does not support document.implementation.createHTMLDocument
1 participant