I have created a post listing application and upon clicking on each post the user will be redirected to single details page of the post
index.js (list of posts)
export default function Home(props) {
const [postData, setPostData] = useState([]);
useEffect(() => {
fetch("https://jsonplaceholder.typicode.com/posts", { next: { revalidate: 10 } })
.then((res) => res.json())
.then((data) => {
setPostData(data);
});
}, []);
const cardArr = postData.map((post) => <Card data={post} key={post.id} />);
return (
<Layout>
<Header>
<div>Posts</div>
</Header>
{cardArr}
</Layout>
);
}
[id].js (single post detail page)
export const PostPage = (props) => {
const router = useRouter();
const [post, setPost] = useState({});
useEffect(() => {
fetch(`https://jsonplaceholder.typicode.com/posts/${router.query.id}`, {
next: { revalidate: 1000 },
})
.then((res) => res.json())
.then((data) => {
setPost(data);
});
}, []);
return (
<Layout>
<Header>
<div className={styles.header}>{post.title}</div>
</Header>
<div>{post.body}</div>
</Layout>
);
};
export default PostPage;
I am trying to implement route caching. Such that if I go to one post then request is made to the server and the data will be cached. Next time if I go to same route within some time then instead of fetching data from the server it should display data from the cache.
I followed next js documentation where they used
{ next: { revalidate: 10 } }
But it is not working please guide me if I am missing something.
Looking at your code, you seem to mixing app-dir with pages-dir logic. In reality you are not fetching anything "on the server" but on the client, judging by the usage of
useEffectanduseStateinside your components. As server components you would define it like this:Furthermore I have noticed you seem to be using the router from
next/router, that only works inside thepagesdirectory. When using the app directory you would be importing it fromnext/navigation, and retrieve your query from thesearchParamsprop passed to every page.I strongly recommend you take a look at the official documentation for the new app-router since you do not seem to understand the fundamentals at all.