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

Location of 'await' in examples/next.js/pages/login.jsx #329

Closed
davebm1 opened this issue Apr 16, 2021 · 3 comments
Closed

Location of 'await' in examples/next.js/pages/login.jsx #329

davebm1 opened this issue Apr 16, 2021 · 3 comments

Comments

@davebm1
Copy link

davebm1 commented Apr 16, 2021

Should lines 24/25 of examples/next.js/pages/login.jsx read :-
...
24 mutateUser(
25 await fetchJson("/api/login", {
...

and not :-
...
24 await mutateUser(
25 fetchJson("/api/login", {
...

I was getting intermittent login problems on a slower connection. Making the above change resolved this.

Thanks...

@vvo
Copy link
Owner

vvo commented May 6, 2021

@davebm1 I think you're right but I never got really down to the root cause/which way we should do mutate/fetch.

Can you investigate the root cause and what's the best way to do "mutate local + fetch"? We're using https://github.com/vercel/swr

It's true that on my own app code I tend to do mutate and then await fetch instead of await end then fetch.

Then we can safely update the example code here and in the Next.js repo (https://github.com/vercel/next.js/blob/canary/examples/with-iron-session/pages/login.js)

@davebm1
Copy link
Author

davebm1 commented May 7, 2021

The promise is returned from fetch, not from swr, so mutate..await..fetch seems the better option.

This approach seems to be supported on the swr npm page https://www.npmjs.com/package/swr
with a couple of examples:-

Section: Mutate Based on Current Data:-

mutate('/api/users', async users => {
const user = await fetcher('/api/users/1')
return [user, ...users.slice(1)]
})

and
section: Prefetching Data:-

function prefetch() {
mutate('/api/data', fetch('/api/data').then(res => res.json()))
// the second parameter is a Promise
// SWR will use the result when it resolves
}

The next-iron-session example also uses await this way for logout in
https://github.com/vvo/next-iron-session/blob/master/examples/next.js/components/Header.jsx

onClick={async (e) => {
e.preventDefault();
mutateUser(
await fetchJson("/api/logout", { method: "POST" }),
false,
);
router.push("/login");
}}

I was getting intermittent authentications problems with await..mutate..fetch with failure to redirect
after authentication. This was resolved by changing to mutate..await..fetch.

Regards...

@vvo vvo closed this as completed in fee7448 Jun 8, 2021
@vvo
Copy link
Owner

vvo commented Jun 8, 2021

Thanks, just updated the examples 🙏

kodiakhq bot pushed a commit to vercel/next.js that referenced this issue Jun 8, 2021
…example link (#25889)

This updates the with-iron-session example with:
- updated way to mutate + fetch (re: vvo/iron-session#329)
- updated readme to include TypeScript example link

Requirements:
- [x] Make sure the linting passes
flybayer pushed a commit to blitz-js/next.js that referenced this issue Jun 16, 2021
…example link (vercel#25889)

This updates the with-iron-session example with:
- updated way to mutate + fetch (re: vvo/iron-session#329)
- updated readme to include TypeScript example link

Requirements:
- [x] Make sure the linting passes
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

2 participants