Performance: GET requests in components
MEDIUM IMPACT
This affects page load speed and interaction responsiveness by controlling when and how data is fetched and rendered in Vue components.
<script setup> import { ref } from 'vue'; const data = ref(null); // Fetch data outside component or use server-side fetching fetch('https://api.example.com/data') .then(res => res.json()) .then(json => data.value = json); </script> <template> <div v-if="data">{{ data }}</div> <div v-else>Loading...</div> </template>
<script setup> import { onMounted, ref } from 'vue'; const data = ref(null); onMounted(async () => { const response = await fetch('https://api.example.com/data'); data.value = await response.json(); }); </script> <template> <div>{{ data }}</div> </template>
| Pattern | DOM Operations | Reflows | Paint Cost | Verdict |
|---|---|---|---|---|
| Fetch in onMounted (after initial render) | Minimal DOM nodes initially | Triggers 1 reflow after data arrives | Paint delayed until data ready | [X] Bad |
| Fetch outside component with loading state | More DOM nodes for loading UI | Single reflow when data replaces loading | Paint happens immediately with loading | [OK] Good |