forked from vercel/swr-site
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* fix styles; update prefetching * add docs about performance; fixes vercel#34 * fixes vercel#45 * add cssnano; fix stylesheet order * add dark mode support * upgrade theme * Apply suggestions from code review Co-authored-by: Paco <[email protected]> * remove unnecessary sentence * upgrade nextra * update theme Co-authored-by: Paco <[email protected]>
- Loading branch information
1 parent
6bb185c
commit 00cbf16
Showing
23 changed files
with
1,080 additions
and
105 deletions.
There are no files selected for viewing
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
{} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,110 @@ | ||
# Performance | ||
|
||
SWR provides critical functionality in all kinds of web apps, so **performance** is a top priority. | ||
|
||
SWR’s built-in **caching** and **[deduplication](/advanced/performance#deduplication)** skips unnecessary network requests, but | ||
the performance of the `useSWR` hook itself still matters. In a complex app, there could be hundreds of `useSWR` calls in a single page render. | ||
|
||
SWR ensures that your app has: | ||
- _no unnecessary requests_ | ||
- _no unnecessary re-renders_ | ||
- _no unnecessary code imported_ | ||
|
||
without any code changes from you. | ||
|
||
## Deduplication | ||
|
||
It’s very common to reuse SWR hooks in your app. For example, an app that renders the current user’s avatar 5 times: | ||
|
||
```jsx | ||
function useUser () { | ||
return useSWR('/api/user', fetcher) | ||
} | ||
|
||
function Avatar () { | ||
const { data, error } = useUser() | ||
|
||
if (error) return <Error /> | ||
if (!data) return <Spinner /> | ||
|
||
return <img src={data.avatar_url} /> | ||
} | ||
|
||
function App () { | ||
return <> | ||
<Avatar /> | ||
<Avatar /> | ||
<Avatar /> | ||
<Avatar /> | ||
<Avatar /> | ||
</> | ||
} | ||
``` | ||
|
||
Each `<Avatar>` component has a `useSWR` hook inside. Since they have the same SWR key and are rendered at the almost same time, **only 1 network request will be made**. | ||
|
||
You can reuse your data hooks (like `useUser` in the example above) everywhere, without worrying about performance or duplicated requests. | ||
|
||
There is also a [`dedupingInterval` option](/docs/options) for overriding the default deduplication interval. | ||
|
||
## Deep Comparison | ||
|
||
SWR **deep compares** data changes by default. If the `data` value isn’t changed, a re-render will not be triggered. | ||
|
||
You can also customize the comparison function via the [`compare` option](/docs/options) if you want to change the behavior. | ||
For example, some API responses return a server timestamp that you might want to exclude from the data diff. | ||
|
||
## Dependency Collection | ||
|
||
`useSWR` returns 3 **stateful** values: `data`, `error` and `isValidating`, each one can be updated independently. | ||
For example, if we print those values within a full data-fetching lifecycle, it will be something like this: | ||
|
||
```jsx | ||
function App () { | ||
const { data, error, isValidating } = useSWR('/api', fetcher) | ||
console.log(data, error, isValidating) | ||
return null | ||
} | ||
``` | ||
|
||
In the worst case (the first request failed, then the retry was successful), you will see 5 lines of logs: | ||
|
||
```js | ||
// console.log(data, error, isValidating) | ||
undefined undefined false // => hydration / initial render | ||
undefined undefined true // => start fetching | ||
undefined Error false // => end fetching, got an error | ||
undefined Error true // => start retrying | ||
Data undefined false // => end retrying, get the data | ||
``` | ||
|
||
The state changes make sense. But that also means our component **rendered 5 times**. | ||
|
||
If we change our component to only use `data`: | ||
|
||
```jsx | ||
function App () { | ||
const { data } = useSWR('/api', fetcher) | ||
console.log(data) | ||
return null | ||
} | ||
``` | ||
|
||
The magic happens — there are only **2 re-renders** now: | ||
|
||
```js | ||
// console.log(data) | ||
undefined // => hydration / initial render | ||
Data // => end retrying, get the data | ||
``` | ||
|
||
The exact same process has happened internally, there was an error from the first request, then we got the data from the retry. | ||
However, **SWR only updates the states that are used by the component**, which is only `data` now. | ||
|
||
If you are not always using all these 3 states, you are already benefitting from this feature. | ||
At [Vercel](https://vercel.com), this optimization results in ~60% fewer re-renders. | ||
|
||
## Tree Shaking | ||
|
||
The SWR package is [tree-shakeable](https://webpack.js.org/guides/tree-shaking) and side-effect free. | ||
That means if you are only importing the core `useSWR` API, unused APIs like `useSWRInfinite` won't be bundled in your application. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,4 @@ | ||
import Callout from 'components/callout' | ||
import Callout from 'nextra-theme-docs/callout' | ||
|
||
# API Options | ||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,4 @@ | ||
import Callout from 'components/callout' | ||
import Callout from 'nextra-theme-docs/callout' | ||
|
||
# Pagination | ||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,23 +1,29 @@ | ||
# Prefetching Data | ||
|
||
## Top-Level Page Data | ||
|
||
There’re many ways to prefetch the data for SWR. For top level requests, [`rel="preload"`](https://developer.mozilla.org/en-US/docs/Web/HTML/Preloading_content) is highly recommended: | ||
|
||
```html | ||
<link rel="preload" href="/api/data" as="fetch" crossorigin="anonymous"> | ||
``` | ||
|
||
This will prefetch the data before the JavaScript starts downloading. And your incoming fetch requests will reuse the result (including SWR, of course). | ||
Just put it inside your HTML `<head>`. It’s easy, fast and native. | ||
|
||
It will prefetch the data when the HTML loads, even before JavaScript starts to download. All your incoming fetch requests with the same URL will reuse the result (including SWR, of course). | ||
|
||
## Programmatically Prefetch | ||
|
||
Another choice is to prefetch the data conditionally. You can have a function to refetch and set the cache via [mutate](/docs/mutation): | ||
Sometimes, you want to preload a resource conditionally. For example, preloading the data when the user is [hovering](https://github.com/GoogleChromeLabs/quicklink) [a](https://github.com/guess-js/guess) [link](https://instant.page). The most inituitive way is to have a function to refetch and set the cache via the global [mutate](/docs/mutation): | ||
|
||
```js | ||
import { mutate } from 'swr' | ||
|
||
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 | ||
} | ||
``` | ||
|
||
Then use it when you need to preload the **resources** (for example when [hovering](https://github.com/GoogleChromeLabs/quicklink) [a](https://github.com/guess-js/guess) [link](https://instant.page)). | ||
|
||
Together with techniques like [page prefetching](https://nextjs.org/docs#prefetching-pages) in Next.js, you will be able to load both next page and data instantly. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,4 @@ | ||
import Callout from 'components/callout' | ||
import Callout from 'nextra-theme-docs/callout' | ||
|
||
# Suspense | ||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,4 @@ | ||
import Callout from 'components/callout' | ||
import Callout from 'nextra-theme-docs/callout' | ||
|
||
# Usage with Next.js | ||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,3 @@ | ||
module.exports = { | ||
plugins: ['tailwindcss', 'postcss-preset-env'] | ||
plugins: ['tailwindcss', 'postcss-preset-env', 'cssnano'] | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.