good-idea / sane-shopify

MIT License
212 stars 15 forks source link

Next.js API timeout #148

Closed valse closed 3 years ago

valse commented 3 years ago

Hi, I'm trying your solution and it works well until the update from Shopify to Sanity by its webhooks: I set the API endpoints in a Next.js project and I deployed it to Vercel; If I check the functions log I have a timeout error like this one:

[POST] /api/onProductUpdate Task timed out after 10.01 seconds

Do you have any suggestions?

Thanks valse

good-idea commented 3 years ago

Hi @valse , sorry for the slow response over here.

The webhook endpoints sometimes need a little time to fetch up-to-date data. After receiving a call to onProductUpdate, it fetches the referenced product using the Storefront API -- which, I have noticed, sometimes returns stale data. If the data received has an updatedAt that is older than the date provided by the call to the webhook, it will wait for several seconds before trying to refetch. It will continue waiting & refetching until it has received up-to-date data, or until it reaches 5 retries.

You can enable debugging to get more detailed output on what is happening, see the new docs:

https://github.com/good-idea/sane-shopify#debugging

Also note that in the latest update (0.20.0) the config for webhooks has changed. Instead of this:

const config = {
  sanity: {
    // ...
  },
  shopify: {
    // ...
  }
};

const onError = error => {
  // ...
};

const webhooks = createWebhooks({ config, onError });

Do this:


const onError = error => {
  // ...
};

const config = {
  secrets: {
    sanity: {
      // ...
    },
    shopify: {
      // ...
    }
  }
  onError: handleError
};

const webhooks = createWebhooks(config)

onError is optional - you can use it to report to a service like Sentry, etc. You should see error messages in your logs wether you include it or not.