By ··8 min read

Sitecore page still 404s on Next.js after publish: cached notFound

A Next.js page cached a 404 and would not recover, even though the content was published and the revalidate call said it worked. Two causes, both reproduced, and how to clear a stuck 404.

A bio page for a new person on a law firm’s site returned a 404. The content was published and I could read it straight from Experience Edge, Sitecore’s hosted delivery store. The site, a Next.js app on Vercel, kept saying the page did not exist.

Someone had already tried to fix it by calling the revalidation endpoint, and got this back:

{
"revalidation": {
"results": [{ "path": "/people/jane-doe", "revalidated": true }],
"skipped": []
}
}

revalidated: true, and the page was still a 404. Both of those were true at the same time, and the reasons are two separate cache behaviours that look like one.

How it works: ISR and the cache key

Incremental Static Regeneration (ISR) is Next.js saving the HTML of a page when it is first built or first requested, then serving the saved copy to everyone after. A page is rebuilt in two ways. Time-based: you return revalidate: 60 from getStaticProps, and once 60 seconds pass, the next request still gets the old copy while Next.js rebuilds a new one in the background. On demand: your code calls res.revalidate('/some/path') and the copy is rebuilt straight away.

A cache key is the label a saved copy is filed under. For a Pages Router page, the key is the path, plus the locale if you use internationalised routing. Two different paths are two different entries, even if they render the same content.

Sitecore XM Cloud “multisite” setups add a step. One Next.js app serves several sites, so the Sitecore Content SDK (software development kit) includes a MultisiteMiddleware. Middleware is code that runs before a request reaches a page. This one rewrites the path: the browser asks for /people/jane-doe, and Next.js is told to treat it as /_site_example/people/jane-doe, where example is the site name. The browser never sees the change.

The catch-all page runs with the rewritten path, so it files its result under the rewritten path.

A request for /people/jane-doe passes through middleware, which rewrites it to /_site_example/people/jane-doe; the page cache stores the result under the rewritten path, not the public one.
The page cache is keyed by the rewritten path, not the URL in the address bar.

Cause one: a cached 404 only heals if it has a timer

Here is the page, trimmed to what matters. When the item is missing, it returns notFound.

src/pages/[[...path]].tsx
export const getStaticPaths: GetStaticPaths = async () => ({
paths: [],
fallback: 'blocking', // render on first request, then cache
});
export const getStaticProps: GetStaticProps = async (context) => {
const page = await client.getPage(pathFromStaticContext(context), { locale: context.locale });
if (!page) {
return { notFound: true }; // cached like a normal page
}
// ...
};

Say someone visits /people/jane-doe a minute before the item reaches Edge. getPage returns nothing, notFound is returned, and Next.js stores the 404. When the item arrives, nothing tells the cache. I timed each hop in why a SitecoreAI publish takes 15 minutes to reach Sitecore Search, and Edge was the fast one at about 18 seconds.

I first wanted to know if adding revalidate to that return would let it recover, and I got it wrong. I built a throwaway page inside the real app, set revalidate: 3, removed the flag that made it return notFound, and polled for 24 seconds. It stayed a 404 every time, and I concluded that Next.js ignores revalidate on a 404.

That conclusion was wrong. The log from the test page had no entries at all: its getStaticProps had never run.

The app’s middleware matches almost every path, so it rewrote my test URL too, and the request landed on the real catch-all page, which has its own 300-second revalidate. I was timing a different page. A cache-key problem hid inside the test for a cache-key post.

So I reran it properly in a bare Next.js 15.5.12 app, using next build and next start locally, with a page that returns notFound only while a flag file exists. I made the first request while the flag existed, deleted the flag, then requested the page every three to four seconds.

Two rows. A notFound result with no revalidate stays 404 for 24 seconds of polling and getStaticProps runs once. With revalidate set to 3, the first request after three seconds gets the stale 404 and the next request gets a 200.
The same page with and without a revalidate timer on its 404.
return { notFound: true } (no revalidate)
req1 404
poll1-6 404 404 404 404 404 404 getStaticProps ran 1 time
return { notFound: true, revalidate: 3 }
req1 404
poll1 404 stale copy, rebuild starts
poll2 200 ... 200 200 200 rebuilt: the item is found

This matches the Next.js docs, which say notFound follows the same revalidate behaviour as any other result, and that revalidate defaults to false: no revalidation. So a bare notFound: true is a 404 with no expiry. I also called the revalidate endpoint on the stuck page in that bare app, and it went to a 200 straight away.

In the real code, the people-detail branch at the time returned { notFound: true } with no revalidate. That alone explains a page that does not heal on its own. It is also the cheap fix: add revalidate to every notFound.

src/pages/[[...path]].tsx
return { notFound: true };
return { notFound: true, revalidate: 60 }; // let a missed publish recover

I can’t tell you which version of the code production was running when the page stuck. The next cause is the one I can prove end to end.

Cause two: revalidating the wrong key

The webhook that Sitecore calls on publish looks up the item’s path and calls res.revalidate('/people/jane-doe'). That worked fine in the sense that it returned revalidated: true. It regenerated an entry under /people/jane-doe, which no visitor was ever served.

I reproduced it twice. First against the real app, on a local production build: revalidating the public path left the modified time of the served cache file under .next/server/pages unchanged, and wrote a new file beside it.

Then I reproduced the stuck 404 itself in the bare app, so I could show it here. This is its middleware, which does the same rewrite:

middleware.ts
import { NextResponse, type NextRequest } from 'next/server';
export function middleware(req: NextRequest) {
const url = req.nextUrl.clone();
url.pathname = `/_site_example${url.pathname}`;
return NextResponse.rewrite(url);
}
export const config = { matcher: ['/people/:path*'] };

The test was: visit /people/jane-doe while the item is missing (a 404 is cached), make the item available, call revalidate, visit again.

1 first visit, item missing 404
2 item published, visit again 404
3 res.revalidate('/people/jane-doe') {"revalidated":true}
visit 404
4 res.revalidate('/_site_example/people/jane-doe') {"revalidated":true}
visit 200

The file listing shows where each call landed. A cached 404 writes no page file to disk, so nothing appeared after steps 1 and 2. Step 3 wrote pages/people/jane-doe.html, a key no visitor is served from. Step 4 wrote pages/_site_example/people/jane-doe.html, the one the rewrite serves.

Before the fix, the publish webhook refreshes /people/jane-doe, which nobody requests, while /_site_example/people/jane-doe stays a 404. After the fix, both entries are refreshed and the served one becomes a 200.
Before and after: the fix revalidates the rewritten path as well.

The fix

The fix is to revalidate both paths: the public one, and the rewritten one. In the real app I did it in the one function that every revalidation goes through, so the webhook and the manual endpoint both get it.

src/lib/isr/revalidation.ts
export function expandMultisiteRevalidationPaths(path: string): string[] {
const variants = new Set<string>([path]);
if (!path.startsWith('/_site_')) {
const siteNames = (process.env.NEXT_PUBLIC_DEFAULT_SITE_NAME || '')
.split(',')
.map((name) => name.trim())
.filter(Boolean);
for (const siteName of siteNames) {
variants.add(`/_site_${siteName}${path === '/' ? '' : path}`);
}
}
return Array.from(variants);
}

Then revalidatePaths expands each path before it calls res.revalidate.

src/lib/isr/revalidation.ts
const normalizedPaths = Array.from(
new Set(
paths
.flatMap((path) => normalizeRevalidationPath(path) || [])
.flatMap((path) => expandMultisiteRevalidationPaths(path))
)
);

Against the real app, the call now returned both paths as revalidated, and the modified time of the served cache file changed, which it did not before. Two limits: I tested only the default locale, and a setup with several languages will have the locale in each key, so check that before trusting this. I also tested on a local production build, not on Vercel itself.

Clearing a stuck 404 right now

In this case nobody could call the revalidate endpoint, because its secret was a Vercel sensitive environment variable, which can’t be read back after it is saved. Two ways round that:

  1. Redeploy. The Vercel docs say each deployment has its own ISR cache and does not reuse the previous one, so a new deployment starts with no stored 404. This is the safest option and needs no secret. It also ships the code fix, which is the point.
  2. Replay the publish webhook. If your publish webhook calls res.revalidate and has no secret gate, send it again for the item. This only works after the both-paths fix is deployed. Before that, it returns revalidated: true and changes nothing visitors see, which is exactly what started this.

How to check it worked

Locally, build for production and start the app, since next dev renders every request fresh and hides all of this. Visit the page, then look at what Next.js wrote.

terminal
find .next/server/pages -ipath "*jane-doe*" -name "*.html" -printf "%TT %p\n"

You should see the _site_ file, and its time should change after a revalidate. A page that is still a cached 404 has no file at all. With next start, curl -I on the page shows an x-nextjs-cache header: MISS is a first visit, HIT is a saved copy, STALE is a saved copy being rebuilt. On Vercel, I did not check which headers reach you, so confirm by running your publish webhook and requesting the page again.

If the Pages editor is the problem instead, I wrote up two causes of hydration errors in the Sitecore Pages editor.

Takeaways

  • A notFound result is a cached page. Without revalidate, it has no expiry.
  • Put revalidate on every notFound that could be a timing problem, such as content that has not reached the delivery store yet.
  • revalidated: true means that path was rebuilt. It does not mean the page visitors see was.
  • A cache key is the path after middleware rewrites it. Revalidate that one, too.
  • Look at the files under .next/server/pages on a production build before guessing.
  • Test your test: if your instrumented page’s log shows it never ran, the result is about some other page.