Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Open
610 opened this issue Oct 16, 2022 · 3 comments
Open

Comments

@610
Copy link

610 commented Oct 16, 2022

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:

  • @bigcommerce/storefront-data-hooks@^1.7.0
  • next@^12.3.1
  • react@^18.1.0
  • swr@^1.3.0
@610
Copy link
Author

610 commented Oct 17, 2022

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 610 closed this as completed Oct 17, 2022
@610
Copy link
Author

610 commented Oct 17, 2022

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 610 reopened this Oct 17, 2022
@610
Copy link
Author

610 commented Oct 17, 2022

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant