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

usage with swr not working #107

Open
lukvermeulen opened this issue Apr 27, 2022 · 7 comments
Open

usage with swr not working #107

lukvermeulen opened this issue Apr 27, 2022 · 7 comments
Assignees
Labels
bug Something isn't working

Comments

@lukvermeulen
Copy link

lukvermeulen commented Apr 27, 2022

Hey, this looks like an awesome utility!

This also works out of the box when using fetch directly, however it appears to not work with my usage of swr.

What would be the best approach to debug this and see why this is not working?

Thanks in advance!

Some snippets for reference:

// the api call does not get intercepted
 const {
    channels,
    isLoading,
    isError,
    mutate: mutateChannels,
  } = useChannels();

// this api call does get intercepted
  useEffect(() => {
    async function getChannels() {
      const response = await fetch("http://localhost:3030/channels");
      console.log(await response.json());
    }
    getChannels();
  }, []);

the useChannels hook:

const channelsFetcher = () => {
  return client
    .service("channels")
    .find()
    .then((res) => {
      return res.data;
    });
};

export default function useChannels() {
  const { data, error, mutate } = useSWR<Channel[]>(
    "channels",
    channelsFetcher
  );

  return {
    channels: data,
    isLoading: !error && !data,
    isError: error,
    mutate: mutate,
  };
}
@lukvermeulen lukvermeulen changed the title Usage with swr usage with swr not working Apr 27, 2022
@GunsStarR
Copy link

Hi @lukvermeulen! Facing the same issue with send XHR through superagent. Did you find solution? Or used something else?

@lukvermeulen
Copy link
Author

Hey @GunsStarR, I ended up using "Mock Service Worker", after following this article. It works quite well for me.

@nutboltu nutboltu self-assigned this May 17, 2022
@nutboltu nutboltu added the bug Something isn't working label May 17, 2022
@nutboltu nutboltu added this to To do in Milestone #1 via automation May 17, 2022
@nutboltu nutboltu added needs investigation and removed bug Something isn't working labels May 17, 2022
@nutboltu
Copy link
Collaborator

@lukvermeulen I tried to run swr with storybook-addon-mock and it works properly. I have used their basic example for testing.

Looking at your code, can you tell me which type of client is this? is it going to call a fetch or XHR request?

const channelsFetcher = () => {
  return client
    .service("channels")
    .find()
    .then((res) => {
      return res.data;
    });
};

@nutboltu nutboltu added bug Something isn't working and removed needs investigation labels May 17, 2022
@nutboltu
Copy link
Collaborator

@GunsStarR it's confirmed that there's a bug in the package which breaks the superagent. You will get the fix in the upcoming release.

@nutboltu
Copy link
Collaborator

@GunsStarR this fix is released in version 2.4.0. Can you please check?

@lukvermeulen
Copy link
Author

lukvermeulen commented May 29, 2022

Hi @nutboltu, my client is configured to use native window.fetch

@pjaws
Copy link

pjaws commented Aug 30, 2022

I'm using swr along with axios in a custom fetcher and also experiencing issues. I posted a detailed comment in another issue about relative URLs here.

I'm using version 2.4.1.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
Development

No branches or pull requests

4 participants