-
Hi! I'm currently working with NextJS and SWR. I created an Next API endpoint ( My issue is that both fetches are returning the same output even though I'm using different keys for each one; the code looks something like this:
// ...
export default async (req: NextApiRequest, res: NextApiResponse) => {
if (req.method === 'GET') {
const { fileId } = req.query
const downloadedFile = await api.file.get(fileId)
res.status(200)
downloadedFile.pipe(res)
}
} I defined a reusable hook:
import axios from 'axios'
import useSWR from 'swr'
export const useFile = (fileId: string | null) => {
const { data, error } = useSWR(
fileId,
async () => (
await axios.get(`/api/files/${fileId}`, {
responseType: 'blob',
})
).data,
)
return { error, file: data, isLoading: !error && !data }
} Using the hook on the page, each File ID comes from a different fetch done elsewhere:
import { useFile } from 'lib/api/hooks'
// ...
const Page: NextPage = () => {
// ...
const { file: firstFile, isLoading: firstLoading } = useFile(firstFileId)
const { file: secondFile, isLoading: secondLoading } = useFile(secondFileId)
// ...
}
// ... If I comment out one of the I'm not sure if I'm missing something, but hopefully someone could clear my mind a bit. If any more info is needed to solve my question, I'll gladly share anything. Thanks a lot! |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment
-
Solved. This was an issue with the library used to fetch the file, nothing to do with Next nor SWR, my bad! |
Beta Was this translation helpful? Give feedback.
Solved.
This was an issue with the library used to fetch the file, nothing to do with Next nor SWR, my bad!