Git Product home page Git Product logo

Comments (8)

abhi12299 avatar abhi12299 commented on September 15, 2024 1

Is it a good practice to run router.refresh function when submitting any info in the form? Or sometimes maybe even on every page that could be highly dynamic?

Just use client components in that case - no need to get the RSC payload from the server, when you only have to fetch the data dynamically.

from next.js.

abhi12299 avatar abhi12299 commented on September 15, 2024

the repo you linked has nothing apart from the boilerplate code from create-next-app.

from next.js.

sidletski avatar sidletski commented on September 15, 2024

the repo you linked has nothing apart from the boilerplate code from create-next-app.

That's true, sorry. It's updated now

from next.js.

abhi12299 avatar abhi12299 commented on September 15, 2024

This is not an issue with nextjs. I see the following issues in your code:

  1. try to build the project and it will not work. the home page will try to prerender by fetching data from localhost:3000/... but there's nothing running on that port.
  2. you need to add export const dynamic = "force-dynamic"; to your page.tsx file disable prerendering
  3. when you build the project you will see this output. notice /api/users is prerendered as static content. you dont want that to happen. so have export const dynamic = "force-dynamic"; in your api route as well. the other api route is not static because it uses dynamic params.
image
  1. the home page is a server component, and when you navigate on the client side (by going back from edit page), nextjs will not run the fetch again. so you need to have a separate userlist component as a client component, and fetch the data in a useEffect.
  2. if you do not have a client component, and instead stick to the server component, you'll notice that after editing and going back to the homepage, when you refresh, you still get the same old data. that is because of data cache, and you can opt out of it using the cache option. read more here: https://nextjs.org/docs/app/api-reference/functions/fetch

from next.js.

sidletski avatar sidletski commented on September 15, 2024

@abhi12299

Thank you for looking into my issue! The main purpose of the api + db file was to simulate an API interacting with a database, and it works as intended in dev mode only.
To make this clearer, I've updated the example project by using SQLite3 and made the /api/users route dynamic. In the updated version, I'm using both the unstable_noStore function and cache: 'no-store' for the fetch call, but the data still doesn't update when using the browser's back button. I suspect this happens because the new page isn't being requested, and only client-side navigation is occurring. Isn't there way to get actual data when having server components? I don’t recall having this issue with older Next.js versions that used getServerSideProps, but feel free to correct me if I'm wrong.

from next.js.

abhi12299 avatar abhi12299 commented on September 15, 2024

I suspect this happens because the new page isn't being requested, and only client-side navigation is occurring. Isn't there way to get actual data when having server components?

The answer is in your question. When client side navigation occurs, the homepage is not rendered again - it is loaded from the client side router cache. Read about it here: https://nextjs.org/docs/app/building-your-application/caching#client-side-router-cache - it also shows how you can invalidate the cache by calling router.refresh, so maybe that helps with your usecase.

Also, from the docs, unstable_noStore can be used to declaratively opt out of static rendering and indicate a particular component should not be cached.

This means that you can mark the page as dynamic, but it won't server-render it again after client side navigation.

from next.js.

sidletski avatar sidletski commented on September 15, 2024

you can invalidate the cache by calling router.refresh, so maybe that helps with your usecase.

Thank you, this approach helped! It's quite a good solution, considering that now when data changes, I can have the same effect as in the previous Next.js flow using getServerSideProps, as shown here: https://github.com/sidletski/next-cache-issues-ssr/tree/main/src
Is it a good practice to run router.refresh function when submitting any info in the form? Or sometimes maybe even on every page that could be highly dynamic?

from next.js.

sidletski avatar sidletski commented on September 15, 2024

@abhi12299 Thank you! Would be also nice to have all the new features like a separate loading layout and fetching using server components, but CSR will work for me, too.

from next.js.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    πŸ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. πŸ“ŠπŸ“ˆπŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❀️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.