Next.js Discord

Discord Forum

Missing Suspense boundary with useSearchParams (Component is surrounded with Suspense)

Answered
West African Lion posted this in #help-forum
Open in Discord
West African LionOP
Answered by West African Lion
https://nuqs.47ng.com/docs/troubleshooting#client-components-need-to-be-wrapped-in-a-suspense-boundary seems like I need to wrap a ton of components with suspense then...
View full answer

18 Replies

West African LionOP
I do want this to be a client component, and it shouldn't be statically rendered afaik?
https://nextjs.org/docs/messages/missing-suspense-with-csr-bailout afaik, im following this correctly? I have next@15.2.3 installed
West African LionOP
man šŸ˜­
ā”‚ Export encountered an error on /_not-found/page: /_not-found, exiting the build. I don't have a custom not-found page
Solved it by surrounding my app with Suspense in the layout.tsx, and adding unstable_noStore() to my auth/cli page
Is that the correct way?
I want to avoid a black page if possible, which suspend adds
no... thats not how its usually fixed

are you sure this is the only spot where you use useSearchParams?
West African LionOP
I also use a posthog provider, which uses the search params
"use client";

import { usePathname, useSearchParams } from "next/navigation";
import { useEffect, Suspense } from "react";
import { usePostHog } from "posthog-js/react";

function PostHogPageView(): null {
  const pathname = usePathname();
  const searchParams = useSearchParams();
  const posthog = usePostHog();

  // Track pageviews
  useEffect(() => {
    if (pathname && posthog) {
      let url = window.origin + pathname;
      if (searchParams.toString()) {
        url = `${url}?${searchParams.toString()}`;
      }

      posthog.capture("$pageview", { $current_url: url });
    }
  }, [pathname, searchParams, posthog]);

  return null;
}

// Wrap this in Suspense to avoid the `useSearchParams` usage above
// from de-opting the whole app into client-side rendering
// See: https://nextjs.org/docs/messages/deopted-into-client-rendering
export default function SuspendedPostHogPageView() {
  return (
    <Suspense fallback={null}>
      <PostHogPageView />
    </Suspense>
  );
}
this was directly from the docs
Maybe it's coming from a library? (nuqs, maybe)
Oh boy
West African LionOP
https://nuqs.47ng.com/docs/troubleshooting#client-components-need-to-be-wrapped-in-a-suspense-boundary seems like I need to wrap a ton of components with suspense then...
Answer
@West African Lion https://nuqs.47ng.com/docs/troubleshooting#client-components-need-to-be-wrapped-in-a-suspense-boundary seems like I need to wrap a ton of components with suspense then...
yea you either need to make your pages dynamic, or use Suspense with a suitable loading state for every place you use nuqs
because nuqs also uses useSearchParams which requires Suspense in static pages
West African LionOP
Alright, thank you