Hey! I’ve been using Axios con Next 14 for a while, and I’d recommend using it in API routes. It keeps your frontend code cleaner and lets you handle all the heavy lifting server-side.
For caching, I’d suggest using `swr`. It’s super easy to set up and works great with Next.js.
Here’s a quick example of how I set up Axios in an API route:
```javascript
export default async function handler(req, res) {
try {
const response = await axios.get('https://api.yoursite.com/data');
res.status(200).json(response.data);
} catch (error) {
res.status(500).json({ error: 'Something went wrong' });
}
}
```
Hope this helps!
For caching, I’d suggest using `swr`. It’s super easy to set up and works great with Next.js.
Here’s a quick example of how I set up Axios in an API route:
```javascript
export default async function handler(req, res) {
try {
const response = await axios.get('https://api.yoursite.com/data');
res.status(200).json(response.data);
} catch (error) {
res.status(500).json({ error: 'Something went wrong' });
}
}
```
Hope this helps!
