renderToStream.spec.ts 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622
  1. import {
  2. createApp,
  3. h,
  4. createCommentVNode,
  5. withScopeId,
  6. resolveComponent,
  7. ComponentOptions,
  8. ref,
  9. defineComponent,
  10. createTextVNode,
  11. createStaticVNode
  12. } from 'vue'
  13. import { escapeHtml } from '@vue/shared'
  14. import { renderToStream as _renderToStream } from '../src/renderToStream'
  15. import { Readable } from 'stream'
  16. import { ssrRenderSlot } from '../src/helpers/ssrRenderSlot'
  17. import { ssrRenderComponent } from '../src/helpers/ssrRenderComponent'
  18. const promisifyStream = (stream: Readable) => {
  19. return new Promise((resolve, reject) => {
  20. let result = ''
  21. stream.on('data', data => {
  22. result += data
  23. })
  24. stream.on('error', () => {
  25. reject(result)
  26. })
  27. stream.on('end', () => {
  28. resolve(result)
  29. })
  30. })
  31. }
  32. const renderToStream = (app: any, context?: any) =>
  33. promisifyStream(_renderToStream(app, context))
  34. describe('ssr: renderToStream', () => {
  35. test('should apply app context', async () => {
  36. const app = createApp({
  37. render() {
  38. const Foo = resolveComponent('foo') as ComponentOptions
  39. return h(Foo)
  40. }
  41. })
  42. app.component('foo', {
  43. render: () => h('div', 'foo')
  44. })
  45. const html = await renderToStream(app)
  46. expect(html).toBe(`<div>foo</div>`)
  47. })
  48. describe('components', () => {
  49. test('vnode components', async () => {
  50. expect(
  51. await renderToStream(
  52. createApp({
  53. data() {
  54. return { msg: 'hello' }
  55. },
  56. render(this: any) {
  57. return h('div', this.msg)
  58. }
  59. })
  60. )
  61. ).toBe(`<div>hello</div>`)
  62. })
  63. test('option components returning render from setup', async () => {
  64. expect(
  65. await renderToStream(
  66. createApp({
  67. setup() {
  68. const msg = ref('hello')
  69. return () => h('div', msg.value)
  70. }
  71. })
  72. )
  73. ).toBe(`<div>hello</div>`)
  74. })
  75. test('setup components returning render from setup', async () => {
  76. expect(
  77. await renderToStream(
  78. createApp(
  79. defineComponent(() => {
  80. const msg = ref('hello')
  81. return () => h('div', msg.value)
  82. })
  83. )
  84. )
  85. ).toBe(`<div>hello</div>`)
  86. })
  87. test('optimized components', async () => {
  88. expect(
  89. await renderToStream(
  90. createApp({
  91. data() {
  92. return { msg: 'hello' }
  93. },
  94. ssrRender(ctx, push) {
  95. push(`<div>${ctx.msg}</div>`)
  96. }
  97. })
  98. )
  99. ).toBe(`<div>hello</div>`)
  100. })
  101. describe('template components', () => {
  102. test('render', async () => {
  103. expect(
  104. await renderToStream(
  105. createApp({
  106. data() {
  107. return { msg: 'hello' }
  108. },
  109. template: `<div>{{ msg }}</div>`
  110. })
  111. )
  112. ).toBe(`<div>hello</div>`)
  113. })
  114. test('handle compiler errors', async () => {
  115. await renderToStream(createApp({ template: `<` }))
  116. expect(
  117. 'Template compilation error: Unexpected EOF in tag.\n' +
  118. '1 | <\n' +
  119. ' | ^'
  120. ).toHaveBeenWarned()
  121. })
  122. })
  123. test('nested vnode components', async () => {
  124. const Child = {
  125. props: ['msg'],
  126. render(this: any) {
  127. return h('div', this.msg)
  128. }
  129. }
  130. expect(
  131. await renderToStream(
  132. createApp({
  133. render() {
  134. return h('div', ['parent', h(Child, { msg: 'hello' })])
  135. }
  136. })
  137. )
  138. ).toBe(`<div>parent<div>hello</div></div>`)
  139. })
  140. test('nested optimized components', async () => {
  141. const Child = {
  142. props: ['msg'],
  143. ssrRender(ctx: any, push: any) {
  144. push(`<div>${ctx.msg}</div>`)
  145. }
  146. }
  147. expect(
  148. await renderToStream(
  149. createApp({
  150. ssrRender(_ctx, push, parent) {
  151. push(`<div>parent`)
  152. push(ssrRenderComponent(Child, { msg: 'hello' }, null, parent))
  153. push(`</div>`)
  154. }
  155. })
  156. )
  157. ).toBe(`<div>parent<div>hello</div></div>`)
  158. })
  159. test('nested template components', async () => {
  160. const Child = {
  161. props: ['msg'],
  162. template: `<div>{{ msg }}</div>`
  163. }
  164. const app = createApp({
  165. template: `<div>parent<Child msg="hello" /></div>`
  166. })
  167. app.component('Child', Child)
  168. expect(await renderToStream(app)).toBe(
  169. `<div>parent<div>hello</div></div>`
  170. )
  171. })
  172. test('mixing optimized / vnode / template components', async () => {
  173. const OptimizedChild = {
  174. props: ['msg'],
  175. ssrRender(ctx: any, push: any) {
  176. push(`<div>${ctx.msg}</div>`)
  177. }
  178. }
  179. const VNodeChild = {
  180. props: ['msg'],
  181. render(this: any) {
  182. return h('div', this.msg)
  183. }
  184. }
  185. const TemplateChild = {
  186. props: ['msg'],
  187. template: `<div>{{ msg }}</div>`
  188. }
  189. expect(
  190. await renderToStream(
  191. createApp({
  192. ssrRender(_ctx, push, parent) {
  193. push(`<div>parent`)
  194. push(
  195. ssrRenderComponent(OptimizedChild, { msg: 'opt' }, null, parent)
  196. )
  197. push(
  198. ssrRenderComponent(VNodeChild, { msg: 'vnode' }, null, parent)
  199. )
  200. push(
  201. ssrRenderComponent(
  202. TemplateChild,
  203. { msg: 'template' },
  204. null,
  205. parent
  206. )
  207. )
  208. push(`</div>`)
  209. }
  210. })
  211. )
  212. ).toBe(
  213. `<div>parent<div>opt</div><div>vnode</div><div>template</div></div>`
  214. )
  215. })
  216. test('nested components with optimized slots', async () => {
  217. const Child = {
  218. props: ['msg'],
  219. ssrRender(ctx: any, push: any, parent: any) {
  220. push(`<div class="child">`)
  221. ssrRenderSlot(
  222. ctx.$slots,
  223. 'default',
  224. { msg: 'from slot' },
  225. () => {
  226. push(`fallback`)
  227. },
  228. push,
  229. parent
  230. )
  231. push(`</div>`)
  232. }
  233. }
  234. expect(
  235. await renderToStream(
  236. createApp({
  237. ssrRender(_ctx, push, parent) {
  238. push(`<div>parent`)
  239. push(
  240. ssrRenderComponent(
  241. Child,
  242. { msg: 'hello' },
  243. {
  244. // optimized slot using string push
  245. default: ({ msg }: any, push: any) => {
  246. push(`<span>${msg}</span>`)
  247. },
  248. // important to avoid slots being normalized
  249. _: 1 as any
  250. },
  251. parent
  252. )
  253. )
  254. push(`</div>`)
  255. }
  256. })
  257. )
  258. ).toBe(
  259. `<div>parent<div class="child">` +
  260. `<!--[--><span>from slot</span><!--]-->` +
  261. `</div></div>`
  262. )
  263. // test fallback
  264. expect(
  265. await renderToStream(
  266. createApp({
  267. ssrRender(_ctx, push, parent) {
  268. push(`<div>parent`)
  269. push(ssrRenderComponent(Child, { msg: 'hello' }, null, parent))
  270. push(`</div>`)
  271. }
  272. })
  273. )
  274. ).toBe(
  275. `<div>parent<div class="child"><!--[-->fallback<!--]--></div></div>`
  276. )
  277. })
  278. test('nested components with vnode slots', async () => {
  279. const Child = {
  280. props: ['msg'],
  281. ssrRender(ctx: any, push: any, parent: any) {
  282. push(`<div class="child">`)
  283. ssrRenderSlot(
  284. ctx.$slots,
  285. 'default',
  286. { msg: 'from slot' },
  287. null,
  288. push,
  289. parent
  290. )
  291. push(`</div>`)
  292. }
  293. }
  294. expect(
  295. await renderToStream(
  296. createApp({
  297. ssrRender(_ctx, push, parent) {
  298. push(`<div>parent`)
  299. push(
  300. ssrRenderComponent(
  301. Child,
  302. { msg: 'hello' },
  303. {
  304. // bailed slots returning raw vnodes
  305. default: ({ msg }: any) => {
  306. return h('span', msg)
  307. }
  308. },
  309. parent
  310. )
  311. )
  312. push(`</div>`)
  313. }
  314. })
  315. )
  316. ).toBe(
  317. `<div>parent<div class="child">` +
  318. `<!--[--><span>from slot</span><!--]-->` +
  319. `</div></div>`
  320. )
  321. })
  322. test('nested components with template slots', async () => {
  323. const Child = {
  324. props: ['msg'],
  325. template: `<div class="child"><slot msg="from slot"></slot></div>`
  326. }
  327. const app = createApp({
  328. components: { Child },
  329. template: `<div>parent<Child v-slot="{ msg }"><span>{{ msg }}</span></Child></div>`
  330. })
  331. expect(await renderToStream(app)).toBe(
  332. `<div>parent<div class="child">` +
  333. `<!--[--><span>from slot</span><!--]-->` +
  334. `</div></div>`
  335. )
  336. })
  337. test('nested render fn components with template slots', async () => {
  338. const Child = {
  339. props: ['msg'],
  340. render(this: any) {
  341. return h(
  342. 'div',
  343. {
  344. class: 'child'
  345. },
  346. this.$slots.default({ msg: 'from slot' })
  347. )
  348. }
  349. }
  350. const app = createApp({
  351. template: `<div>parent<Child v-slot="{ msg }"><span>{{ msg }}</span></Child></div>`
  352. })
  353. app.component('Child', Child)
  354. expect(await renderToStream(app)).toBe(
  355. `<div>parent<div class="child">` +
  356. // no comment anchors because slot is used directly as element children
  357. `<span>from slot</span>` +
  358. `</div></div>`
  359. )
  360. })
  361. test('async components', async () => {
  362. const Child = {
  363. // should wait for resolved render context from setup()
  364. async setup() {
  365. return {
  366. msg: 'hello'
  367. }
  368. },
  369. ssrRender(ctx: any, push: any) {
  370. push(`<div>${ctx.msg}</div>`)
  371. }
  372. }
  373. expect(
  374. await renderToStream(
  375. createApp({
  376. ssrRender(_ctx, push, parent) {
  377. push(`<div>parent`)
  378. push(ssrRenderComponent(Child, null, null, parent))
  379. push(`</div>`)
  380. }
  381. })
  382. )
  383. ).toBe(`<div>parent<div>hello</div></div>`)
  384. })
  385. test('parallel async components', async () => {
  386. const OptimizedChild = {
  387. props: ['msg'],
  388. async setup(props: any) {
  389. return {
  390. localMsg: props.msg + '!'
  391. }
  392. },
  393. ssrRender(ctx: any, push: any) {
  394. push(`<div>${ctx.localMsg}</div>`)
  395. }
  396. }
  397. const VNodeChild = {
  398. props: ['msg'],
  399. async setup(props: any) {
  400. return {
  401. localMsg: props.msg + '!'
  402. }
  403. },
  404. render(this: any) {
  405. return h('div', this.localMsg)
  406. }
  407. }
  408. expect(
  409. await renderToStream(
  410. createApp({
  411. ssrRender(_ctx, push, parent) {
  412. push(`<div>parent`)
  413. push(
  414. ssrRenderComponent(OptimizedChild, { msg: 'opt' }, null, parent)
  415. )
  416. push(
  417. ssrRenderComponent(VNodeChild, { msg: 'vnode' }, null, parent)
  418. )
  419. push(`</div>`)
  420. }
  421. })
  422. )
  423. ).toBe(`<div>parent<div>opt!</div><div>vnode!</div></div>`)
  424. })
  425. })
  426. describe('vnode element', () => {
  427. test('props', async () => {
  428. expect(
  429. await renderToStream(
  430. h('div', { id: 'foo&', class: ['bar', 'baz'] }, 'hello')
  431. )
  432. ).toBe(`<div id="foo&amp;" class="bar baz">hello</div>`)
  433. })
  434. test('text children', async () => {
  435. expect(await renderToStream(h('div', 'hello'))).toBe(`<div>hello</div>`)
  436. })
  437. test('array children', async () => {
  438. expect(
  439. await renderToStream(
  440. h('div', [
  441. 'foo',
  442. h('span', 'bar'),
  443. [h('span', 'baz')],
  444. createCommentVNode('qux')
  445. ])
  446. )
  447. ).toBe(
  448. `<div>foo<span>bar</span><!--[--><span>baz</span><!--]--><!--qux--></div>`
  449. )
  450. })
  451. test('void elements', async () => {
  452. expect(await renderToStream(h('input'))).toBe(`<input>`)
  453. })
  454. test('innerHTML', async () => {
  455. expect(
  456. await renderToStream(
  457. h(
  458. 'div',
  459. {
  460. innerHTML: `<span>hello</span>`
  461. },
  462. 'ignored'
  463. )
  464. )
  465. ).toBe(`<div><span>hello</span></div>`)
  466. })
  467. test('textContent', async () => {
  468. expect(
  469. await renderToStream(
  470. h(
  471. 'div',
  472. {
  473. textContent: `<span>hello</span>`
  474. },
  475. 'ignored'
  476. )
  477. )
  478. ).toBe(`<div>${escapeHtml(`<span>hello</span>`)}</div>`)
  479. })
  480. test('textarea value', async () => {
  481. expect(
  482. await renderToStream(
  483. h(
  484. 'textarea',
  485. {
  486. value: `<span>hello</span>`
  487. },
  488. 'ignored'
  489. )
  490. )
  491. ).toBe(`<textarea>${escapeHtml(`<span>hello</span>`)}</textarea>`)
  492. })
  493. })
  494. describe('raw vnode types', () => {
  495. test('Text', async () => {
  496. expect(await renderToStream(createTextVNode('hello <div>'))).toBe(
  497. `hello &lt;div&gt;`
  498. )
  499. })
  500. test('Comment', async () => {
  501. // https://www.w3.org/TR/html52/syntax.html#comments
  502. expect(
  503. await renderToStream(
  504. h('div', [
  505. createCommentVNode('>foo'),
  506. createCommentVNode('->foo'),
  507. createCommentVNode('<!--foo-->'),
  508. createCommentVNode('--!>foo<!-')
  509. ])
  510. )
  511. ).toBe(`<div><!--foo--><!--foo--><!--foo--><!--foo--></div>`)
  512. })
  513. test('Static', async () => {
  514. const content = `<div id="ok">hello<span>world</span></div>`
  515. expect(await renderToStream(createStaticVNode(content, 1))).toBe(content)
  516. })
  517. })
  518. describe('scopeId', () => {
  519. // note: here we are only testing scopeId handling for vdom serialization.
  520. // compiled srr render functions will include scopeId directly in strings.
  521. const withId = withScopeId('data-v-test')
  522. const withChildId = withScopeId('data-v-child')
  523. test('basic', async () => {
  524. expect(
  525. await renderToStream(
  526. withId(() => {
  527. return h('div')
  528. })()
  529. )
  530. ).toBe(`<div data-v-test></div>`)
  531. })
  532. test('with slots', async () => {
  533. const Child = {
  534. __scopeId: 'data-v-child',
  535. render: withChildId(function(this: any) {
  536. return h('div', this.$slots.default())
  537. })
  538. }
  539. const Parent = {
  540. __scopeId: 'data-v-test',
  541. render: withId(() => {
  542. return h(Child, null, {
  543. default: withId(() => h('span', 'slot'))
  544. })
  545. })
  546. }
  547. expect(await renderToStream(h(Parent))).toBe(
  548. `<div data-v-child data-v-test><span data-v-test data-v-child-s>slot</span></div>`
  549. )
  550. })
  551. })
  552. test('serverPrefetch', async () => {
  553. const msg = Promise.resolve('hello')
  554. const app = createApp({
  555. data() {
  556. return {
  557. msg: ''
  558. }
  559. },
  560. async serverPrefetch() {
  561. this.msg = await msg
  562. },
  563. render() {
  564. return h('div', this.msg)
  565. }
  566. })
  567. const html = await renderToStream(app)
  568. expect(html).toBe(`<div>hello</div>`)
  569. })
  570. })