-
Notifications
You must be signed in to change notification settings - Fork 27.1k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Already display component even when getInitialProps hasn't returned yet #388
Comments
@nmaro I have a related situation. Clicking export default class extends React.Component {
static async getInitialProps() {
const res = await axios.get('http://example.com/path/endpoint');
return {data: res.data}
}
render() {
return <span>{this.props.data.value}</span>
}
} The other thing though is that the network tab suggests something CORS related which seems weird because the request completes when not accessing via
This is not the case if the page is not accessed with UpdateI was able to hack around this by detecting environment: export default class extends React.Component {
static async getInitialProps () {
if(!process.browser) {
// Only make request if not on client
const res = await axios.get('http://example.com/path/endpoint')
return {data: res.data}
} else {
// Load data from store
return {data: JSON.parse(sessionStorage.getItem('bpl'))}
}
}
componentDidMount () {
// Put data in store
sessionStorage.setItem('bpl', JSON.stringify(this.props.data))
}
// . . .
} |
@nmaro You can do it by using export default class extends React.Component {
constructor(props) {
super(props)
this.state = { done: false }
}
componentDidMount () {
setTimeout(() => {
this.setState({ done: true })
}, 5000))
}
render() {
return <span>{this.state.done ? 'done' : 'loading'}</span>
}
} @christiannwamba That is not a hack but something we expect. |
Awesome. Thank you! |
@nkzawa I'm sorry I gave a bad example, I don't think componentDidMount can solve what I mean. What I mean is: imagine having a getInitialProps that takes a somewhat long time to load data. When you end on that page from the client (i.e. no server-rendering involved), next.js just hangs. Or are you suggesting not to use getInitialProps to load data on the client, whenever the data takes a long time to fetch? |
Yes, thanks |
@christiannwamba your problem is that the endpoint you're using doesn't support CORS. It's not related to nextjs at all |
Imagine you click on a
<Link/>
that leads to this page:right now next.js just waits.
Wouldn't it be better to be able to show the component already, with a loading flag of some kind?
The text was updated successfully, but these errors were encountered: