diff --git a/src/render.js b/src/render.js
index e0fec45424..2443b0e363 100644
--- a/src/render.js
+++ b/src/render.js
@@ -12,6 +12,11 @@ import { slice } from './util';
* existing DOM tree rooted at `replaceNode`
*/
export function render(vnode, parentDom, replaceNode) {
+ // https://github.com/preactjs/preact/issues/3794
+ if (parentDom === document) {
+ parentDom = document.documentElement;
+ }
+
if (options._root) options._root(vnode, parentDom);
// We abuse the `replaceNode` parameter in `hydrate()` to signal if we are in
diff --git a/test/_util/helpers.js b/test/_util/helpers.js
index 2f2bcecdfa..ff3fbbf959 100644
--- a/test/_util/helpers.js
+++ b/test/_util/helpers.js
@@ -208,6 +208,8 @@ export function clearOptions() {
* @param {HTMLElement} scratch
*/
export function teardown(scratch) {
+ if (!document.contains(scratch)) return;
+
if (
scratch &&
('__k' in scratch || '_children' in scratch) &&
diff --git a/test/browser/render.test.js b/test/browser/render.test.js
index cac7c7c255..d1b9427512 100644
--- a/test/browser/render.test.js
+++ b/test/browser/render.test.js
@@ -1,5 +1,5 @@
import { setupRerender } from 'preact/test-utils';
-import { createElement, render, Component, options } from 'preact';
+import { createElement, render, Component, options, Fragment } from 'preact';
import {
setupScratch,
teardown,
@@ -1884,4 +1884,22 @@ describe('render()', () => {
);
}
});
+
+ it('should work with document', () => {
+ document.textContent = '';
+ const App = () => (
+ Test
Test
\n' + ); + }); });