Git Product home page Git Product logo

swr-site's Introduction

The official website for SWR.

The project uses pnpm, Nextra and deploys via Vercel. To develop it locally, clone this repository and run the following command to start the local dev server:

pnpm install
pnpm dev

And visit localhost:3000 to preview your changes.

Contributing

When making a change, or creating a new page, please make sure to edit all language files. You can simply copy the content of the edited English document (or the edited paragraph) and apply it to other language files. And then, volunteers are welcome to help with any untranslated sections.

Contributors

swr-site's People

Contributors

alaazorkane avatar alany411 avatar bertho-zero avatar birdmii avatar cs09g avatar d3press3dd avatar dependabot[bot] avatar eraldoforgoli avatar guilherssousa avatar huozhi avatar huzhengen avatar koba04 avatar leodr avatar litesun avatar macmeharder avatar markozxuu avatar marucircle avatar mattc41190 avatar minsoo-web avatar numb86 avatar nuts3745 avatar oil-oil avatar pacocoursey avatar promer94 avatar shuding avatar uttk-dev avatar valentinpolitov avatar vassbence avatar whinc avatar xnuk avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

swr-site's Issues

In useSWRInfinite i want to get next page url along with data.

let's assume my data is in this format
{
"count": 4,
"next": "http://localhost:8000/api/?page=2&id=102&paginate_by=2",
"previous": null,
"results": [
{
"id": 153,
"name": "pre workshop"
},
{
"id": 154,
"name": "workshop"
}
]
}

now if write code in this way, I don't have the option to store next page URL
const fetcher = url => axios.get(url).then(res => res.data);
const { data, error, mutate, size, setSize, isValidating } = useSWRInfinite(
index =>
https://api.github.com/repos/${repo}/issues?per_page=${PAGE_SIZE}&page=${index + 1},
fetcher
);

I want to store data.results in data array and also store next page URL.
how can I achieve that

Feedback for “Middleware – SWR”

this documentation is really hard to understand

I want to do some simple transform after the fetch...

what do i do?
key, fetcher, config?
how does the data get backed back to the caller?

Also, where does useSWRNext come from?

import useSWR, {useSWRNext} from 'swr'?
it's not shown

Feedback for “Data Fetching – SWR”

Axios and GraphQL should have their standalone pages under this section. For GraphQL, it’ll be great to have examples for common use cases such as query variables, mutations, and probably other advanced features.

page jump error

When i click Introduction - Features, the page jumps to Community.

image

I18: Spanish

Support for the Spanish language, it is the second most spoken language in the world :-) 🙏

Errors in Windows environment

i am using windows

yarn
yarn dev

open http://localhost:3000/docs/options in the chrome

the error is:

./pages/docs/options.mdx:8:0
Module not found: Can't resolve 'E:swr-site	heme.config.js'
   6 | import withLayout from 'nextra-theme-docs'
   7 | import { withSSG } from 'nextra/ssg'
>  8 | import layoutConfig from 'E:\swr-site\theme.config.js'
   9 | import Callout from 'components/callout'
  10 | 
  11 | const makeShortcode = name => function MDXDefaultShortcode(props) {

Feedback for “Suspense – SWR”

From what I understand it's experimental in React 17, but not React 18. It's confusing to me if I can use Suspense with React 18, Next 12, and swr. I tried it out, but was getting errors. It could be because I'm doing data fetching with SSR in addition to the client. I'd love if the docs could be updated with a clear explanation and good example for how to use it.

Scroll to top is inconsistent during navigation

OS: iOS 15
Browser: Google Chrome
Device: iPhone 11

When navigating back and forth between different links on the site, sometimes the page scrolls to top correctly, and sometimes stays at the bottom.

Feedback for “Usage with Next.js – SWR”

Im trying to use "useSWRInfinite" to make a post request that contains in his body the number of a specific page. I cannot change to make a get request, and the docs do not specifie very well how to make other types of requests. Any ideias?

Import statement in docs

Hello, I found a small error in the doc I believe

import { useSWR } from 'swr'
import type { SWRConfiguration } from 'swr'

const config: SWRConfiguration = {
  fallbackData: "fallback",
  revalidateOnMount: false
  // ...
}

const { data } = useSWR<string[]>('/api/data', fetcher, config)

I think the import should be :

import useSWR from 'swr'

Source
(Same in every languages)
🇫🇷👋🏻

Feedback for “Mutation – SWR”

It would be really nice to add an example of what mutation is in SWR, like a general definition of how mutation can be defined in the context of SWR and how useful it is compared to other tools and approaches

The logo is not centered vertically

I understand that this is actually an issue of nextra (in which I'll propose a fix later). It's just easier and more clear to describe the issue based on a real project like this.

So the issue is the logo is not centered vertically if you use an SVG logo. It's just a bit off. I believe this is because the "a" tag is an inline element, so the height of its "div" wrapper is not correct. I took 2 screenshots for more detail. The size of the "a" element is 24px (correct) but the size of the wrapper is 27px (incorrect) due to "a" being an inline element (vertical align and base line etc)

image

image

Site search gives results for different languages

Actual behavior

When searching through the English version of the site, I got results for the Chinese docs.

Screen Shot 2021-01-29 at 9 41 24 PM

And when clicking on those results (e.g. the first one), it really did take me to the Chinese docs (this page)

Expected behavior

Searching through the docs would only return results for the same language.

More examples for Custom Cache

We'd like to see the following examples/use cases of Custom Cache in the docs:

  • Partial mutation (cross-reference on the "Mutation" page)
  • Preloading data in SSG/SSR/CSR, and feeding back to the cache provider (cross-reference on the "Prefetching" page)
  • Clearing the cache (creating a new "Testing" page)
  • Usage for React Native (might worth a dedicated page for RN)

`fetch` calls missing `ok` check, sometimes missing reading body content

I happened to notice two issues with fetch calls in the documentation:

  1. Not checking HTTP success (several sets of pages)

  2. Not reading the response body, but seeming to expect it to have been read (one set of pages)

I can do a PR for this if it would help.

Not checking HTTP success

Unfortunately, fetch doesn't reject its promise on HTTP error, just on network error (a footgun in the API IMHO). So code like this is problematic:

const fetcher = url => fetch(url).then(r => r.json())

If there's an HTTP error (status 500, for instance, or 404 or 401, etc.), that will try to read the response and parse it as JSON, leading to either A) A JSON parsing error, obscuring the real problem; or B) (If the server is unusual and returns a JSON body for the error page) Incorrect (error) data returned as though it were correct data.

The code should be something like:

const fetcher = url => fetch(url).then(r => {
  if (!r.ok) {
    throw new Error(`HTTP error ${r.status}`)
  }
  return r.json()
})

Programmers have a tendency to copy and paste from documentation from justly-respected folks like Vercel, so it would be better to help them not get hit by this footgun.

This is present on:

  • swr-v1.<lang>.mdx
  • change-log.<lang>.mdx
  • data-fetching.<lang>.mdx
  • error-handling.<lang>.mdx
  • getting-started.<lang>.mdx
  • global-configuration.<lang>.md
  • mutation.<lang>.md (see also the issue below)
  • options.<lang>.mdx
  • prefixing.<lang>.mdx

Not reading the response

On the mutation.<lang>.md pages, the main example doesn't read the response, but the code seems to expect that the response has been read:

mutate('/api/todos', async todos => {
  // let's update the todo with ID `1` to be completed,
  // this API returns the updated data
  const updatedTodo = await fetch('/api/todos/1', {
    method: 'PATCH',
    body: JSON.stringify({ completed: true })
  })

  // filter the list, and return it with the updated item
  const filteredTodos = todos.filter(todo => todo.id !== '1')
  return [...filteredTodos, updatedTodo]
})

There, updatedTodo is a Response object, not a "todo" object. It should probably be (fixing both this and the HTTP error part):

mutate('/api/todos', async todos => {
  // let's update the todo with ID `1` to be completed,
  // this API returns the updated data
  const response = await fetch('/api/todos/1', {
    method: 'PATCH',
    body: JSON.stringify({ completed: true })
  })
  if (!response.ok) {
    throw new Error(`HTTP error ${response.status}`);
  }
  const updatedTodo = await response.json();

  // filter the list, and return it with the updated item
  const filteredTodos = todos.filter(todo => todo.id !== '1')
  return [...filteredTodos, updatedTodo]
})

Feedback for “Getting Started – SWR”

This package is f* nice. This solved my inability to use the Firebase Realtime data updates inside an Electron desktop app.

With useSWR() it's easy to register a specific document or collection of the firestore database.

Context: for some reasons the Firestore realtime updates are incompatible with the Electron browser window.

Thank you for your work it really helped me.

usage with next.js section uses wrong code example

In the pre-rendering section of usage with next.js, there's a mistake in the code ig.
You are fetching from the api route inside a getStaticProps method.
We should never fetch from api routes inside getStaticProps or getServerSideProps, instead we can write the API logic inside the method.
The proposed solution would be here to change the code so that it fetches from an external API.
Something like,

const posts = await fetcher('https://jsonplaceholder.typicode.com/posts');

Thanks.

style/build: rename default branch from master to main

Request

As the PR title suggests, renaming the current default branch for swr-site from master to main.

  • Made a suggestion in my recent PR here, #204, however elevating it here as its own request so it can be properly reviewed/considered by the Vercel team. Please and thank you for the consideration.

Motivation

Adopt naming conventions that are more meaningful and inclusive.

Scope

  • Current repository, swr-site, plus
  • Any and all other repositories in the Vercel org scope that currently use master as the default branch name

Resources

Search not working

Type something in the search box, there are autocomplete suggestions. Good.

But when you click one of the suggested terms, it doesn't do anything.

Change search to `cmd + k`

When folks start using nextra with live react code edit/previews in mdx, typing </> will trigger search since search is triggered with just /. To mitigate, you'll have to track focus and disable the listener when they are in edit mode. Kinda annoying.

I found it was much easier to just switch to CMD+K

Mutate docs

Hi, there's a sentence that is challenging to understand. See this screenshot:
image

How can I reconcile a single value with a list of some things? Could you elaborate on what these things are?

Thanks,
-- L

The project brand in the top bar looks cramped

image

The SWR span has a mx-2 applied to it but I can't find this class in any of the stylesheets that the page downloads.

I believe that swr-site is not building its own Tailwind stylesheet but (accidentally?) relies on the one embedded in nextra-theme-docs.

I have compared older previews on Vercel (❤) and it looks like the issue was introduced by #43, where nextra-theme-docs got bumped from version 0.1.11 to version 0.2.0. Somewhere between these two versions, the library stopped delivering the full Tailwind style.css (>=100kb), started purging unused classes and mx-2 got lost in the process.

Search functionality broken

Hey maintainers,
It looks like the search functionality's broken, no matter how much I type and, wait and hit Enter, there's no response.
image

Also, there was an error logged in the console.
image

how to do condtional rendering

function MyProjects () {
  const { data: user, error } = useSWR('/api/user?training_id=router.query')
  const { data: projects,error } = useSWRInfinite(index => '/api/projects?uid=' + user.id&page=index+1)
  if (!projects) return 'loading...'
  return 'You have ' + projects.length + ' projects' + user.name + 'user'
}

first, get the user using useSWR(which depends on training_id) than get his projects using useSWRInfinite. I want to implement this

and also for both, I need error const

@shuding, please help me in this
thanks in advance

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.