Next.js Discord

Discord Forum

Disable server actions caching

Answered
Asiatic Lion posted this in #help-forum
Open in Discord
Asiatic LionOP
Hello, so I have a server side action which I use to retrieve data for my gallery page, I have another feature in the gallery page which allows the users to like a item. Upon liking the like count gets updated locally and on the database, but when I refresh to page to see the database updated like count it doesn't gets updated and remains same. I am using drizzle orm along with noendb, I've already have a talk with neon team stating that they aren't caching the data anywhere

11 Replies

Asiatic LionOP
The like count is updated in the db and in the drizzle studio
@@ts-ignore add `export const dynamic = 'force-dynamic'` to your page
Asiatic LionOP
I have tried it, doesn't seems to work
@Asiatic Lion I have tried it, doesn't seems to work
how are you fetching the data
show the code
@@ts-ignore how are you fetching the data
Asiatic LionOP
Using a server action
'use client';

import InfiniteScroll from 'react-infinite-scroller';
import Grid from './ui/Grid';
import GalleryItem from './GalleryItem';
import {
  products,
  userLikedProducts as userLikedProductsSchema,
} from '@/lib/db/schema';
import { Skeleton } from '@nextui-org/react';
import { toast } from 'sonner';
import { getGalleryItems } from '@/app/actions';
import { useState } from 'react';

export const dynamic = 'force-dynamic'

const Loader = (
  <Grid className="mt-6">
    {new Array(3).fill(0).map((_, i) => {
      return <Skeleton key={i} className="h-[300px] w-auto rounded-xl" />;
    })}
  </Grid>
);

const InfiniteGridGallery = ({
  userLikedProducts,
}: {
  userLikedProducts: Array<typeof userLikedProductsSchema.$inferSelect>;
}) => {
  const [items, setItems] = useState<Array<typeof products.$inferSelect>>([]);
  const [currentPage, setCurrentPage] = useState(1);
  const [loading, setLoading] = useState(false);
  const [hasMore, setHasMore] = useState(true);

  const fetchItems = async () => {
    if (loading) return;

    setLoading(true);

    try {
      const products = await getGalleryItems(currentPage, 9);
      if (products.length === 0) {
        setHasMore(false);
      } else {
        setItems([...items, ...products]);
        setCurrentPage(currentPage + 1);
      }
    } catch (err: any) {
      toast.error(err.message);
    } finally {
      setLoading(false);
    }
  };

  return (
    <InfiniteScroll loadMore={fetchItems} hasMore={hasMore} loader={Loader}>
      <Grid>
        {items.map((item, i) => (
          <GalleryItem
            key={i}
            item={item}
            likedByUser={
              userLikedProducts.filter(
                (product) => product.productId === item.id,
              ).length > 0
            }
          />
        ))}
      </Grid>
    </InfiniteScroll>
  );
};

export default InfiniteGridGallery;
I've tested the force-dynamic on both the page and component
Answer
ig you need this