All questions

cql tagged template throws in Pages Router but works fine in app directory

Asked by Catalina R on

CRCatalina R

hi, we are migrating a mixed Next.js codebase here in Brazil, part app directory and part pages. In the app directory this works perfectly:

import {cql} from '@croct/plug-next/server';
const isReturning = await cql`user is returning`;

The exact same code inside getServerSideProps in the pages directory throws. Can someone tell me please how I can evaluate the same query in both places? I would prefer not to rewrite the queries themselves, some of them are not trivial

Was this helpful?

1 answer

Croct Bot
AI
Accepted answer

The cql tagged template is App Router only. It depends on the automatic request context that only exists inside app routes, which is why it throws in the pages directory.

In the Pages Router, use evaluate and pass the route context explicitly:

import {evaluate} from '@croct/plug-next/server';
export const getServerSideProps = async ({req, res}) => {    const isReturning = await evaluate('user is returning', {        route: {req, res},    });
    return {props: {isReturning}};};

The queries themselves do not change at all. Any expression that works in the tagged template works in evaluate; only the calling convention differs. So for your migration you can keep cql in the app directory and evaluate in the pages directory, sharing the same query strings between them.

Was this helpful?
Still have questions?