bigcommerce / storefront-data-hooks

Hooks for React Storefront UI Components
MIT License
165 stars 36 forks source link

useCustomer and other hooks return undefined even though api returns data #131

Open 610 opened 1 year ago

610 commented 1 year ago

I'm having a problem using storefront hooks. After successful SSO login and having all the api endpoints on NextJS application for BigCommerce in place, the hooks return undefined result on the first page load, even though if I look into the Network tab of the browser, I can see SWR does successful requests to /api/bigcommerce/customers and this endpoint returns logged in user data.

Whats interesting, if I update a code and NextJS running in development mode will refresh the page in browser automatically, the hook returns data as expected. But then if I refresh the page manually again, it returns undefined.

const { data: customer } = useCustomer(); // It gets invoked in the component at least 2 times.
console.log(customer); // <--- is undefined on first page load, and returns data only if NextJS
                       // refreshes the page automatically after code change.

On the first call of useCustomer I can see api request in the Network tab to the /api/bigcommerce/customers endpoint, which returns expected data.

I have also tried to revalidate the cache, but it also doesn't help.

const { data: customer, revalidate } = useCustomer();

revalidate().then(() => {
  console.log(customer);
});

The same problem with useCart hook.

It looks like hooks don't revalidate SWR cache properly. Does anyone experience the same issue? I have already read all issues/topics and debugged everything as much as possible.

Package versions:

610 commented 1 year ago

This has nothing to do with storefront-data-hooks, that appears to be the same behavior for all useSWR hooks I have. Closing this issue.

610 commented 1 year ago

Nope, wrong again :) My other useSWR hooks work in all components. Custom implemented useCustomer also works. But whenever I try to use useCustomer from storefront-data-hooks - it always returns undefined.....

610 commented 1 year ago

I noticed that swr in storefront-data-hooks is very outdated - 0.3.6, which has been released more than 2 years ago. The same applies to the https://github.com/storyblok/nextjs-bigcommerce-starter template, which is using the older swr and nextjs 10. Could it be because of version incompatibility? I'm using the latest next and swr in my project.