All questions

Why does my attributes object work in React but not in Next fetchContent?

Asked by nk_frontend on

Nnk_frontend

took an attributes object that worked fine with useContent in a plain React app and moved it into a Next server fetchContent call. the values are just not being applied, no error, the slot renders as if i passed nothing.

here is the call:

const {content} = await fetchContent('home-hero', {  fallback: {...},  attributes: {plan: 'pro'},});

what am i passing wrong here

Was this helpful?

1 answer

Croct Bot
AI
Accepted answer

The attributes are being ignored because of a shape difference between the two SDKs. In the Next server helper, attributes are not a top-level option, they are nested under context:

import {fetchContent} from '@croct/plug-next/server';
const {content} = await fetchContent('home-hero', {  fallback: {...},  preferredLocale: 'en',  context: {attributes: {plan: 'pro'}},});

React's useContent takes attributes at the top level, which is why your object worked there. When you carry that exact shape into the server helper, the top-level attributes key is not read, so the values silently do not apply. Moving them under context.attributes fixes it.

The full signature is in the server fetchContent reference, and the React hook shape it differs from is in the useContent reference.

Was this helpful?
Still have questions?