[{"data":1,"prerenderedAt":-1},["ShallowReactive",2],{"$KiqIp5Qu8t":3,"vue-tips-\u002Fvue-tips\u002Feffortless-handle-asynchronous-components-with-suspense":52,"blogToc-\u002Fvue-tips\u002Feffortless-handle-asynchronous-components-with-suspense":377,"vue-tips-surrounding-\u002Fvue-tips\u002Feffortless-handle-asynchronous-components-with-suspense":582},{"id":4,"extension":5,"items":6,"meta":49,"stem":50,"__hash__":51},"menu\u002Fmenu.json","json",[7,12,16,20,24,28,32,36,41,45],{"i18nId":8,"to":9,"ariaLabel":10,"mainNavigation":11},"components.menu.home","\u002F","Home",true,{"i18nId":13,"to":14,"ariaLabel":15,"mainNavigation":11},"components.menu.about","\u002Fhire-me","About",{"i18nId":17,"to":18,"ariaLabel":19,"mainNavigation":11},"components.menu.testimonials","\u002Ftestimonials","Testimonials",{"i18nId":21,"to":22,"ariaLabel":23},"components.menu.contact","\u002Fcontact","Contact",{"i18nId":25,"to":26,"ariaLabel":27,"mainNavigation":11},"components.menu.blog","\u002Fblog","Blog",{"i18nId":29,"to":30,"ariaLabel":31,"mainNavigation":11},"components.menu.tips","\u002Fvue-tips","Vue Tips",{"i18nId":33,"to":34,"ariaLabel":35,"mainNavigation":11},"components.menu.projects","\u002Fprojects","Projects",{"i18nId":37,"to":38,"ariaLabel":39,"mainNavigation":40},"components.menu.stats","\u002Fstats","stats",false,{"i18nId":42,"to":43,"ariaLabel":44,"mainNavigation":40},"components.menu.businessCard","\u002Fbusiness-card","Business Card",{"i18nId":46,"to":47,"ariaLabel":48,"mainNavigation":40},"components.menu.publications","\u002Fpublications","Publications",{},"menu","njLT1QoRt2Dv8QWoVmH8xdTAiDywSj0bxBQdmoyWUE0",{"id":53,"title":54,"body":55,"categories":356,"cover":358,"date":359,"description":360,"extension":361,"lastUpdated":362,"meta":363,"navigation":11,"ogImage":362,"path":364,"readingTime":365,"robots":362,"schemaOrg":362,"seo":370,"sitemap":371,"stem":375,"tags":362,"__hash__":376},"vueTips\u002Fvue-tips\u002Feffortless-handle-asynchronous-components-with-suspense\u002Findex.md","Vue Tip: Effortless Handle Asynchronous Components With Suspense",{"type":56,"value":57,"toc":352},"minimark",[58,69,72,75,80,83,93,100,130,133,139,149,152,158,162,168,280,293,299,304,316,325,331,348],[59,60,61],"warning",{},[62,63,64,68],"p",{},[65,66,67],"code",{},"\u003CSuspense>"," is an experimental feature. It is not guaranteed to reach stable status and the API may change before it does.",[62,70,71],{},"In the world of modern web development, delivering seamless user experiences is of utmost importance. Asynchronous components, which load data or resources dynamically, play a crucial role in achieving this goal.",[62,73,74],{},"Vue 3 introduces an interesting feature called Suspense to effortlessly handle asynchronous components. You can use it to show a loading state while all async dependencies in the nested component tree are being resolved.",[76,77,79],"h2",{"id":78},"without-suspense","Without Suspense",[62,81,82],{},"For this article, let's use the following component hierarchy for demonstration purposes:",[84,85,90],"pre",{"className":86,"code":88,"language":89},[87],"language-text","\u003CDashboard>\n  ├─ \u003CProfile> (async component)\n  └─ \u003CFetchComponent> (component with async setup())\n","text",[65,91,88],{"__ignoreMap":92},"",[62,94,95,96,99],{},"We have a ",[65,97,98],{},"Dashboard"," component that includes two child components:",[101,102,103,117],"ul",{},[104,105,106,109,110],"li",{},[65,107,108],{},"Profile",": This component is ",[111,112,116],"a",{"href":113,"rel":114},"https:\u002F\u002Fvuejs.org\u002Fguide\u002Fcomponents\u002Fasync.html",[115],"nofollow","loaded asynchronously",[104,118,119,122,123,126,127],{},[65,120,121],{},"FetchComponent",": This component fetches data using ",[65,124,125],{},"async\u002Fawait"," within ",[65,128,129],{},"\u003Cscript setup>",[62,131,132],{},"Each of the child components has its loading state, they are visible if the component is loaded:",[62,134,135],{},[136,137],"img",{"alt":79,"src":138},"vue-tips\u002Feffortless-handle-asynchronous-components-with-suspense\u002Fwithout-suspense.gif",[140,141,142,145],"note",{},[62,143,144],{},"The code for this demo is available at StackBlitz:",[146,147],"stackblitz",{"project-id":148},"vue-tip-suspense",[62,150,151],{},"As you can imagine this can harm the user experience if you see multiple loading spinners on the page and content displayed at different times.",[62,153,154,155,157],{},"Let's take a look at how ",[65,156,67],{}," is trying to solve that problem.",[76,159,161],{"id":160},"with-suspense","With Suspense",[62,163,164,165,167],{},"The ",[65,166,67],{}," component enables us to display top-level loading & error states while waiting for nested async dependencies to be resolved:",[84,169,173],{"className":170,"code":171,"language":172,"meta":92,"style":92},"language-vue shiki shiki-themes github-light poimandres poimandres","\u003Ctemplate>\n  \u003CSuspense>\n    \u003C!-- Component with nested async dependencies -->\n    \u003CDashboard \u002F>\n\n    \u003C!-- Loading state via #fallback slot -->\n    \u003Ctemplate #fallback> Loading... \u003C\u002Ftemplate>\n  \u003C\u002FSuspense>\n\u003C\u002Ftemplate>\n","vue",[65,174,175,191,202,209,220,226,232,261,271],{"__ignoreMap":92},[176,177,180,184,188],"span",{"class":178,"line":179},"line",1,[176,181,183],{"class":182},"sMzB8","\u003C",[176,185,187],{"class":186},"s8PnL","template",[176,189,190],{"class":182},">\n",[176,192,194,197,200],{"class":178,"line":193},2,[176,195,196],{"class":182},"  \u003C",[176,198,199],{"class":186},"Suspense",[176,201,190],{"class":182},[176,203,205],{"class":178,"line":204},3,[176,206,208],{"class":207},"s1MN7","    \u003C!-- Component with nested async dependencies -->\n",[176,210,212,215,217],{"class":178,"line":211},4,[176,213,214],{"class":182},"    \u003C",[176,216,98],{"class":186},[176,218,219],{"class":182}," \u002F>\n",[176,221,223],{"class":178,"line":222},5,[176,224,225],{"emptyLinePlaceholder":11},"\n",[176,227,229],{"class":178,"line":228},6,[176,230,231],{"class":207},"    \u003C!-- Loading state via #fallback slot -->\n",[176,233,235,237,239,243,247,250,254,257,259],{"class":178,"line":234},7,[176,236,214],{"class":182},[176,238,187],{"class":186},[176,240,242],{"class":241},"sygvE"," #",[176,244,246],{"class":245},"sppiw","fallback",[176,248,249],{"class":182},">",[176,251,253],{"class":252},"syBNg"," Loading... ",[176,255,256],{"class":182},"\u003C\u002F",[176,258,187],{"class":186},[176,260,190],{"class":182},[176,262,264,267,269],{"class":178,"line":263},8,[176,265,266],{"class":182},"  \u003C\u002F",[176,268,199],{"class":186},[176,270,190],{"class":182},[176,272,274,276,278],{"class":178,"line":273},9,[176,275,256],{"class":182},[176,277,187],{"class":186},[176,279,190],{"class":182},[62,281,282,284,285,288,289,292],{},[65,283,67],{}," has two slots: ",[65,286,287],{},"#default"," and ",[65,290,291],{},"#fallback",". Both slots only allow for one immediate child node.",[62,294,295,296,298],{},"Our demo application now behaves this way using ",[65,297,67],{},":",[62,300,301],{},[136,302],{"alt":161,"src":303},"vue-tips\u002Feffortless-handle-asynchronous-components-with-suspense\u002Fwith-suspense.gif",[62,305,164,306,308,309,288,312,315],{},[65,307,67],{}," component has two states: ",[65,310,311],{},"pending",[65,313,314],{},"resolve",".",[62,317,318,319,321,322,324],{},"When ",[65,320,67],{}," is first rendered, it will display its default slot content in memory. If any asynchronous dependencies are encountered, it will transition to a pending state. While in the pending state, the fallback content will be shown. Once all the encountered asynchronous dependencies are resolved, ",[65,323,67],{}," enters a resolved state and displays the resolved default slot content.",[62,326,327,328,330],{},"If no async dependencies were encountered during the initial render, ",[65,329,67],{}," will directly go into a resolved state.",[140,332,333],{},[62,334,335,337,338,341,342,345,346,315],{},[65,336,67],{}," currently does not provide error handling via the component itself.\nHowever, you can use the ",[65,339,340],{},"errorCaptured"," option or the ",[65,343,344],{},"onErrorCaptured()"," hook to capture and handle async errors in the parent component of ",[65,347,67],{},[349,350,351],"style",{},"html pre.shiki code .sMzB8, html code.shiki .sMzB8{--shiki-light:#24292E;--shiki-default:#E4F0FB;--shiki-dark:#E4F0FB}html pre.shiki code .s8PnL, html code.shiki .s8PnL{--shiki-light:#22863A;--shiki-default:#5DE4C7;--shiki-dark:#5DE4C7}html pre.shiki code .s1MN7, html code.shiki .s1MN7{--shiki-light:#6A737D;--shiki-light-font-style:inherit;--shiki-default:#767C9DB0;--shiki-default-font-style:italic;--shiki-dark:#767C9DB0;--shiki-dark-font-style:italic}html pre.shiki code .sygvE, html code.shiki .sygvE{--shiki-light:#24292E;--shiki-default:#A6ACCD;--shiki-dark:#A6ACCD}html pre.shiki code .sppiw, html code.shiki .sppiw{--shiki-light:#6F42C1;--shiki-light-font-style:inherit;--shiki-default:#91B4D5;--shiki-default-font-style:italic;--shiki-dark:#91B4D5;--shiki-dark-font-style:italic}html pre.shiki code .syBNg, html code.shiki .syBNg{--shiki-light:#24292E;--shiki-default:#5DE4C7;--shiki-dark:#5DE4C7}html .light .shiki span {color: var(--shiki-light);background: var(--shiki-light-bg);font-style: var(--shiki-light-font-style);font-weight: var(--shiki-light-font-weight);text-decoration: var(--shiki-light-text-decoration);}html.light .shiki span {color: var(--shiki-light);background: var(--shiki-light-bg);font-style: var(--shiki-light-font-style);font-weight: var(--shiki-light-font-weight);text-decoration: var(--shiki-light-text-decoration);}html .default .shiki span {color: var(--shiki-default);background: var(--shiki-default-bg);font-style: var(--shiki-default-font-style);font-weight: var(--shiki-default-font-weight);text-decoration: var(--shiki-default-text-decoration);}html .shiki span {color: var(--shiki-default);background: var(--shiki-default-bg);font-style: var(--shiki-default-font-style);font-weight: var(--shiki-default-font-weight);text-decoration: var(--shiki-default-text-decoration);}html .dark .shiki span {color: var(--shiki-dark);background: var(--shiki-dark-bg);font-style: var(--shiki-dark-font-style);font-weight: var(--shiki-dark-font-weight);text-decoration: var(--shiki-dark-text-decoration);}html.dark .shiki span {color: var(--shiki-dark);background: var(--shiki-dark-bg);font-style: var(--shiki-dark-font-style);font-weight: var(--shiki-dark-font-weight);text-decoration: var(--shiki-dark-text-decoration);}",{"title":92,"searchDepth":193,"depth":193,"links":353},[354,355],{"id":78,"depth":193,"text":79},{"id":160,"depth":193,"text":161},[187,357],"script","vue-tips\u002Feffortless-handle-asynchronous-components-with-suspense\u002Fog.png","2023-04-28","Suspense components are used to display fallback content when waiting for some sort of asynchronous component to resolve.","md",null,{},"\u002Fvue-tips\u002Feffortless-handle-asynchronous-components-with-suspense",{"text":366,"minutes":367,"time":368,"words":369},"2 min read",1.785,107100,357,{"title":54,"description":360},{"loc":364,"images":372,"lastmod":359},[373,374],{"loc":138},{"loc":303},"vue-tips\u002Feffortless-handle-asynchronous-components-with-suspense\u002Findex","kfXtocHHvgEOi6WVwWbzK5WB7UyA3yZLmuFm8uvMjqg",{"id":378,"title":54,"body":379,"cover":358,"description":360,"extension":361,"meta":573,"navigation":11,"path":364,"robots":362,"seo":576,"sitemap":577,"stem":375,"__hash__":581},"content\u002Fvue-tips\u002Feffortless-handle-asynchronous-components-with-suspense\u002Findex.md",{"type":56,"value":380,"toc":569},[381,387,389,391,393,395,400,404,421,423,427,433,435,439,441,445,521,529,533,537,545,551,555,567],[59,382,383],{},[62,384,385,68],{},[65,386,67],{},[62,388,71],{},[62,390,74],{},[76,392,79],{"id":78},[62,394,82],{},[84,396,398],{"className":397,"code":88,"language":89},[87],[65,399,88],{"__ignoreMap":92},[62,401,95,402,99],{},[65,403,98],{},[101,405,406,413],{},[104,407,408,109,410],{},[65,409,108],{},[111,411,116],{"href":113,"rel":412},[115],[104,414,415,122,417,126,419],{},[65,416,121],{},[65,418,125],{},[65,420,129],{},[62,422,132],{},[62,424,425],{},[136,426],{"alt":79,"src":138},[140,428,429,431],{},[62,430,144],{},[146,432],{"project-id":148},[62,434,151],{},[62,436,154,437,157],{},[65,438,67],{},[76,440,161],{"id":160},[62,442,164,443,167],{},[65,444,67],{},[84,446,447],{"className":170,"code":171,"language":172,"meta":92,"style":92},[65,448,449,457,465,469,477,481,485,505,513],{"__ignoreMap":92},[176,450,451,453,455],{"class":178,"line":179},[176,452,183],{"class":182},[176,454,187],{"class":186},[176,456,190],{"class":182},[176,458,459,461,463],{"class":178,"line":193},[176,460,196],{"class":182},[176,462,199],{"class":186},[176,464,190],{"class":182},[176,466,467],{"class":178,"line":204},[176,468,208],{"class":207},[176,470,471,473,475],{"class":178,"line":211},[176,472,214],{"class":182},[176,474,98],{"class":186},[176,476,219],{"class":182},[176,478,479],{"class":178,"line":222},[176,480,225],{"emptyLinePlaceholder":11},[176,482,483],{"class":178,"line":228},[176,484,231],{"class":207},[176,486,487,489,491,493,495,497,499,501,503],{"class":178,"line":234},[176,488,214],{"class":182},[176,490,187],{"class":186},[176,492,242],{"class":241},[176,494,246],{"class":245},[176,496,249],{"class":182},[176,498,253],{"class":252},[176,500,256],{"class":182},[176,502,187],{"class":186},[176,504,190],{"class":182},[176,506,507,509,511],{"class":178,"line":263},[176,508,266],{"class":182},[176,510,199],{"class":186},[176,512,190],{"class":182},[176,514,515,517,519],{"class":178,"line":273},[176,516,256],{"class":182},[176,518,187],{"class":186},[176,520,190],{"class":182},[62,522,523,284,525,288,527,292],{},[65,524,67],{},[65,526,287],{},[65,528,291],{},[62,530,295,531,298],{},[65,532,67],{},[62,534,535],{},[136,536],{"alt":161,"src":303},[62,538,164,539,308,541,288,543,315],{},[65,540,67],{},[65,542,311],{},[65,544,314],{},[62,546,318,547,321,549,324],{},[65,548,67],{},[65,550,67],{},[62,552,327,553,330],{},[65,554,67],{},[140,556,557],{},[62,558,559,337,561,341,563,345,565,315],{},[65,560,67],{},[65,562,340],{},[65,564,344],{},[65,566,67],{},[349,568,351],{},{"title":92,"searchDepth":193,"depth":193,"links":570},[571,572],{"id":78,"depth":193,"text":79},{"id":160,"depth":193,"text":161},{"categories":574,"date":359,"readingTime":575},[187,357],{"text":366,"minutes":367,"time":368,"words":369},{"title":54,"description":360},{"loc":364,"images":578,"lastmod":359},[579,580],{"loc":138},{"loc":303},"UkAVz9EXeyA7FE9OYeqi5pgqkGveMJo25jvEQ7Q5qpk",[583,587],{"title":584,"path":585,"stem":586,"children":-1},"Vue Tip: Chaining Event Modifiers","\u002Fvue-tips\u002Fchaining-event-modifiers","vue-tips\u002Fchaining-event-modifiers\u002Findex",{"title":588,"path":589,"stem":590,"children":-1},"Nuxt Tip: Use Nuxt Layers to Share Components, Utils, and Configuration Between Your Apps","\u002Fvue-tips\u002Fnuxt-use-nuxt-layers-to-share-components-utils-and-configuration-between-your-apps","vue-tips\u002Fnuxt-use-nuxt-layers-to-share-components-utils-and-configuration-between-your-apps\u002Findex"]