How to display the dynamic content from api in View Source? #11744
Unanswered
Maniraj-Murugan
asked this question in
Help
Replies: 1 comment
-
So it sounds like you have a few different issues going on.
So in order to fetch your data on the server, you will need to determine if a user is logged in another way other than localStorage, such as cookies. Otherwise, here is a quick example of how you can fetch data on the server: import React from "react";
class Home extends React.Component {
// `getServerSideProps` is available as of Next v9.3. It only runs on the server.
// https://nextjs.org/docs/basic-features/data-fetching#getserversideprops-server-side-rendering
// otherwise if you are on Next v9.2 or lower use `getInitialProps`
static async getServerSideProps(context) {
const res = await fetch("https://api.github.com/repos/developit/preact");
const json = await res.json();
return { stars: json.stargazers_count };
}
render() {
return (
<div>
the home component
</div>
);
}
}
export default Home; |
Beta Was this translation helpful? Give feedback.
0 replies
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
-
I have already looked into https://nextjs.org/docs/basic-features/data-fetching and made the following,
I am made a simple example to determine my problem,
Index.js (Simply calling another file Layout in index)
Layout.js
This is the main file I would like to explain what issue I am facing.
Let us not get confused by seeing the above file because I am making same api call in different scenarios.
Say I am fetching api based on two different conditions, one is based on user logged in and another is if not logged in.
For which I have stored the data in localstorage.
But as I am using next js, all the data that has been fetched from api should be displayed in view source but now it is not displaying and I can understand this is due to async behaviour.
Complete simple application example is here: https://codesandbox.io/s/nextjs-getinitialprops-748d5
If you get into this link https://748d5.sse.codesandbox.io/ and click
ctrl + u
then you would see the source where I am also need the dynaic content fetched from api.If I am switching the
localstorage
intogetInitialProps
method then it doesn't work because I can understand that localstorage not available for SSR..So it can only be used in
componentDidMount
but if I use incomponentDidMount
then I am not getting the dynamic content in view source.I have made in layout.js file that If I call the localstorage condition in getInitialProps method then it wont work but if I make the same in componentDidMount it works but the dynamic content is not visible in view page source
In the above given example it should get into
else
part insidecomponentDidMount()
and onctrl + u
it should view the content fetched from api.As per my understanding it is needed to display all the content including the content fetched from api inside the view source for SEO purpose.
So kindly help me how can I achieve the dynamic content displaying in view source (Which is my end goal)?
Beta Was this translation helpful? Give feedback.
All reactions