renderToString.spec.ts 15 KB

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