bug: Home fetch run twice
See original GitHub issue/src/routes/home index.js: fetch
import React from 'react';
import Home from './Home';
import fetch from '../../core/fetch';
import Layout from '../../components/Layout';
export default {
path: '/',
async action() {
const resp = await fetch('/graphql', {
method: 'post',
headers: {
Accept: 'application/json',
'Content-Type': 'application/json',
},
body: JSON.stringify({
query: '{news{title,link,contentSnippet}}',
}),
credentials: 'include',
});
const { data } = await resp.json();
if (!data || !data.news) throw new Error('Failed to load the news feed.');
return {
title: 'React Starter Kit',
component: <Layout><Home news={data.news} /></Layout>,
};
},
};
**server.js **
app.use('/graphql', expressGraphQL(req => ({
schema,
graphiql: process.env.NODE_ENV !== 'production',
rootValue: { request: req },
pretty: process.env.NODE_ENV !== 'production',
})));
debug log,expressGraphQL will run twice
Issue Analytics
- State:
- Created 7 years ago
- Comments:8
Top Results From Across the Web
fetch request is called twice? - node.js - Stack Overflow
For example if I want to send POST Request to create an item this item will be created twice with the same id...
Read more >Fetch is causing form submission to occur twice - SitePoint
I've narrowed it down to the changeCallback function running twice when I submit the forms but for the life of me I can't...
Read more >How to Fetch Data From an API Using React Hooks
In this article, we will look at how to fetch data from API using React hooks and also how to use the data...
Read more >Detecting Double-fetch Bugs by Multi-taint Parallel Tracking
value that resides in the user space twice, first time for verifying or establishing a relation with ... A double-fetch bug is caused...
Read more >React 18 useEffect Double Call for APIs: Emergency Fix
This createFetch function will create a cached fetch for you. If you call it with the same URL twice, it will return the...
Read more >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
This is not a bug my friend.
since the routes are universal, it will first run on the server and then run again on the client.
This is basically one of the major point of using isomorphic architecture: to prefetch the data on the server.
I personally use redux for data handling. So basically when request comes in, on server side I would fetch the data, put the data in redux store, render the route, and at last, pass down the rendered page with the redux state to the client.
Then on the client side, I would check if the data is already in the redux store. if it is, then there is no need to fetch the data again.
For your specific case
store.getState()
can be replaced by a selector like const data = getVideo(store.getState(), vedioId); implement the getter according to any business logic.On Thu, May 4, 2017 6:42 AM, Dan Van Brunt notifications@github.com wrote: This message is eligible for Automatic Cleanup! (notifications@github.com) Add cleanup rule | More info
@DylanYasen, two questions…
is your code sample pseudo code, in that would that if (!data) check more then just existence, or would it be diving into the type of data to validate its contents?
What about in the case of a dynamic route. Say a VideoDetails page where the url is of type domain.com/video/24234 if all we do is check for existence… as you browse from videodetails page for video1 to videodetails page for video2 the content would never change since that check if (!data) will always be true.
— You are receiving this because you are subscribed to this thread. Reply to this email directly, view it on GitHub, or mute the thread.
Thanks & Regards Shishir Arora