ElMassimo / iles

🏝 The joyful site generator
https://iles.pages.dev
MIT License
1.08k stars 31 forks source link

Add `useFetch` and `useAsyncData` for data fetching #52

Open MichaelPrecel opened 2 years ago

MichaelPrecel commented 2 years ago

Thanks so much for this really interesting project!

I was wondering whether there is any way built into Iles that allows fetching data on the server at build time to be used in components? Similar to Nuxt's asyncData?

ElMassimo commented 2 years ago

Hi Michael!

Currently there's no built-in way, but there are several ways to achieve it.

In îles you can use await in script setup or in getStaticPaths.

See:

I'll keep this open as a reminder to automatically inject $fetch with ohmyfetch if it's used.

In the meantime you could ponyfill fetch or use ohmyfetch directly.

If you need caching, check this example using a caching utility, which ensures the request is made only once across rendered pages. Something like this might be built-in in the future, as useFetch and useAsyncData.

MichaelPrecel commented 2 years ago

This is all very helpful, thanks @ElMassimo !