Are Plasmic sites truly using static site generation?

I’m also having the Error: NextRouter was not mounted.
On Next 13.1.6, @plasmicapp/loader-nextjs": “^1.0.232”

I’m trying to fetch some CMS data thru data component with on a static page.

It was working fine last week, but know it’s throwing this error and the data component is fetching on the client side instead that on the build side.

The index.jsx

import Head from 'next/head'
import { Inter } from '@next/font/google'
import {
  PlasmicRootProvider,
  PlasmicComponent,
  ComponentRenderData,
  extractPlasmicQueryData
} from '@plasmicapp/loader-nextjs';
import { PLASMIC } from 'plasmic-init';
import type { GetStaticPropsContext } from 'next'

const inter = Inter({ subsets: ['latin'] })

export async function getStaticProps({
  previewData,
}: GetStaticPropsContext) {

  const plasmicData = await PLASMIC.fetchComponentData("Home");
  if (!plasmicData) {
    throw new Error("No Plasmic design found");
  }

  console.log(plasmicData)
  const compMeta = plasmicData.entryCompMetas[0];

  const queryCache = await extractPlasmicQueryData(
    <PlasmicRootProvider
      loader={PLASMIC}
      prefetchedData={plasmicData}
      pageParams={compMeta.params}
    >
      <PlasmicComponent component={compMeta.displayName} />
    </PlasmicRootProvider>
  );

  return {
    props: {
      plasmicData,
      queryCache,
    },
  };
};

export default function Home(props: {plasmicData: ComponentRenderData; queryCache?: Record<string, any>,}) {
  const {plasmicData, queryCache} = props;
  const compMeta = props.plasmicData.entryCompMetas[0];

  return (
    <>
      <Head>
        <title>Create Next App</title>
        <meta name="description" content="Generated by create next app" />
        <meta name="viewport" content="width=device-width, initial-scale=1" />
        <link rel="icon" href="/favicon.ico" />
      </Head>
      <PlasmicRootProvider
        loader={PLASMIC}
        prefetchedData={plasmicData}
        prefetchedQueryData={queryCache}
        pageParams={compMeta.params}
      >
        <PlasmicComponent component={compMeta.displayName} />
      </PlasmicRootProvider>
    </>
  )
}

The data component I use in Plasmic :

import { DataProvider, usePlasmicQueryData } from '@plasmicapp/loader-nextjs';
import { ReactNode } from 'react';
import { createClient } from '../prismicio'
import { PrismicPageListType } from './PrismicPageList';

export default function PageDataProvider(props: { children?: ReactNode; page: PrismicPageListType }) {
  const { children, page } = props;

  const data  = usePlasmicQueryData(/${page}, async () => {
    const client = createClient()
    if(page === "categories" || page === "peintures"){
        return await client.getAllByType(page);
    }
    return await client.getSingle(page);
  });

  return (
      <DataProvider name={page} data={data}>
        {children}
      </DataProvider>
  );
}

The way it’s declared

PLASMIC.registerComponent(PageDataProvider, {
  name: "Page Data Provider",
  props: {
    children: 'slot',
    page: {
      type: 'choice',
      options: PrismicPageListArray
    }
  },
  providesData: true
})

@chungwu @yang
Do you know if it’s a mistake on my side or it’s plasmic related ?
I’m trying to make the data fetching of my code component happen at build time using usePlasmicQueryData

Hi folks, sorry for the issue. We’ve reproduced this but are still investigating. We do believe it’s likely Plasmic related. We’ll update this as we have more information.

Just to make sure, can everyone confirm the following?

  1. You’re using pages/ dir (not the new app/ dir).
  2. This is not breaking your app, it’s just a new error that prints out during SSG/SSR.

Confirming on my side

  1. yes
  2. indeed
    For us it also happens on just one site. A site that uses antd accordion and react slider. Not sure if related, but that’s the only difference from that app with the other ones

The NextRouter was not mounted. issue should be fixed now

Nice ! I will test that by the end of the week

You may need to run publish again for it to take effect.
Error stopped for us too :+1:

Works fine, thanks again !

Could it have prevented SSG/SSR data fetching on code component using usePlasmicQueryData and force data to be fetched client-side ?
Or it was an unrelated issue ?

Now everything with working perfectly, the data is fetched at built time (as it was 1 week ago).

Correct, I now believe https://plasmiccommunity.slack.com/archives/C0128PVPESU/p1677620549169049?thread_ts=1669592260.532829&cid=C0128PVPESU is inaccurate.

The bug was causing extractPlasmicQueryData to fail, so users using usePlasmicQueryData were not getting server-side data fetching (and forcing data to be fetched client-side).

Hi @chungwu I went to investigate why my pages aren’t being served using SSG , and noticed this error in my server side console:

PLASMIC: Encountered error when pre-rendering Search: Error: Error: NextRouter was not mounted. <https://nextjs.org/docs/messages/next-router-not-mounted>

Looks similar to the bug in the thread above—is there something I can do to fix this?

are you using useRouter() in your components somewhere? if that usage is not important to what data should be pre-fetched, you can surrounding in a try/catch:

function useRouterSafe() {
  try {
    return useRouter();
  } catch (err) {
    return undefined;
  }
}

and handle the undefined case.

If you do need to know what the url params are, you can read it like this…

function useRouterParams() {
  try {
    return useRouter().params;
  } catch (err) {
    return useSelector("params");
  }
}

The params read from useSelector() is passed into <PlasmicRootProvider/> in getStaticProps().

ah that could be it

That worked, thanks!