renderToString.spec.ts 16 KB

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