All questions

someFunction() requires specifying the route parameter, Pages Router

Asked by Jasper Boone on

JBJasper Boone

Bit of background: I spent ten years doing WordPress and moved to headless last year. Did a side project on the App Router where Croct worked great out of the box, and now my day job put me on a legacy Next.js codebase that is all Pages Router. Same package, same setup as far as I can tell, but every server-side call throws something like:

Error: fetchContent() requires specifying the route parameter outside app routes

The exact same call worked in my App Router project with no extra parameters. I searched the error and got nothing useful. Is the Pages Router just not supported, or am I missing a config step somewhere? honestly starting to think it's the old codebase cursing me

Was this helpful?

1 answer

Mmickw

Pages Router is supported, it just cannot do the magic the App Router does. In app routes the SDK resolves the request context automatically. Outside them there is no ambient request, so server functions need you to pass the route context manually, and that error is the SDK telling you it was omitted.

In getServerSideProps it looks like this:

import {fetchContent} from '@croct/plug-next/server';
export const getServerSideProps = async ({req, res}) => {    const content = await fetchContent('home-hero', {        route: {req, res},    });
    return {props: {content}};};

Same idea in API routes: hand it the request and response from the handler. Once the route context is there, everything else behaves exactly like your App Router project.

Was this helpful?
Still have questions?