-
-
Notifications
You must be signed in to change notification settings - Fork 2k
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
Uncaught DOMException when having <!DOCTYPE html>
and setting the parentDom
to document
when rendering
#3794
Comments
That is the correct behavior and not a bug as the The second argument passed to render is a DOM node that's expected to be fully owned by Preact. When Preact detects that there are existing nodes there that doesn't match the virtual tree that it was given, we'll try to insert nodes before that. One of those nodes in the doctype and inserting nodes before that is not allowed like the error states. Even in theory, rendering into the Instead, create a container element inside <!DOCTYPE html>
<html>
<body>
+ <div id="app"></div>
+ <script type="module">
+ import { h, Component, render } from 'https://unpkg.com/preact?module';
+
+ const app = h('h1', null, 'Hello World!');
+
+ render(app, document.getElementById("app"));
+ </script>
</body>
</html>
- <script type="module">
- import { h, Component, render } from 'https://unpkg.com/preact?module';
-
- const app = h('html', null, h('body', null, h('h1', null, 'Hello World!')));
-
- render(app, document);
- </script> |
@marvinhagemeister What about this:
Rendering in the |
You don't get this error in React because it aggressively removes any nodes not matching the virtual tree. In this case it drops the doctype node completely which makes the HTML invalid. It works regardless of that because browsers are pretty good with dealing with invalid HTML documents due to quirks mode. The common and recommended approach to doing SSR in both React and Preact is to stitch the document manually. Nodes supposed to be included in the const html = `<!DOCTYPE html>
<html>
<head>
${headNodes}
</head>
<body>
<div id="app>${app}</div>
<script type="module">
// ...app code + hydration here
</script>
</body>
</html>
` |
I know about this way of rendering the head, but what if we wanted to use a state in the head? |
Can you share more about your use case why you need state in the |
I don't have anything in mind right now. It's just super weird to not be able to hydrate the whole document. Everything does that nowadays. |
That doesn't match my experience, but I'm curious to learn more. Can you share some links to projects which do that in the way you're describing? |
Ultra This even supports both React and Preact, but for the Preact support it does not use preact-render-to-string because of this issue, instead it uses ReactDOMServer with preact/compat. https://github.com/exhibitionist-digital/ultra/blob/main/examples/with-react-router/client.tsx#L6 |
@marvinhagemeister Can you please reopen this? |
@marvinhagemeister @JoviDeCroock @andrewiggins Since you're not reopening this, should I open a new issue? |
Shouldn't this be rendering into I'd like to support this in Preact, since Remix is already relying on it. |
Describe the bug
If we have
<!DOCTYPE html>
in our HTML document where we use Preact, and try to render or hydrate a component in thedocument
, we get an Uncaught DOMException error, and the component does not render/hydrate. Although, it'll be fine if we remove<!DOCTYPE html>
. This is very a strange behavior.To Reproduce
Steps to reproduce the behavior:
<!DOCTYPE html>
.Expected behavior
Should act just fine :)
The text was updated successfully, but these errors were encountered: