renderToStream.spec.ts 15 KB

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