I am new to Next.js, coming from an express/create-react-app background, and can't wrap my head around how SSR works in Next.js.
With SSR on expressJs, we would click on a link, and the server will send the corresponding ejs/pug/html page matching the new route. The state on the client is lost, as expected.
But with Next.js, the state on the client remains, which is not how I expect SSR to behave. Maybe SSR with Next is more similar to fetching an api for data, like the flow I am imagining below?
- User visit a new page that requires SSR
NextJscallsgetServerSidePropsto grab some data(from api/db) and populatepropsfor the page component.- Client's page component detects the new
propsand update itself(hydration?)
If someone can confirm or clarify would be appreciated.
Same page code for reference:
paged.js
import React from "react";
import axios from "axios";
const sendGetRequest = async () => {
try {
const resp = await axios.get("https://dog.ceo/api/breeds/image/random");
return resp.data.message;
} catch (err) {
console.error(err);
}
};
function Paged(props) {
return (
<div>
<p>{props.dogstring}</p>
</div>
);
}
export async function getServerSideProps(context) {
const dogstring = await sendGetRequest();
return {
props: {
dogstring: dogstring,
},
};
}
export default Paged;