-
-
Notifications
You must be signed in to change notification settings - Fork 92
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
feat: use comments instead of element as marker #260
Merged
marvinhagemeister
merged 5 commits into
preactjs:streaming-render
from
jacob-ebey:comments-ce-script
Nov 8, 2022
Merged
Changes from 1 commit
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
a84fca4
feat: use comments instead of element as marker
jacob-ebey d61cda8
chore: use NodeIterator to locate comments
jacob-ebey bca7c53
chore: remove redundancy and minify code
jacob-ebey 31f1839
more minification
jacob-ebey 3ad1d62
even more minification
jacob-ebey File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,61 @@ | ||
import { PassThrough } from 'node:stream'; | ||
|
||
import { renderToChunks } from './index'; | ||
|
||
/** | ||
* @typedef {object} RenderToPipeableStreamOptions | ||
* @property {() => void} [onShellReady] | ||
* @property {() => void} [onAllReady] | ||
* @property {() => void} [onError] | ||
*/ | ||
|
||
/** | ||
* @param {VNode} vnode | ||
* @param {RenderToPipeableStreamOptions} options | ||
* @param {any} [context] | ||
* @returns {{}} | ||
*/ | ||
export function renderToPipeableStream(vnode, options, context) { | ||
const encoder = new TextEncoder('utf-8'); | ||
|
||
const controller = new AbortController(); | ||
const stream = new PassThrough(); | ||
|
||
renderToChunks(vnode, { | ||
context, | ||
abortSignal: controller.signal, | ||
onError: (error) => { | ||
if (options.onError) { | ||
options.onError(error); | ||
} | ||
controller.abort(error); | ||
}, | ||
onWrite(s) { | ||
stream.write(encoder.encode(s)); | ||
} | ||
}) | ||
.then(() => { | ||
options.onAllReady && options.onAllReady(); | ||
stream.end(); | ||
}) | ||
.catch((error) => { | ||
stream.destroy(error); | ||
}); | ||
|
||
Promise.resolve().then(() => { | ||
options.onShellReady && options.onShellReady(); | ||
}); | ||
|
||
return { | ||
abort() { | ||
controller.abort(); | ||
stream.destroy(new Error('aborted')); | ||
}, | ||
/** | ||
* @param {import("stream").Writable} writable | ||
*/ | ||
pipe(writable) { | ||
stream.pipe(writable, { end: true }); | ||
} | ||
}; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,35 +1,76 @@ | ||
/** | ||
* @param {ReadableStream} input | ||
*/ | ||
function createSink(input) { | ||
const decoder = new TextDecoder('utf-8'); | ||
const queuingStrategy = new CountQueuingStrategy({ highWaterMark: 1 }); | ||
import { PassThrough } from 'node:stream'; | ||
import { h } from 'preact'; | ||
import { expect } from 'chai'; | ||
import { Suspense } from 'preact/compat'; | ||
import { createSubtree, createInitScript } from '../src/client'; | ||
import { renderToPipeableStream } from '../src/stream-node'; | ||
import { Deferred } from '../src/util'; | ||
import { createSuspender } from './utils'; | ||
|
||
function streamToString(stream) { | ||
const decoder = new TextDecoder(); | ||
const def = new Deferred(); | ||
const result = []; | ||
|
||
const stream = new WritableStream( | ||
{ | ||
// Implement the sink | ||
write(chunk) { | ||
result.push(decoder.decode(chunk)); | ||
}, | ||
close() { | ||
def.resolve(result); | ||
}, | ||
abort(err) { | ||
def.reject(err); | ||
} | ||
}, | ||
queuingStrategy | ||
); | ||
stream.on('data', (chunk) => { | ||
chunks.push(decoder.decode(chunk)); | ||
}); | ||
stream.on('error', (err) => def.reject(err)); | ||
stream.on('end', () => def.resolve(chunks)); | ||
const chunks = []; | ||
return def; | ||
} | ||
|
||
input.pipeTo(stream); | ||
/** | ||
* @param {ReadableStream} input | ||
*/ | ||
function createSink() { | ||
const stream = new PassThrough(); | ||
const def = streamToString(stream); | ||
|
||
return { | ||
promise: def.promise, | ||
stream | ||
}; | ||
} | ||
|
||
describe('', () => {}); | ||
describe('renderToPipeableStream', () => { | ||
it('should render non-suspended JSX in one go', async () => { | ||
const sink = createSink(); | ||
const { pipe } = renderToPipeableStream(<div class="foo">bar</div>, { | ||
onAllReady: () => { | ||
pipe(sink.stream); | ||
} | ||
}); | ||
const result = await sink.promise; | ||
|
||
expect(result).to.deep.equal(['<div class="foo">bar</div>']); | ||
}); | ||
|
||
it('should render fallback + attach loaded subtree on suspend', async () => { | ||
const { Suspender, suspended } = createSuspender(); | ||
|
||
const sink = createSink(); | ||
const { pipe } = renderToPipeableStream( | ||
<div> | ||
<Suspense fallback="loading..."> | ||
<Suspender /> | ||
</Suspense> | ||
</div>, | ||
{ | ||
onShellReady: () => { | ||
pipe(sink.stream); | ||
} | ||
} | ||
); | ||
suspended.resolve(); | ||
|
||
const result = await sink.promise; | ||
|
||
expect(result).to.deep.equal([ | ||
'<div><!--preact-island:00-->loading...<!--/preact-island:00--></div>', | ||
'<div hidden>', | ||
createInitScript(), | ||
createSubtree('00', '<p>it works</p>'), | ||
'</div>' | ||
]); | ||
}); | ||
}); |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Is there a better way to get HTML comments than this?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Ok, this should be more performant now: https://github.com/preactjs/preact-render-to-string/pull/260/files#diff-627fe08d8bba6a4fa76e7c5ed36ef6f16d126733e65e19236b391de0d34f1fe0R14
Decided to go with a NodeIterator. TIL
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
If we are worried about perf we can switch to TreeWalker, but NodeIterator has better browser support if I remember right:
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
More benchmarks if we want to switch:
Chome:
Safari:
Walker with a filter is also significantly faster than without and that assumption holds true for iterator as well:
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Here's the source for the benchmark: