diff --git a/.changeset/grumpy-kings-flow.md b/.changeset/grumpy-kings-flow.md new file mode 100644 index 00000000..7fc76b9d --- /dev/null +++ b/.changeset/grumpy-kings-flow.md @@ -0,0 +1,5 @@ +--- +'preact-render-to-string': minor +--- + +Introduce a streaming renderer which can be imported from `preact-render-to-string/stream` and `preact-render-to-string/stream-node` diff --git a/jsx.d.ts b/jsx.d.ts index 277bda5a..dd74ffd8 100644 --- a/jsx.d.ts +++ b/jsx.d.ts @@ -15,3 +15,5 @@ export default function render( context?: any, options?: Options ): string; + +export function shallowRender(vnode: VNode, context?: any): string; diff --git a/src/internal.d.ts b/src/internal.d.ts index 26deff6f..8d2959fb 100644 --- a/src/internal.d.ts +++ b/src/internal.d.ts @@ -1,7 +1,4 @@ -// import { VNode } from 'preact'; - -type VNode = import('preact').VNode; -type ComponentChildren = import('preact').ComponentChildren; +import { ComponentChildren, VNode } from 'preact'; interface Suspended { id: string; diff --git a/src/pretty.js b/src/pretty.js index 3079a974..70e9e5a8 100644 --- a/src/pretty.js +++ b/src/pretty.js @@ -9,7 +9,8 @@ import { VOID_ELEMENTS, NAMESPACE_REPLACE_REGEX, SVG_CAMEL_CASE, - HTML_LOWER_CASE + HTML_LOWER_CASE, + getContext } from './lib/util.js'; import { COMMIT, DIFF, DIFFED, RENDER, SKIP_EFFECTS } from './lib/constants.js'; import { options, Fragment } from 'preact'; @@ -122,18 +123,11 @@ function _renderToStringPretty( let renderHook = options[RENDER]; - let cctx = context; - let cxType = nodeName.contextType; - if (cxType != null) { - let provider = context[cxType.__c]; - cctx = provider ? provider.props.value : cxType.__; - } - if ( !nodeName.prototype || typeof nodeName.prototype.render !== 'function' ) { - // let cctx = getContext(nodeName, context); + let cctx = getContext(nodeName, context); // If a hook invokes setState() to invalidate the component during rendering, // re-render it up to 25 times to allow "settling" of memoized states. @@ -150,7 +144,7 @@ function _renderToStringPretty( rendered = nodeName.call(vnode.__c, props, cctx); } } else { - // let cctx = getContext(nodeName, context); + let cctx = getContext(nodeName, context); // c = new nodeName(props, context); c = vnode.__c = new nodeName(props, cctx); diff --git a/src/stream.js b/src/stream.js index 9aee9c8e..181614f0 100644 --- a/src/stream.js +++ b/src/stream.js @@ -34,7 +34,6 @@ export function renderToReadableStream(vnode, context) { controller.error(error); allReady.reject(error); }); - // controller.enqueue(encoder.encode(shell)); } });