Skip to content
Myr

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

10 files+25 added−25 removed

modified:deno_dist/jsx/components.ts+3 added-3 removed
modified:deno_dist/jsx/streaming.ts+1 added-1 removed
74 unmodified lines
75 )76 }77 let html = buffer78 ? ''79 : `<template>${content}</template><script>79 : `<template data-hono-target="H:${index}">${content}</template><script>80((d,c,n) => {81c=d.currentScript.previousSibling82d=d.getElementById('H:${index}')83if(!d)return
82 unmodified lines
modified:runtime_tests/deno-jsx/jsx.test.tsx+1 added-1 removed
66 unmodified lines
67 }6869 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) => {73c=d.currentScript.previousSibling74d=d.getElementById('H:0')75if(!d)return
6 unmodified lines
modified:src/helper/css/index.test.tsx+1 added-1 removed
238 unmodified lines
239 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) => {245c=d.currentScript.previousSibling246d=d.getElementById('H:0')247if(!d)return
8 unmodified lines
modified:src/jsx/components.test.tsx+3 added-3 removed
189 unmodified lines
190 }191192 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) => {196c=d.currentScript.previousSibling197d=d.getElementById('E:${errorBoundaryCounter}')198if(!d)return199d.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) => {204c=d.currentScript.previousSibling205d=d.getElementById('H:${suspenseCounter}')206if(!d)return
4 unmodified lines
211((d,c,n) => {212d=d.getElementById('E:${errorBoundaryCounter}')213if(!d)return214n=d.nextSibling215do{n=n.nextSibling}while(n.nodeType!=8||n.nodeValue!='E:${errorBoundaryCounter}')215while(n.nodeType!=8||n.nodeValue!='E:${errorBoundaryCounter}'){n=n.nextSibling}216n.remove()217d.remove()218})(document)219</script>`,
259 unmodified lines
modified:src/jsx/components.ts+3 added-3 removed
77 unmodified lines
78 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) => {84c=d.currentScript.previousSibling85d=d.getElementById('E:${index}')86if(!d)return
12 unmodified lines
99 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) => {105c=d.currentScript.previousSibling106d=d.getElementById('E:${index}')107if(!d)return
52 unmodified lines
160((d,c,n) => {161d=d.getElementById('E:${index}')162if(!d)return163n=d.nextSibling164do{n=n.nextSibling}while(n.nodeType!=8||n.nodeValue!='E:${index}')164while(n.nodeType!=8||n.nodeValue!='E:${index}'){n=n.nextSibling}165n.remove()166d.remove()167})(document)168</script>`),
17 unmodified lines
modified:src/jsx/index.test.tsx+1 added-1 removed
651 unmodified lines
652 }653654 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) => {658c=d.currentScript.previousSibling659d=d.getElementById('H:0')660if(!d)return
62 unmodified lines
modified:src/jsx/streaming.test.tsx+9 added-9 removed
42 unmodified lines
43 }4445 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) => {49c=d.currentScript.previousSibling50d=d.getElementById('H:${suspenseCounter}')51if(!d)return
39 unmodified lines
91 }9293 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) => {97c=d.currentScript.previousSibling98d=d.getElementById('H:${suspenseCounter}')99if(!d)return
107 unmodified lines
207 }208209 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) => {213c=d.currentScript.previousSibling214d=d.getElementById('H:${suspenseCounter}')215if(!d)return
25 unmodified lines
241 }242243 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) => {247c=d.currentScript.previousSibling248d=d.getElementById('H:${suspenseCounter}')249if(!d)return
65 unmodified lines
315 }316317 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) => {321c=d.currentScript.previousSibling322d=d.getElementById('H:${suspenseCounter}')323if(!d)return
37 unmodified lines
361 }362363 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) => {367c=d.currentScript.previousSibling368d=d.getElementById('H:${suspenseCounter}')369if(!d)return
47 unmodified lines
417 }418419 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) => {425c=d.currentScript.previousSibling
2 unmodified lines
428do{n=d.nextSibling;n.remove()}while(n.nodeType!=8||n.nodeValue!='/$')429d.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) => {434c=d.currentScript.previousSibling435d=d.getElementById('H:${suspenseCounter + 1}')436if(!d)return
167 unmodified lines
604 }605606 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) => {610c=d.currentScript.previousSibling611d=d.getElementById('H:${suspenseCounter}')612if(!d)return
12 unmodified lines
modified:src/jsx/streaming.ts+1 added-1 removed
74 unmodified lines
75 )76 }77 let html = buffer78 ? ''79 : `<template>${content}</template><script>79 : `<template data-hono-target="H:${index}">${content}</template><script>80((d,c,n) => {81c=d.currentScript.previousSibling82d=d.getElementById('H:${index}')83if(!d)return
82 unmodified lines
modified:src/middleware/jsx-renderer/index.test.tsx+2 added-2 removed
231 unmodified lines
232 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) => {238c=d.currentScript.previousSibling239d=d.getElementById('H:0')240if(!d)return
60 unmodified lines
301 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) => {307c=d.currentScript.previousSibling308d=d.getElementById('H:1')309if(!d)return
109 unmodified lines