Next.js Discord

Discord Forum

revalidatePath() Not working initially until x amount posts

Unanswered
Blue orchard bee posted this in #help-forum
Open in Discord
Blue orchard beeOP
Revalidation & Pagination
is there a work around for revalidatePath()? It starts to work properly only x amount of posts since I'm fetching a limited amount of data.

3 Replies

Blue orchard beeOP
export const fetchAllReviews = async (page, limit) => {
  try {
    const skip = (page - 1) * limit;
    await connectToDB();
    const reviews = await Review.find()
      .sort({ createdAt: -1 })
      .limit(limit)
      .skip(skip);
    if (!reviews) return { message: 'No reviews found', status: 404 };

    const plainReviews = convertObjectIdsToString(reviews);
    return plainReviews;
  } catch (error) {
    console.error('Error during GET:', error);
    return { message: 'Server error', status: 500 };
  }
};
const handleReview = async (id, formData, isEdit = false) => {
  const session = await getServerSession(authOptions);
  const userId =
    session?.user?.userId || new mongoose.Types.ObjectId().toString();
  const { content, summonerName, summonerTag, username, region, rating } =
    await extractFormData(formData);

  const validationError = validateReview(
    content,
    summonerName,
    summonerTag,
    username,
    rating
  );
  if (validationError) {
    return { error: validationError };
  }

  try {
    let review;
    if (isEdit) {
      review = await Review.findById(id);
      if (!review) {
        return { error: 'Review not found', status: 404 };
      }
    } else {
      review = new Review({
        creator: userId,
        content,
        summonerName,
        summonerTag,
        username,
        region,
        rating,
      });
    }

    Object.assign(review, { content, summonerName, summonerTag, rating });

    await review.save();
    revalidatePath('/');
    revalidatePath('/reviews');

    return review;
  } catch (error) {
    console.error(`Error during ${isEdit ? 'PUT' : 'POST'}:`, error);
    return {
      error: `Failed to ${isEdit ? 'edit' : 'create'} review`,
      status: 500,
    };
  }
};
this is only happening to this fetch since i have a limit param. if i increase or decrease the limit the revalidation doesnt start working until that x amount of limit is met then it starts working properly after every new post