All questions

What happens to useContent if the Croct API is unreachable?

Asked by milesr on

Mmilesr

We run a travel site and a lot of our trafic comes in on flaky mobile connections. Before we roll personalization out to the hotel landing pages I need to understand the failure mode: if the personalization call times out or the API is unreachable, does useContent throw and break the page, or is there a graceful path? These pages earn money so a broken hero is not an option. Thanks in advance for any pointers

Was this helpful?

1 answer

Jjmartell

There is a graceful path, you just have to opt into it. Pass a fallback option to the hook:

const content = useContent('hotel-hero', {    fallback: {        title: 'Find your next stay',        subtitle: 'Handpicked hotels at the best rates',    },});

On a timeout or network failure the fallback renders instead of an error, so the worst case for a user on a bad connection is seeing your default hero rather than a personalized one. Without a fallback the error propagates, so for revenue pages I would treat the option as mandatory.

If you also use the plain JS SDK anywhere, the equivalent is catching on the fetch:

const {content} = await croct.fetch('hotel-hero')    .catch(() => ({content: {title: 'Find your next stay'}}));

Same principle in both cases: personalization degrades to default content, never to a broken page.

Was this helpful?
Still have questions?