All questions

attributes option ignored in fetchContent but works in useContent

Asked by oskar_nw on

Ooskar_nw

working on a client project at our agency, passing attributes to influence the content resolution. on the client this works fine:

const content = useContent('home-hero', {    attributes: {plan: 'enterprise'},});

so i did the same thing on the server:

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

no error, but the attirbutes seem to be completely ignored, the audience that depends on them never matches. same slot, same attributes, only difference is server vs client. thanks in advance everyone

Was this helpful?

1 answer

Rrafal_k

You hit a real shape difference between the two APIs. The React useContent hook takes attributes at the top level, but the Next.js server-side fetchContent expects them nested under context:

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

The full server signature is fetchContent('home-hero', {fallback, preferredLocale, context: {attributes}}). Your top-level attributes key was silently ignored because the server options object simply does not have that field, which is why there was no error to point you at the problem.

Was this helpful?
Ooskar_nw

ah that explains it, moved them under context and the audience matches now. thanks!

Still have questions?