slice-machine icon indicating copy to clipboard operation
slice-machine copied to clipboard

fix(adapter-next): prevent `revalidatePath` error in the slice simulator

Open angeloashmore opened this issue 1 year ago • 1 comments

Context

The Next.js slice simulator can throw an error when used in a React Server Component:

Uncaught (in promise): Error: Invariant: static generation store missing in revalidateTag _N_T_/slice-simulator

This PR prevents that error.

The Solution

Refactor <SliceSimulator> to properly pass the revalidatePath function as a prop. Passing as a prop seems to prevent the error.

The refactor allowed for removing duplicate code.

Impact / Dependencies

Existing projects with the Server Components simulator boilerplate do not need to change their code. The Next.js simulator's API did not change as of this PR.

// src/app/slice-simulator/page.tsx

import {
  SliceSimulator,
  SliceSimulatorParams,
  getSlices,
} from "@slicemachine/adapter-next/simulator";
import { SliceZone } from "@prismicio/react";

import { components } from "@/slices";

export default function SliceSimulatorPage({
  searchParams,
}: SliceSimulatorParams) {
  const slices = getSlices(searchParams.state);

  return (
    <SliceSimulator>
      <SliceZone slices={slices} components={components} />
    </SliceSimulator>
  );
}

Likewise, simulators in the Pages Router are unaffected.

Checklist before requesting a review

  • [ ] I hereby declare my code ready for review.
  • [ ] If it is a critical feature, I have added tests.
  • [ ] The CI is successful.
  • [ ] If there could backward compatibility issues, it has been discussed and planned.

angeloashmore avatar Mar 28 '24 01:03 angeloashmore

The latest updates on your projects. Learn more about Vercel for Git ↗︎

Name Status Preview Updated (UTC)
slice-machine ✅ Ready (Inspect) Visit Preview Mar 28, 2024 1:56am

vercel[bot] avatar Mar 28 '24 01:03 vercel[bot]

The correct fix is to update Next.js to the latest version. Next.js v14.1.4 is confirmed to work.

angeloashmore avatar Apr 02 '24 23:04 angeloashmore