Closed marinav closed 5 years ago
Hi @marinav what's up? Please could you reproduce the problem in codesandbox?
CodeSandbox is an online editor that's built for web application development. Web application development is a growing field, and with all new configuration options it becomes harder and harder to focus on writing code.
Regards!
@marinav can you put ur example in a git repo. I'd love to just clone it and take a look
Hi everyone, thank you for your answers. I will try to put an example when I have a little time because it is a private project I can not share it as simply.
Hi,
I restarted from scratch, I clone this repo, and instead of simulate an API call, I updated getCurrentProfile function (in /modules/profile.js) to call my own api (like proposed here #33).
I can see my data, but when I curl my page after a yarn build && yarn serve
I don't see them, and currentProfile is empty. But when I try with the original function it works. This is the only difference I've done in the project :
The original function (with working SSR)
export const getCurrentProfile = id => dispatch =>
new Promise(resolve => {
setTimeout(() => {
let profile;
if (id === 1) {
profile = {
id,
name: 'Pekka Rinne',
image: pekka
};
} else {
profile = {
id,
name: 'Viktor Arvidsson',
image: arvidsson
};
}
dispatch({
type: SET_CURRENT_PROFILE,
profile
});
resolve(profile);
}, 3000);
});
The new function (with non working SSR)
export const getCurrentProfile = id => dispatch =>
fetch('https://myapi/data', {
headers: {
Authorization: `Bearer xxxxxxxxxxx`,
'Content-Type': 'application/x-www-form-urlencoded'
}
})
.then(res => res.json())
.then(
result => {
dispatch({
type: SET_CURRENT_PROFILE,
profile: {
id,
name: result.name,
image: arvidsson
}
});
},
error => {
console.log(error.message);
}
)
What am I doing wrong ??
Thank you !
@marinav Please show us the screenshot of the Google Chrome console log. That's how we can start to figure out.
If you rely on json structure in my example API, you should use something like below
export const getCurrentProfile = id => dispatch =>
fetch("https://myapi/data/profile/" + id, {
headers: {
"Authorization": "Bearer xxxxxxxxxxx",
"Content-Type": "application/x-www-form-urlencoded"
}
})
.then(res => res.json())
.then(
result => {
dispatch({
type: SET_CURRENT_PROFILE,
profile: {
id: id,
name: result.profile.name,
image: result.profile.image
}
});
},
error => {
console.log(error.message);
}
);
@sabitertan in fact I call an external API that doesn't return a profile user but a list of producers, so I make the call, and put in profile's name the name of my first producer. I'm just testing so the profile id doesn't really matter.
@francisrod01 I have no errors in my google chrome console log, but here a screenshot of my result and network request (the name is the name I get from my api but on curl nothing...) :
Ah ok "curl" only issue, I will test later and check the issue.
@marinav If it's just a test, why are you hiding the urls in the screenshot?
I'm confuse about your way to fetch the data.
I think you should change the content-type
to application/json
and perform new tests.
Reference:
Hi guys thank you for your answers !
@francisrod01 in fact it's a private project and I don't necessarily want to publish the staging url of my api. But it could be any url actually I don't feel that the problem comes from there. I still changed the content-type but it's the same thing 😢
@sabitertan thank you 🙏
I couldn't replicate this issue using "yarn". But if I use "npm i && npm run build && npm run serve" that throws error and crashes server. Can you confirm you are installing and running with only "yarn"?
@marinav it's a good practice of many developers to isolate the problem then trying to share the doubt or problem with someone using codesandbox, for example.
Many times we discover the problem coding it again.
Currently we don't know the problem and it make the situation too hard to help.
Hi,
Thank you for you answers !
@sabitertan yes I'm installing and running with only "yarn".
@francisrod01 I understand, I created a codeSandbox here I just restarted from your project and updated getCurrentProfile function to make an api call (like I did above), I see the name in the profile page but I when I do a curl on my sandbox page :
curl https://30v6849055.codesandbox.io/profile/2
I don't see the html of my page (unlike it is in local when I do a yarn build and serve) so it's hard to say if it's working or not.
I just forked your codesandox example and I typed curl
on the console and I could see the response.
curl https://4jj997n9x.codesandbox.io/profile/2
Below I present a screenshot about that:
ok sorry I didn't express myself well, when I curl
codesansbox url I have the same result as you but I don't see body's html. While in local when I just clone the project and modify the function I see this :
Orignal function curl result:
With api call (same as codeSandbox) curl result :
I think it maybe comes from react-frontload because in loader.js file, after frontloadServerRender function when I get the state with store.getState(), I get an empty state with my custom function. And it's not the case with the orignal function witch get profile data
Hi,
First thank you for your awesome work. I'm using your repo since last july. I took it because of SSR, it was working fine but now I found that SSR is not working anymore. When I curl my page, I see all html exept my async data, I see the window.__PRELOADED_STATE__ but the store is empty. I tryied to update my version like you did but it's not working better. You can see my code below, may be you will see what I missed...thank you very much !
This is my package.json dependencies :
This is my component :
This is the frontload section in my loader.js file
And my index.js file :