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

feat(react): create client tailored to React #1473

Merged
merged 22 commits into from
Jun 11, 2021

Conversation

balazsorban44
Copy link
Member

@balazsorban44 balazsorban44 commented Mar 8, 2021

What:

These changes ensure that we work more tightly with React that can also result in unforeseen performance boosts. In case we would decide on expanding to other libraries/frameworks, a new file per framework could be added.

Why:

Some performance issues (#844) could only be fixed by moving more of the client code into the Provider.

How:

Refactoring next-auth/client

Related: #1461, #1084, #1462

BREAKING CHANGE:
1. next-auth/client is renamed to next-auth/react.

2. In the past, we exposed most of the functions with different names for convenience. To simplify our source code, the new React specific client code exports only the following functions, listed with the necessary changes:

  • setOptions: Not exposed anymore, use SessionProvider props
  • options: Not exposed anymore, use SessionProvider props
  • session: Rename to getSession
  • providers: Rename to getProviders
  • csrfToken: Rename to getCsrfToken
  • signin: Rename to signIn
  • signout: Rename to signOut
  • Provider: Rename to SessionProvider

3. Provider changes.

  • Provider is renamed to SessionProvider
  • The options prop is now flattened as the props of SessionProvider.
  • clientMaxAge has been renamed to staleTime.
  • keepAlive has been renamed to refetchInterval.
    An example of the changes:
- <Provider options={{clientMaxAge: 0, keepAlive: 0}}>{children}</Provider>
+ <SessionProvider staleTime={0} refetchInterval={0}>{children}</SessionProvider> 

4. It is now required to wrap the part of your application that uses useSession into a SessionProvider.

Usually, the best place for this is in your pages/_app.jsx file:

import { SessionProvider } from "next-auth/react"

export default function App({
  Component,
  pageProps: { session, ...pageProps }
}) {
  return (
    // `session` comes from `getServerSideProps` or `getInitialProps`.
    // Avoids flickering/session loading on first load.
    <SessionProvider session={session}>
      <Component {...pageProps} />
    </SessionProvider>
  )
}

These changes ensure that we work more tightly with React
that can also result in unforeseen performance boosts.
In case we would decide on expanding
to other libraries/frameworks, a new file per framework could be added.
We might also want to update the import
from `next-auth/client` to `next-auth/react`.

BREAKING CHANGE: Some performance issues (#844)
could only be fixed by moving setting up event listeners
inside `Provider`. This breaks the current tab/window sync behavior
for users who don't use `Provider` in their app. For these users,
wrap your app in `<Provider>` at a level above all of  your `useSession` calls.
If you don't care about tab/window syncing, this might not be needed,
but still recommended.
@vercel
Copy link

vercel bot commented Mar 8, 2021

This pull request is being automatically deployed with Vercel (learn more).
To see the status of your deployment, click below or on the icon next to each commit.

🔍 Inspect: https://vercel.com/nextauthjs/next-auth/G9wkLL8BcqcqvAyACVcfM9dzRH7d
✅ Preview: https://next-auth-git-feature-split-react-client-nextauthjs.vercel.app

@github-actions github-actions bot added client Client related code core Refers to `@auth/core` labels Mar 8, 2021
@balazsorban44 balazsorban44 self-assigned this Mar 25, 2021
@vercel vercel bot temporarily deployed to Preview March 26, 2021 15:28 Inactive
@github-actions github-actions bot added docs Relates to documentation pages providers labels Mar 26, 2021
@stale stale bot added the stale Did not receive any activity for 60 days label May 27, 2021
@nextauthjs nextauthjs deleted a comment from stale bot May 28, 2021
@stale stale bot removed the stale Did not receive any activity for 60 days label May 28, 2021
Conflicts:
	package-lock.json
	package.json
	src/providers/faceit.js
	src/providers/index.js
	src/providers/instagram.js
	src/providers/zoho.js
	src/server/index.js
	src/server/lib/oauth/client.js
	www/docs/configuration/providers.md
	www/docs/providers/faceit.md
	www/docs/providers/zoho.md
	www/package-lock.json
@vercel vercel bot temporarily deployed to Preview June 9, 2021 13:00 Inactive
@github-actions github-actions bot removed the docs Relates to documentation label Jun 9, 2021
@vercel vercel bot temporarily deployed to Preview June 9, 2021 13:03 Inactive
@vercel vercel bot temporarily deployed to Preview June 10, 2021 13:09 Inactive
@vercel vercel bot temporarily deployed to Preview June 10, 2021 13:11 Inactive
@vercel vercel bot temporarily deployed to Preview June 10, 2021 13:19 Inactive
@balazsorban44 balazsorban44 marked this pull request as ready for review June 10, 2021 18:20
@balazsorban44 balazsorban44 requested a review from 0ubbe as a code owner June 10, 2021 18:20
Copy link
Contributor

@hboylan hboylan left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks good so far!

www/docs/tutorials/securing-pages-and-api-routes.md Outdated Show resolved Hide resolved
@vercel vercel bot temporarily deployed to Preview June 10, 2021 19:59 Inactive
@codecov-commenter
Copy link

codecov-commenter commented Jun 10, 2021

Codecov Report

Merging #1473 (77b06fe) into next (0c17af9) will decrease coverage by 0.34%.
The diff coverage is 82.97%.

Impacted file tree graph

@@            Coverage Diff            @@
##             next   #1473      +/-   ##
=========================================
- Coverage   10.05%   9.71%   -0.35%     
=========================================
  Files          81      82       +1     
  Lines        1382    1390       +8     
  Branches      387     381       -6     
=========================================
- Hits          139     135       -4     
- Misses       1022    1034      +12     
  Partials      221     221              
Impacted Files Coverage Δ
src/providers/dropbox.js 0.00% <ø> (ø)
src/providers/instagram.js 0.00% <ø> (ø)
src/client/react.js 82.97% <82.97%> (ø)
src/providers/azure-ad-b2c.js 0.00% <0.00%> (ø)
src/providers/azure-ad.js 0.00% <0.00%> (ø)

Continue to review full report at Codecov.

Legend - Click here to learn more
Δ = absolute <relative> (impact), ø = not affected, ? = missing data
Powered by Codecov. Last update 0c17af9...77b06fe. Read the comment docs.

@vercel vercel bot temporarily deployed to Preview June 10, 2021 21:34 Inactive
Copy link
Contributor

@hboylan hboylan left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I used npm link for my existing project to confirm this solves the issue of the unnecessary client-side session call. 🎉

I had to install nodemailer, which seems like it only be an peerOptionalDependency though.

src/client/react.js Show resolved Hide resolved
@balazsorban44
Copy link
Member Author

@hboylan yes, from version 4, nodemailer will be optional, as it's only needed if someone uses the email provider. anyone else won't have to install it. smaller bundle size, faster install. 😊

Copy link
Collaborator

@0ubbe 0ubbe left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Impressive work 💯 , did a pair programming review with @balazsorban44 and in my opinion, we're good to go as a base for v4 👍🏽

@vercel vercel bot temporarily deployed to Preview June 11, 2021 19:46 Inactive
@vercel vercel bot temporarily deployed to Preview June 11, 2021 19:52 Inactive
@balazsorban44 balazsorban44 merged commit ca0ed1e into next Jun 11, 2021
@balazsorban44 balazsorban44 deleted the feature/split-react-client branch June 11, 2021 19:59
mnphpexpert added a commit to mnphpexpert/next-auth that referenced this pull request Sep 2, 2024
**What**:

These changes ensure that we work more tightly with React that can also result in unforeseen performance boosts. In case we would decide on expanding to other libraries/frameworks, a new file per framework could be added.

**Why**:

Some performance issues (nextauthjs#844) could only be fixed by moving more of the client code into the `Provider`.

**How**:

Refactoring `next-auth/client`

Related: nextauthjs#1461, nextauthjs#1084, nextauthjs#1462

BREAKING CHANGE:
**1.** `next-auth/client` is renamed to `next-auth/react`.

**2.** In the past, we exposed most of the functions with different names for convenience. To simplify our source code, the new React specific client code exports only the following functions, listed with the necessary changes:

- `setOptions`: Not exposed anymore, use `SessionProvider` props
- `options`: Not exposed anymore, use `SessionProvider` props
- `session`: Rename to `getSession`
- `providers`: Rename to `getProviders`
- `csrfToken`: Rename to `getCsrfToken`
- `signin`: Rename to `signIn`
- `signout`: Rename to `signOut`
- `Provider`: Rename to `SessionProvider`

**3.** `Provider` changes.
- `Provider` is renamed to `SessionProvider`
- The `options` prop is now flattened as the props of `SessionProvider`.
- `clientMaxAge` has been renamed to `staleTime`.
- `keepAlive` has been renamed to `refetchInterval`.
An example of the changes:
```diff
- <Provider options={{clientMaxAge: 0, keepAlive: 0}}>{children}</Provider>
+ <SessionProvider staleTime={0} refetchInterval={0}>{children}</SessionProvider> 
```

**4.** It is now **required** to wrap the part of your application that uses `useSession` into a `SessionProvider`.

Usually, the best place for this is in your `pages/_app.jsx` file:

```jsx
import { SessionProvider } from "next-auth/react"

export default function App({
  Component,
  pageProps: { session, ...pageProps }
}) {
  return (
    // `session` comes from `getServerSideProps` or `getInitialProps`.
    // Avoids flickering/session loading on first load.
    <SessionProvider session={session}>
      <Component {...pageProps} />
    </SessionProvider>
  )
}
```
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
client Client related code core Refers to `@auth/core` docs Relates to documentation pages providers test Related to testing TypeScript Issues relating to TypeScript
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants