Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Stale content in next dev mode #40

Open
glomotion opened this issue Feb 16, 2025 · 2 comments
Open

Stale content in next dev mode #40

glomotion opened this issue Feb 16, 2025 · 2 comments

Comments

@glomotion
Copy link

glomotion commented Feb 16, 2025

Hello there πŸ‘‹

First of all, thank you for this project. It's amazing. ❀ It feels so good to be able to consume WP content with nextjs.

So i'm having some problems trying to work around the rather aggressive caching that nextjs does while developing locally. πŸ˜…

For example, when running the project locally, it would be amazing to be able to easily disable caching so that I can immediately see updates made to the wordpress cms.

// app/posts/[slug]/page.tsx
export const revalidate = 0;

i've also tried adding:

// app/posts/[slug]/page.tsx
export const revalidate = 0;
export const dynamic = "force-dynamic";

And i have tried setting:

// lib/wordpress.ts
...
const defaultFetchOptions: FetchOptions = {
  next: {
    tags: ["wordpress"],
    // revalidate: 3600, // Revalidate every hour by default
    revalidate: 0,
    cache: "no-store",
  },
};

Still, i cannot get nextjs to purge cache and show me non-stale wordpress post content. :(

Ive also tried creating an api route to purge cache - this does not work either (i refresh the page after calling this API - and nothing changes - i still see stale WP post content

// app/api/clear-cache/route.tsx
import type { NextRequest } from "next/server";
import { revalidateWordPressData } from "../../../lib/wordpress";

export async function POST(request: NextRequest) {
  try {
    await revalidateWordPressData([
      "wordpress",
      "post",
      "posts",
      "post-hello-world",
    ]);
    console.log("@@@@@@@@@ CACHE CLEARED @@@@@@@@@");
    return Response.json({ message: "Successfully revalidated all WP data" });
  } catch (e: unknown) {
    console.error("Cache clear error:", e);
    return Response.json({ message: "Failed to clear cache" }, { status: 500 });
  }
}

About the only thing that has worked so far is to modify the base wordpressFetch function:

// lib/wordpress.ts
async function wordpressFetch<T>(
  url: string,
  options: FetchOptions = {}
): Promise<T> {
  const userAgent = "Next.js WordPress Client";
  // Add timestamp to URL to prevent caching
  const timestampedUrl = `${url}${
    url.includes("?") ? "&" : "?"
  }_ts=${Date.now()}`;

  const response = await fetch(timestampedUrl, { // modify the url each time its being made, to ensure its never cached.
    ...defaultFetchOptions,
    ...options,
    headers: {
      "User-Agent": userAgent,
    },
  });

But this solve feels a little like the nuclear option. Is there something i'm missing?

@glomotion glomotion changed the title Problems with caching Problems with local dev & caching Feb 16, 2025
@glomotion glomotion changed the title Problems with local dev & caching Problems with local dev caching Feb 16, 2025
@glomotion
Copy link
Author

I've also tried killing the server and restarting after deleting the whole .next folder, this still does not update stale content.

@glomotion
Copy link
Author

glomotion commented Feb 16, 2025

Near as I can tell - when running in dev mode nextjs 15 shouldn't be caching any fetch data, but this definitely does not seem to be the case for me.

@glomotion glomotion changed the title Problems with local dev caching Stale content in next dev mode Feb 18, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant