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

useSearchParams() should be wrapped in a suspense boundary at page "/update-prompt" #90

Open
avoirsolutions opened this issue Jan 25, 2024 · 3 comments

Comments

@avoirsolutions
Copy link

avoirsolutions commented Jan 25, 2024

I get the following Error when building the solution (it works in DEV): >>>
useSearchParams() should be wrapped in a suspense boundary at page "/update-prompt". Read more: https://nextjs.org/docs/messages/missing-suspense-with-csr-bailout
Error occurred prerendering page "/update-prompt". Read more: https://nextjs.org/docs/messages/prerender-error

<<<

The documentation mentions this behavior appears when we use useSearchParams hook in a statically rendered page/route. If we want to keep this page static rendered then it says to use Suspense boundary but I cannot get any solutions to build so far.

Has anyone managed to get this to work please without disabling the rule (you can disable it by setting the missingSuspenseWithCSRBailout option to false in your next.config.js)?

I am using Next.js version 14.1.0 and React 18.

Thanks

@Felipe-dot
Copy link

Hello, I have the same error. And I fixed it, disabling this rule in the nextConfig file.

module.exports = {
experimental: {
missingSuspenseWithCSRBailout: false,
},
}

Is not the best approach. But it resolved it for me. I don`t know where i need to wrap the suspense tag, so i hope can anyone help

@kk-1590
Copy link

kk-1590 commented Feb 3, 2024

Just simply wrap the component where useSearchParams is being used in a Suspense component, here is the code -

"use client";
import { useEffect, useState,Suspense } from "react";
import { useSession } from "next-auth/react";
import { useRouter, useSearchParams } from "next/navigation";
import Form from "@components/Form";

const UpdatePrompt = () => {
  const router = useRouter();

  const { data: session } = useSession();

  const [submitting, setSubmitting] = useState(false);

  const searchParams =  useSearchParams();

  const promptId = searchParams.get("id");

  const [post, setPost] = useState({
    prompt: "",
    tag: "",
  });

  useEffect(() => {
    const getPromptDetails = async () => {
        const response = await fetch(`/api/prompt/${promptId}`)
        const data =  await response.json();

        setPost(
            {
                prompt : data.prompt,
                tag: data.tag,
            }
        )
    }
    if(promptId){
        getPromptDetails();
    }
  },[promptId]);

  const updatePrompt = async (e) => {
    e.preventDefault();
    setSubmitting(true);

    if(!promptId){
        return alert("Prompt ID  not found!");
    }

    try {
      const response = await fetch(`/api/prompt/${promptId}`, {
        method: "PATCH",
        body: JSON.stringify({
          prompt: post.prompt,
          tag: post.tag,
        }),
      });

      if (response.ok) {
        router.push("/");
      }
    } catch (error) {
      console.log(error);
    } finally {
      setSubmitting(false);
    }
  };
  

  return (
      <Form
        type="Edit"
        post={post}
        setPost={setPost}
        submitting={submitting}
        handleSubmit={updatePrompt}
      ></Form>
  );
};

const EditPrompt = () => {
    return <Suspense>
        <UpdatePrompt />
    </Suspense>
}

export default EditPrompt;

For more info : https://nextjs.org/docs/messages/missing-suspense-with-csr-bailout

@snade-dev
Copy link

Je suivais exactement le même tuto ça matche
merci

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

4 participants