Allow rendering in alternative (not browser) DOM implementation.
See original GitHub issueI know that there is a preact-render-to -string project, but it have some limitations with accessing to real dom/document/window ect. from component, also async rendering is not trivial. There are few implementations of DOM for node.js eg domino. This will be very useful to have possibility to render directly to such implementations instead of render to string.
The required change is only to remove reference to global document object and replace it by using parent.ownerDocument. I saw there is only one on two places (createNode) in code which are using global document object. This should be very trivial work.
After such change there should be possibility to :
import * as domino from domino;
import { h, render} from 'preact';
var window = domino.createWindow();
var document = window.document;
render((
<div id="foo">
<span>Hello, world!</span>
<button onClick={ e => alert("hi!") }>Click Me</button>
</div>
), document.body);
Best Regards.
Issue Analytics
- State:
- Created 7 years ago
- Comments:7 (3 by maintainers)
Top Results From Across the Web
Is a browser obliged to use a DOM to render an HTML page?
A Web browser is not obliged to use DOM in order to render an HTML document. You can find the entire context on...
Read more >capricorn86/happy-dom - GitHub
The goal of Happy DOM is to emulate enough of a web browser to be useful for testing, scraping web sites and server-side...
Read more >101 Javascript Critical Rendering Path - inDepth.dev
First things first, every webpage has a Document Object Model or a DOM. This is an object-based representation of the entire HTML page,...
Read more >[Proposal] (Another) Alternative to HTML - Architecture - WICG
Also, I am proposing an alternative to HTML, NOT a replacement. ... the browser attempts to use it's default facilities to render the...
Read more >Declarative Shadow DOM - web.dev
Declarative Shadow DOM is a new way to implement and use Shadow DOM directly in HTML.
Read more >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
Ok I found better workaround. I’m able to do this by the thread local storage (eg. Zone.js)
Regards.
I still don’t think this is something Preact will ever account for. It does not store or rely on any global state in the document, so there is absolutely no difference between passing
render()
a new document each time, or letting it share one. Furthermore, Preact (currently) recycles DOM elements - this would mean it is reusing DOM elements from onedomino
instance inside anotherdomino
instance - probably not something you want, and unlikely somethingdomino
properly accounts for (unless you’re callingdocument.importNode
, but even then…I don’t intend to pursue overriding the document global, and I think I’ve given fairly good justification for taking that position. Preact is a singleton by design, passing in a
document
reference doesn’t change anything about that. All it does is increase complexity in order to superficially support a use-case, without actually doing so. Think about the real implications of this: caching/recycling, creation, removal, etc - all of these must then take into account the fact that the document is not a global.BTW - why do you need to use
document.querySelector()
in the first place? Just pass a new root element and query within that: