renderToString.spec.ts 15 KB

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