I want to fetch data using getInitialProps
. I use isomorphic-unfetch
lib.
import fetch from 'isomorphic-unfetch'
...My Component Page...
Component.getInitialProps = async () => {
const res = await fetch(<path>, {method: 'GET', headers: {
'Authorization': `bearer ${typeof Storage !== 'undefined' && localStorage.getItem('my_token')}`,
}})
const data = await res.data
return {stuff: data}
}
It works accurately on client side however it returns 401 on server side. I think the problem is localStorage. LocalStorage is not accessible from the server. As I understand it, I need to wait until page has mounted. How to solve this problem?
Supplementary question, can I use the axios
instead of isomophic-unfetch
?