ssrScopeId.spec.ts 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272
  1. import { createApp, h, mergeProps, withCtx } from 'vue'
  2. import { renderToString } from '../src/renderToString'
  3. import { ssrRenderAttrs, ssrRenderComponent, ssrRenderSlot } from '../src'
  4. describe('ssr: scopedId runtime behavior', () => {
  5. test('id on component root', async () => {
  6. const Child = {
  7. ssrRender: (ctx: any, push: any, parent: any, attrs: any) => {
  8. push(`<div${ssrRenderAttrs(attrs)}></div>`)
  9. },
  10. }
  11. const Comp = {
  12. __scopeId: 'parent',
  13. ssrRender: (ctx: any, push: any, parent: any) => {
  14. push(ssrRenderComponent(Child), null, null, parent)
  15. },
  16. }
  17. const result = await renderToString(createApp(Comp))
  18. expect(result).toBe(`<div parent></div>`)
  19. })
  20. test('id and :slotted on component root', async () => {
  21. const Child = {
  22. // <div></div>
  23. ssrRender: (_: any, push: any, _parent: any, attrs: any) => {
  24. push(`<div${ssrRenderAttrs(attrs)} child></div>`)
  25. },
  26. }
  27. const Wrapper = {
  28. __scopeId: 'wrapper',
  29. ssrRender: (ctx: any, push: any, parent: any) => {
  30. // <slot/>
  31. ssrRenderSlot(
  32. ctx.$slots,
  33. 'default',
  34. {},
  35. null,
  36. push,
  37. parent,
  38. 'wrapper-s',
  39. )
  40. },
  41. }
  42. const Comp = {
  43. __scopeId: 'parent',
  44. ssrRender: (_: any, push: any, parent: any) => {
  45. // <Wrapper><Child/></Wrapper>
  46. push(
  47. ssrRenderComponent(
  48. Wrapper,
  49. null,
  50. {
  51. default: withCtx(
  52. (_: any, push: any, parent: any, scopeId: string) => {
  53. push(ssrRenderComponent(Child, null, null, parent, scopeId))
  54. },
  55. ),
  56. _: 1,
  57. } as any,
  58. parent,
  59. ),
  60. )
  61. },
  62. }
  63. const result = await renderToString(createApp(Comp))
  64. expect(result).toBe(`<!--[--><div parent wrapper-s child></div><!--]-->`)
  65. })
  66. // #2892
  67. test(':slotted on forwarded slots', async () => {
  68. const Wrapper = {
  69. __scopeId: 'wrapper',
  70. ssrRender: (ctx: any, push: any, parent: any, attrs: any) => {
  71. // <div class="wrapper"><slot/></div>
  72. push(
  73. `<div${ssrRenderAttrs(
  74. mergeProps({ class: 'wrapper' }, attrs),
  75. )} wrapper>`,
  76. )
  77. ssrRenderSlot(
  78. ctx.$slots,
  79. 'default',
  80. {},
  81. null,
  82. push,
  83. parent,
  84. 'wrapper-s',
  85. )
  86. push(`</div>`)
  87. },
  88. }
  89. const Slotted = {
  90. __scopeId: 'slotted',
  91. ssrRender: (ctx: any, push: any, parent: any, attrs: any) => {
  92. // <Wrapper><slot/></Wrapper>
  93. push(
  94. ssrRenderComponent(
  95. Wrapper,
  96. attrs,
  97. {
  98. default: withCtx(
  99. (_: any, push: any, parent: any, scopeId: string) => {
  100. ssrRenderSlot(
  101. ctx.$slots,
  102. 'default',
  103. {},
  104. null,
  105. push,
  106. parent,
  107. 'slotted-s' + scopeId,
  108. )
  109. },
  110. ),
  111. _: 1,
  112. } as any,
  113. parent,
  114. ),
  115. )
  116. },
  117. }
  118. const Root = {
  119. __scopeId: 'root',
  120. // <Slotted><div></div></Slotted>
  121. ssrRender: (_: any, push: any, parent: any, attrs: any) => {
  122. push(
  123. ssrRenderComponent(
  124. Slotted,
  125. attrs,
  126. {
  127. default: withCtx(
  128. (_: any, push: any, parent: any, scopeId: string) => {
  129. push(`<div root${scopeId}></div>`)
  130. },
  131. ),
  132. _: 1,
  133. } as any,
  134. parent,
  135. ),
  136. )
  137. },
  138. }
  139. const result = await renderToString(createApp(Root))
  140. expect(result).toBe(
  141. `<div class="wrapper" root slotted wrapper>` +
  142. `<!--[--><!--[--><div root slotted-s wrapper-s></div><!--]--><!--]-->` +
  143. `</div>`,
  144. )
  145. })
  146. // #3513
  147. test('scopeId inheritance across ssr-compiled and on-ssr compiled parent chain', async () => {
  148. const Child = {
  149. ssrRender: (ctx: any, push: any, parent: any, attrs: any) => {
  150. push(`<div${ssrRenderAttrs(attrs)}></div>`)
  151. },
  152. }
  153. const Middle = {
  154. render() {
  155. return h(Child)
  156. },
  157. }
  158. const Comp = {
  159. __scopeId: 'parent',
  160. ssrRender: (ctx: any, push: any, parent: any) => {
  161. push(ssrRenderComponent(Middle, null, null, parent))
  162. },
  163. }
  164. const result = await renderToString(createApp(Comp)) // output: `<div></div>`
  165. expect(result).toBe(`<div parent></div>`)
  166. })
  167. // #6093
  168. test(':slotted on forwarded slots on component', async () => {
  169. const Wrapper = {
  170. __scopeId: 'wrapper',
  171. ssrRender: (ctx: any, push: any, parent: any, attrs: any) => {
  172. // <div class="wrapper"><slot/></div>
  173. push(
  174. `<div${ssrRenderAttrs(
  175. mergeProps({ class: 'wrapper' }, attrs),
  176. )} wrapper>`,
  177. )
  178. ssrRenderSlot(
  179. ctx.$slots,
  180. 'default',
  181. {},
  182. null,
  183. push,
  184. parent,
  185. 'wrapper-s',
  186. )
  187. push(`</div>`)
  188. },
  189. }
  190. const Slotted = {
  191. __scopeId: 'slotted',
  192. ssrRender: (ctx: any, push: any, parent: any, attrs: any) => {
  193. // <Wrapper><slot/></Wrapper>
  194. push(
  195. ssrRenderComponent(
  196. Wrapper,
  197. attrs,
  198. {
  199. default: withCtx(
  200. (_: any, push: any, parent: any, scopeId: string) => {
  201. ssrRenderSlot(
  202. ctx.$slots,
  203. 'default',
  204. {},
  205. null,
  206. push,
  207. parent,
  208. 'slotted-s' + scopeId,
  209. )
  210. },
  211. ),
  212. _: 1,
  213. } as any,
  214. parent,
  215. ),
  216. )
  217. },
  218. }
  219. const Child = {
  220. ssrRender: (ctx: any, push: any, parent: any, attrs: any) => {
  221. push(`<div${ssrRenderAttrs(attrs)}></div>`)
  222. },
  223. }
  224. const Root = {
  225. __scopeId: 'root',
  226. // <Slotted><Child></Child></Slotted>
  227. ssrRender: (_: any, push: any, parent: any, attrs: any) => {
  228. push(
  229. ssrRenderComponent(
  230. Slotted,
  231. attrs,
  232. {
  233. default: withCtx(
  234. (_: any, push: any, parent: any, scopeId: string) => {
  235. push(ssrRenderComponent(Child, null, null, parent, scopeId))
  236. },
  237. ),
  238. _: 1,
  239. } as any,
  240. parent,
  241. ),
  242. )
  243. },
  244. }
  245. const result = await renderToString(createApp(Root))
  246. expect(result).toBe(
  247. `<div class="wrapper" root slotted wrapper>` +
  248. `<!--[--><!--[--><div root slotted-s wrapper-s></div><!--]--><!--]-->` +
  249. `</div>`,
  250. )
  251. })
  252. })