0

I have created an nextjs 15 app and I have redux toolkit used in it. When I run the build, it throws an error

Error occurred prerendering page "/_not-found". Read more: https://nextjs.org/docs/messages/prerender-error
Error: Failed to fetch data
    at i.execute (/Users/vishnucr/Projects/mise811-sales/.next/server/app/_not-found/page.js:1:3366)
    at process.processTicksAndRejections (node:internal/process/task_queues:95:5)
    at async /Users/vishnucr/Projects/mise811-sales/.next/server/app/_not-found/page.js:1:3955
Export encountered an error on /_not-found/page: /_not-found, exiting the build.
 ⨯ Static worker exited with code: 1 and signal: null

I found the StoreProvider created for Redux Store is the issue, may be I did it wrong. Below is my app structure. It uses AppRouter.

Layout.tsx

import type { Metadata } from "next";
import { Geist, Geist_Mono } from "next/font/google";
import "./globals.css";
import Launch from "./launch";

const geistSans = Geist({
  variable: "--font-geist-sans",
  subsets: ["latin"],
});

const geistMono = Geist_Mono({
  variable: "--font-geist-mono",
  subsets: ["latin"],
});

export const metadata: Metadata = {
  title: "Create Next App",
  description: "Generated by create next app",
};

export default function RootLayout({
  children,
}: Readonly<{
  children: React.ReactNode;
}>) {
  return (
    <html lang="en">
      <body
        className={`${geistSans.variable} ${geistMono.variable} antialiased`}
      >
        <Launch>{children}</Launch>
      </body>
    </html>
  );
}

Launch.tsx

import React from "react";
import StoreProvider from "@/lib/store/providers/storeProvider";

interface LaunchProps {
  children: React.ReactNode;
}

const Launch = ({ children }: LaunchProps) => {
  /* fetch sales and populate */
  return <StoreProvider>{children}</StoreProvider>;
  // return children;
};

export default Launch;

StoreProvider.tsx

"use client";
import { useRef } from "react";
import { Provider } from "react-redux";
import { makeStore, AppStore } from "@/lib/store/store";

export default function StoreProvider({
  children,
}: {
  children: React.ReactNode;
}) {
  const storeRef = useRef<AppStore>(undefined);
  if (!storeRef.current) {
    // Create the store instance the first time this renders
    storeRef.current = makeStore();
  }

  return <Provider store={storeRef.current}>{children}</Provider>;
}

If I Removed the store provider and returns the {children}, build will be created.

What am I doing wrong here, is This how the store is suppose to be used in NextJS ?

3
  • Fixed the issue. added page extentions in next config Commented Feb 1 at 19:07
  • Can you explain how to fix it. I am also facing same issue. pageExtensions: ['tsx', 'ts', 'jsx', 'js'], Commented Feb 21 at 6:25
  • 1
    @DeepakJha use the below code in your parent layout and try. export const dynamic = 'force-dynamic'; Commented Apr 8 at 5:41

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.