refactor: jsx streaming (#2216)
Before adoption: this commit came in with the history the project adopted, so no landing records it.
* refactor(jsx): tweaks loop in order to avoid error for blank content * feat(jsx): add data-hono-target attribute to streaming content * chore: denoify * test: fix runtime_tests/deno-jsx/jsx.test.tsx
authored by Taku Amano · committed by GitHub · parent 89f8e07 · Browse files · 10 files·+25 added−25 removed
This commit
State
Before adoption: this commit came in with the history the project adopted, so no landing records it.
Diff stats
10 files+25 added−25 removed
- tests:+17 added−17 removed
- other:+8 added−8 removed
Provenance
- Commit
473a6f0- Tree
b062029- Author
Taku Amano <taku@taaas.jp>- Committer
GitHub <noreply@github.com>- Parent
89f8e07
10 files changed+25 added−25 removed
modified:deno_dist/jsx/components.ts+3 added-3 removed
77 unmodified lines7879808182828384858612 unmodified lines9910010110210310310410510610752 unmodified lines16016116216316416416516616716817 unmodified lines77 unmodified lines buffer[0] = buffer[0].replace(replaceRe, fallbackResString) } return buffer ? '' : `<template>${fallbackResString}</template><script> : `<template data-hono-target="E:${index}">${fallbackResString}</template><script>((d,c,n) => {c=d.currentScript.previousSiblingd=d.getElementById('E:${index}')if(!d)return12 unmodified lines htmlArray = htmlArray.flat() const content = htmlArray.join('') let html = buffer ? '' : `<template>${content}</template><script> : `<template data-hono-target="E:${index}">${content}</template><script>((d,c) => {c=d.currentScript.previousSiblingd=d.getElementById('E:${index}')if(!d)return52 unmodified lines((d,c,n) => {d=d.getElementById('E:${index}')if(!d)returnn=d.nextSiblingdo{n=n.nextSibling}while(n.nodeType!=8||n.nodeValue!='E:${index}')while(n.nodeType!=8||n.nodeValue!='E:${index}'){n=n.nextSibling}n.remove()d.remove()})(document)</script>`),17 unmodified linesmodified:deno_dist/jsx/streaming.ts+1 added-1 removed
74 unmodified lines75 )76 }77 let html = buffer78 ? ''79- : `<template>${content}</template><script>79+ : `<template data-hono-target="H:${index}">${content}</template><script>80 ((d,c,n) => {81 c=d.currentScript.previousSibling82 d=d.getElementById('H:${index}')83 if(!d)return82 unmodified linesmodified:runtime_tests/deno-jsx/jsx.test.tsx+1 added-1 removed
66 unmodified lines67 }68 69 assertEquals(chunks, [70 '<template id="H:0"></template><p>Loading...</p><!--/$-->',71- `<template><h1>Hello</h1></template><script>71+ `<template data-hono-target="H:0"><h1>Hello</h1></template><script>72 ((d,c,n) => {73 c=d.currentScript.previousSibling74 d=d.getElementById('H:0')75 if(!d)return6 unmodified linesmodified:src/helper/css/index.test.tsx+1 added-1 removed
238 unmodified lines239 it('/stream', async () => {240 const res = await app.request('http://localhost/stream')241 expect(res).not.toBeNull()242 expect(await res.text()).toBe(243- `<style id="hono-css"></style><template id="H:0"></template><p>Loading...</p><!--/$--><script>document.querySelector('#hono-css').textContent+=".css-2458908649{background-color:blue}"</script><template><h1 class="css-2458908649">Hello!</h1></template><script>243+ `<style id="hono-css"></style><template id="H:0"></template><p>Loading...</p><!--/$--><script>document.querySelector('#hono-css').textContent+=".css-2458908649{background-color:blue}"</script><template data-hono-target="H:0"><h1 class="css-2458908649">Hello!</h1></template><script>244 ((d,c,n) => {245 c=d.currentScript.previousSibling246 d=d.getElementById('H:0')247 if(!d)return8 unmodified linesmodified:src/jsx/components.test.tsx+3 added-3 removed
189 unmodified lines190 }191 192 expect(chunks).toEqual([193 `<template id="E:${errorBoundaryCounter}"></template><!--E:${errorBoundaryCounter}-->`,194- `<template><template id="H:${suspenseCounter}"></template><p>Loading...</p><!--/$--></template><script>194+ `<template data-hono-target="E:${errorBoundaryCounter}"><template id="H:${suspenseCounter}"></template><p>Loading...</p><!--/$--></template><script>195 ((d,c) => {196 c=d.currentScript.previousSibling197 d=d.getElementById('E:${errorBoundaryCounter}')198 if(!d)return199 d.parentElement.insertBefore(c.content,d.nextSibling)200 })(document)201 </script>`,202- `<template><div>Hello</div></template><script>202+ `<template data-hono-target="H:${suspenseCounter}"><div>Hello</div></template><script>203 ((d,c,n) => {204 c=d.currentScript.previousSibling205 d=d.getElementById('H:${suspenseCounter}')206 if(!d)return4 unmodified lines211 ((d,c,n) => {212 d=d.getElementById('E:${errorBoundaryCounter}')213 if(!d)return214 n=d.nextSibling215-do{n=n.nextSibling}while(n.nodeType!=8||n.nodeValue!='E:${errorBoundaryCounter}')215+while(n.nodeType!=8||n.nodeValue!='E:${errorBoundaryCounter}'){n=n.nextSibling}216 n.remove()217 d.remove()218 })(document)219 </script>`,259 unmodified linesmodified:src/jsx/components.ts+3 added-3 removed
77 unmodified lines78 buffer[0] = buffer[0].replace(replaceRe, fallbackResString)79 }80 return buffer81 ? ''82- : `<template>${fallbackResString}</template><script>82+ : `<template data-hono-target="E:${index}">${fallbackResString}</template><script>83 ((d,c,n) => {84 c=d.currentScript.previousSibling85 d=d.getElementById('E:${index}')86 if(!d)return12 unmodified lines99 htmlArray = htmlArray.flat()100 const content = htmlArray.join('')101 let html = buffer102 ? ''103- : `<template>${content}</template><script>103+ : `<template data-hono-target="E:${index}">${content}</template><script>104 ((d,c) => {105 c=d.currentScript.previousSibling106 d=d.getElementById('E:${index}')107 if(!d)return52 unmodified lines160 ((d,c,n) => {161 d=d.getElementById('E:${index}')162 if(!d)return163 n=d.nextSibling164-do{n=n.nextSibling}while(n.nodeType!=8||n.nodeValue!='E:${index}')164+while(n.nodeType!=8||n.nodeValue!='E:${index}'){n=n.nextSibling}165 n.remove()166 d.remove()167 })(document)168 </script>`),17 unmodified linesmodified:src/jsx/index.test.tsx+1 added-1 removed
651 unmodified lines652 }653 654 expect(chunks).toEqual([655 '<template id="H:0"></template><span>red</span><!--/$-->',656- `<template><span>dark</span><span>black</span></template><script>656+ `<template data-hono-target="H:0"><span>dark</span><span>black</span></template><script>657 ((d,c,n) => {658 c=d.currentScript.previousSibling659 d=d.getElementById('H:0')660 if(!d)return62 unmodified linesmodified:src/jsx/streaming.test.tsx+9 added-9 removed
42 unmodified lines43 }44 45 expect(chunks).toEqual([46 `<template id="H:${suspenseCounter}"></template><p>Loading...</p><!--/$-->`,47- `<template><h1>Hello</h1></template><script>47+ `<template data-hono-target="H:${suspenseCounter}"><h1>Hello</h1></template><script>48 ((d,c,n) => {49 c=d.currentScript.previousSibling50 d=d.getElementById('H:${suspenseCounter}')51 if(!d)return39 unmodified lines91 }92 93 expect(chunks).toEqual([94 `<template id="H:${suspenseCounter}"></template><p>Loading...</p><!--/$-->`,95- `<template><p>thrown a promise then resolved</p></template><script>95+ `<template data-hono-target="H:${suspenseCounter}"><p>thrown a promise then resolved</p></template><script>96 ((d,c,n) => {97 c=d.currentScript.previousSibling98 d=d.getElementById('H:${suspenseCounter}')99 if(!d)return107 unmodified lines207 }208 209 expect(chunks).toEqual([210 `<template id="H:${suspenseCounter}"></template><p>Loading...</p><!--/$-->`,211- `<template><p></p></template><script>211+ `<template data-hono-target="H:${suspenseCounter}"><p></p></template><script>212 ((d,c,n) => {213 c=d.currentScript.previousSibling214 d=d.getElementById('H:${suspenseCounter}')215 if(!d)return25 unmodified lines241 }242 243 expect(chunks).toEqual([244 `<template id="H:${suspenseCounter}"></template><p>Loading...</p><!--/$-->`,245- `<template><p></p></template><script>245+ `<template data-hono-target="H:${suspenseCounter}"><p></p></template><script>246 ((d,c,n) => {247 c=d.currentScript.previousSibling248 d=d.getElementById('H:${suspenseCounter}')249 if(!d)return65 unmodified lines315 }316 317 expect(chunks).toEqual([318 `<template id="H:${suspenseCounter}"></template><p>Loading...</p><!--/$-->`,319- `<template><h1>Hello</h1><h2>World</h2></template><script>319+ `<template data-hono-target="H:${suspenseCounter}"><h1>Hello</h1><h2>World</h2></template><script>320 ((d,c,n) => {321 c=d.currentScript.previousSibling322 d=d.getElementById('H:${suspenseCounter}')323 if(!d)return37 unmodified lines361 }362 363 expect(chunks).toEqual([364 `<template id="H:${suspenseCounter}"></template>Loading<span>...</span><!--/$-->`,365- `<template><h1>Hello</h1></template><script>365+ `<template data-hono-target="H:${suspenseCounter}"><h1>Hello</h1></template><script>366 ((d,c,n) => {367 c=d.currentScript.previousSibling368 d=d.getElementById('H:${suspenseCounter}')369 if(!d)return47 unmodified lines417 }418 419 expect(chunks).toEqual([420 `<template id="H:${suspenseCounter}"></template><p>Loading...</p><!--/$-->`,421- `<template><h1>Hello</h1><template id=\"H:${421+ `<template data-hono-target="H:${suspenseCounter}"><h1>Hello</h1><template id=\"H:${422 suspenseCounter + 1423 }\"></template><p>Loading sub content...</p><!--/$--></template><script>424 ((d,c,n) => {425 c=d.currentScript.previousSibling2 unmodified lines428 do{n=d.nextSibling;n.remove()}while(n.nodeType!=8||n.nodeValue!='/$')429 d.replaceWith(c.content)430 })(document)431 </script>`,432- `<template><h2>World</h2></template><script>432+ `<template data-hono-target="H:${suspenseCounter + 1}"><h2>World</h2></template><script>433 ((d,c,n) => {434 c=d.currentScript.previousSibling435 d=d.getElementById('H:${suspenseCounter + 1}')436 if(!d)return167 unmodified lines604 }605 606 expect(chunks).toEqual([607 `<template id="H:${suspenseCounter}"></template><p>Loading...</p><!--/$-->`,608- `<template><h1>Hello from use()</h1></template><script>608+ `<template data-hono-target="H:${suspenseCounter}"><h1>Hello from use()</h1></template><script>609 ((d,c,n) => {610 c=d.currentScript.previousSibling611 d=d.getElementById('H:${suspenseCounter}')612 if(!d)return12 unmodified linesmodified:src/jsx/streaming.ts+1 added-1 removed
74 unmodified lines75 )76 }77 let html = buffer78 ? ''79- : `<template>${content}</template><script>79+ : `<template data-hono-target="H:${index}">${content}</template><script>80 ((d,c,n) => {81 c=d.currentScript.previousSibling82 d=d.getElementById('H:${index}')83 if(!d)return82 unmodified linesmodified:src/middleware/jsx-renderer/index.test.tsx+2 added-2 removed
231 unmodified lines232 chunk.push(decoder.decode(value))233 }234 expect(chunk).toEqual([235 '<!DOCTYPE html><html><body><template id="H:0"></template><p>Loading...</p><!--/$--></body></html>',236- `<template><p>Hello Hono!</p></template><script>236+ `<template data-hono-target="H:0"><p>Hello Hono!</p></template><script>237 ((d,c,n) => {238 c=d.currentScript.previousSibling239 d=d.getElementById('H:0')240 if(!d)return60 unmodified lines301 chunk.push(decoder.decode(value))302 }303 expect(chunk).toEqual([304 '<!DOCTYPE html><html><body><template id="H:1"></template><p>Loading...</p><!--/$--></body></html>',305- `<template><p>Hello Hono again!</p></template><script>305+ `<template data-hono-target="H:1"><p>Hello Hono again!</p></template><script>306 ((d,c,n) => {307 c=d.currentScript.previousSibling308 d=d.getElementById('H:1')309 if(!d)return109 unmodified lines