Performance: Mocking data fetching
Mocking data fetching affects page load speed and interaction responsiveness by simulating network calls without real server delays.
export async function getServerSideProps() { const data = { id: 1, name: 'Mocked Item' }; return { props: { data } }; }
export async function getServerSideProps() { const res = await fetch('https://api.example.com/data'); const data = await res.json(); return { props: { data } }; }
| Pattern | DOM Operations | Reflows | Paint Cost | Verdict |
|---|---|---|---|---|
| Real network fetch on each load | Minimal | 1 reflow after data arrives | Moderate paint cost due to delayed content | [X] Bad |
| Mocked data returned immediately | Minimal | 1 reflow immediately | Low paint cost with instant content | [OK] Good |
async function fetchData() {
return new Promise(resolve => setTimeout(() => resolve({ users: ["Alice", "Bob"] }), 500));
}
fetchData().then(data => console.log(data.users));async function fetchData() {
setTimeout(() => {
return { data: [1, 2, 3] };
}, 1000);
}Pass the test and this pattern moves unknown