-
|
Example, this code here: ^^^ the fetch calling from above is referring to this page code below, which is calling a mongoose schema and the actual connection to the MongoDB database... Could this work? yes? no? why? What would happen during build time? I see a local host 3000.... could this data be fetch during build time??? How about during revalidation? ISR ??? Is there a cleaner way to do it? |
Beta Was this translation helpful? Give feedback.
Replies: 3 comments 2 replies
-
|
If the development mode application is also running in Think of When Let's say you mock a server at There's nothing forbidding you from reusing the Exampleexport const getCodeWarsUser = (user) =>
axios
.get(`https://www.codewars.com/api/v1/users/${user}`)
.then(({ data }) => data);Then in export async function getStaticProps() {
const codewars = await getCodeWarsUser('some-user');
return {
props: { codewars },
revalidate: 10,
};
}And also in const codewars = async (req, res) => {
const data = await getCodeWarsUser('some-user');
return res.send(data);
};
export default codewars;And again, don't be scared about leaking stuff to the client, and use the https://next-code-elimination.vercel.app/ to verify what's going on. |
Beta Was this translation helpful? Give feedback.
-
|
Do I need to build a server to run ISR? Can I just implement a code like: Or..... do I need something else besides that? |
Beta Was this translation helpful? Give feedback.
-
|
I am converting a regular CSR website into a Nextjs website. I think I have 90% done going from this project here (regular REACT): To this project here(Nextjs): Everything is done except for one page that needs data from MongoDB... working on the sever side to extract data and ideally.... do ISR with pre-view mode... the final page I am working is this one live here: http://www.tu.biz/Inventory But somebody told me I need a next.js sever in order to make it work? At this point I need to implement connection at the getStaticProps, include revalidation, and figure out the pre-view mode... And.... if I have a dark confession to make, I am working with another person because I was thinking I could finish it up quickly, and apparently they don't know how to do this last section either.... so... I might have to do it myself after all.... grrr... I am thinking about hosting it in regular $20 bucks Vercel place.... I don't think I need any extras other than the code... |
Beta Was this translation helpful? Give feedback.
If the development mode application is also running in
localhost:3000(this is a bit of a complicated way to say it, keep on reading), it won't work, because, when building the application doesn't even exist.Think of
api/routesas runtime artifacts. The server needs to be running and a request must arrive for them to run.When
getStaticPropsruns at build time, there's none of the above. This is why, it's best to abstract the mongo db connections into reusable functions that you can use both ingetStaticProps,getServerSidePropsandapi/routes.Let's say you mock a server at
localhost:3000, then it better continue to exist when ISR happens.There's nothing forbidding you from reusing the
a…