|
| 1 | +--- |
| 2 | +title: Facade Components |
| 3 | +description: Facade Components are fake UI elements that get replaced once a third-party script loads. |
| 4 | +--- |
| 5 | + |
| 6 | +Nuxt Scripts provides several Facade Components that you can use to speed up your app's performance. |
| 7 | + |
| 8 | +Using them has trade-offs, but they can aid in improving the performance experience of your app. |
| 9 | + |
| 10 | +## What are Facade Components? |
| 11 | + |
| 12 | +To render complex components using third-party scripts such as a Video embed, payment modal, or chat widget, we need many resources. |
| 13 | +Loading these while Nuxt is starting will slow down your app's performance. |
| 14 | + |
| 15 | +However, if we delay loading the script until Nuxt is finished, we end up with harmful content layout shifts (CLS) and visual noise, |
| 16 | +leading to a poor user experience. |
| 17 | + |
| 18 | +Facade Components aim to solve this by rendering a "fake" UI element that gets replaced once the third-party script loads. |
| 19 | + |
| 20 | +By hooking into appropriate DOM events and providing user feedback, we can use these fake elements while still providing a great user experience. |
| 21 | + |
| 22 | +## What are the trade-offs in using Facade Components? |
| 23 | + |
| 24 | +While the performance benefit is quite clear, there can be trade-off on user experience. |
| 25 | + |
| 26 | +- **Flash of mismatched content**: The fake UI element may not look like the final UI element, leading to a flash of mis-matched content. Only minimal |
| 27 | +styling is provided by Nuxt Scripts, you may need to tweak it to match your app's design. |
| 28 | +- **Interactivity can break**: Interactivity of the elements requires the script to load, if it doesn't load then you need to provide a fallback. |
| 29 | +- **Accessibility Concerns**: We need to provide clear a11y feedback to the user when the script is loading or fails to load. |
| 30 | + |
| 31 | +## Nuxt Scripts Facade Components |
| 32 | + |
| 33 | +All Facade Components are headless components that wrap the relevant `useScript<provider>` composable. Minimal styling is |
| 34 | +provided within the docs to give you a starting point. |
| 35 | + |
| 36 | +## Best Practices in using Facade Components |
| 37 | + |
| 38 | +### Provide an error fallback |
| 39 | + |
| 40 | +If the script fails to load, provide a fallback that informs the user of the failure and provides an alternative way to access the content. |
| 41 | + |
| 42 | +```vue |
| 43 | +<ScriptYouTubePlayer> |
| 44 | + <template #error> |
| 45 | + <UAlert color="red" title="YouTube player failed to load" description="Please refresh the page to try again." /> |
| 46 | + </template> |
| 47 | +</ScriptYouTubePlayer> |
| 48 | +``` |
| 49 | + |
| 50 | +### Provide a loading state with accessible feedback |
| 51 | + |
| 52 | +When the script is loading, provide a loading state that informs the user that the content is loading. |
| 53 | + |
| 54 | +The `ScriptLoadingIndicator` component is provided by Nuxt Scripts to help with this and provides a11y feedback. |
| 55 | + |
| 56 | +```vue |
| 57 | +<ScriptYouTubePlayer> |
| 58 | + <template #loading> |
| 59 | + <ScriptLoadingIndicator /> |
| 60 | + </template> |
| 61 | +</ScriptYouTubePlayer> |
| 62 | +``` |
| 63 | + |
| 64 | +### Choose the triggering event wisely |
| 65 | + |
| 66 | +The Facade Components are pre-configured for the best general performance, but you can customize the triggering event to better match your app's needs. |
| 67 | + |
| 68 | +The best triggers are one that require explicit user interaction such as a click. Loading on hover may cause user experience issues |
| 69 | +with subsequent events being lost such as clicks. |
| 70 | + |
| 71 | +## Facade Components API |
| 72 | + |
| 73 | +All Facade Components share a similar API. |
| 74 | + |
| 75 | +### Props |
| 76 | + |
| 77 | +- `trigger` - The event that triggers the script to load. See [Element Event Triggers](/docs/guides/script-triggers#element-event-triggers) for more information. |
| 78 | + |
| 79 | +### Slots |
| 80 | + |
| 81 | +The component provides minimal UI by default, only enough to be functional and accessible. There are a number of slots for you to customize the components however you need. |
| 82 | + |
| 83 | +- `default` - Content to always display with the component. |
| 84 | + |
| 85 | +```vue |
| 86 | +<template> |
| 87 | + <ScriptYouTubePlayer> |
| 88 | + <div class="bg-blue-500 text-white p-5"> |
| 89 | + Youtube! |
| 90 | + </div> |
| 91 | + </ScriptYouTubePlayer> |
| 92 | +</template> |
| 93 | +``` |
| 94 | + |
| 95 | +- `loading` - The content to display only while the script is loading. |
| 96 | + |
| 97 | +```vue |
| 98 | +<template> |
| 99 | + <ScriptYouTubePlayer> |
| 100 | + <template #loading> |
| 101 | + <ScriptLoadingIndicator /> |
| 102 | + </template> |
| 103 | + </ScriptYouTubePlayer> |
| 104 | +</template> |
| 105 | +``` |
| 106 | + |
| 107 | +- `awaitingLoad` - The content to display only while the script is waiting to load. |
| 108 | + |
| 109 | +```vue |
| 110 | +<template> |
| 111 | + <ScriptYouTubePlayer> |
| 112 | + <template #awaitingLoad> |
| 113 | + <div class="bg-blue-500 text-white p-5"> |
| 114 | + Click to play! |
| 115 | + </div> |
| 116 | + </template> |
| 117 | + </ScriptYouTubePlayer> |
| 118 | +</template> |
| 119 | +``` |
| 120 | + |
| 121 | +- `error` - The content to display if the script fails to load. |
| 122 | + |
| 123 | +```vue |
| 124 | +<template> |
| 125 | + <ScriptYouTubePlayer> |
| 126 | + <template #error> |
| 127 | + <UAlert color="red" title="YouTube player failed to load" description="Please refresh the page to try again." /> |
| 128 | + </template> |
| 129 | + </ScriptYouTubePlayer> |
| 130 | +</template> |
| 131 | +``` |
| 132 | + |
| 133 | +### Events |
| 134 | + |
| 135 | +- `ready` - Emitted when the script has loaded. Gives you access to the underlying script API. |
| 136 | +- `error` - Emitted when the script fails to load. |
0 commit comments