update deps including useSWR 1.0

This commit is contained in:
spencerwooo 2022-02-02 16:43:17 +08:00
parent 89dcc1f614
commit 9794fb2df4
No known key found for this signature in database
GPG key ID: 24CD550268849CA0
4 changed files with 724 additions and 641 deletions

View file

@ -16,10 +16,11 @@
"@fortawesome/react-fontawesome": "^0.1.14",
"@headlessui/react": "^1.4.0",
"awesome-debounce-promise": "^2.1.0",
"axios": "^0.21.1",
"axios": "^0.25.0",
"cors": "^2.8.5",
"crypto-js": "^4.1.1",
"emoji-regex": "^9.2.2",
"csstype": "^3.0.10",
"emoji-regex": "^10.0.0",
"ioredis": "^4.28.2",
"jszip": "^3.7.1",
"next": "^12.0.10",
@ -32,15 +33,15 @@
"react-dom": "^17.0.2",
"react-hot-toast": "^2.0.0",
"react-hotkeys-hook": "^3.4.4",
"react-markdown": "^6.0.2",
"react-markdown": "^8.0.0",
"react-player": "^2.9.0",
"react-reader": "^0.20.4",
"react-viewer": "^3.2.2",
"rehype-katex": "^5.0.0",
"rehype-katex": "^6.0.2",
"rehype-raw": "^6.0.0",
"remark-gfm": "^1.0.0",
"remark-math": "^4.0.0",
"swr": "^0.5.6",
"remark-gfm": "^3.0.1",
"remark-math": "^5.1.1",
"swr": "^1.2.0",
"use-clipboard-copy": "^0.2.0",
"use-constant": "^1.1.0"
},
@ -49,16 +50,16 @@
"@types/crypto-js": "^4.0.2",
"@types/ioredis": "^4.28.5",
"@types/prismjs": "^1.16.5",
"@types/react": "17.0.11",
"@types/react": "17.0.38",
"@types/react-copy-to-clipboard": "^5.0.0",
"@types/react-dom": "^17.0.8",
"@types/react-pdf": "^5.0.4",
"@types/react-syntax-highlighter": "^13.5.1",
"autoprefixer": "^10.4.0",
"eslint": "7.29.0",
"eslint-config-next": "11.0.0",
"eslint": "8.8.0",
"eslint-config-next": "12.0.10",
"postcss": "^8.4.5",
"tailwindcss": "^3.0.18",
"typescript": "4.3.4"
"typescript": "4.5.5"
}
}

File diff suppressed because it is too large Load diff

View file

@ -17,7 +17,8 @@
"resolveJsonModule": true,
"isolatedModules": true,
"jsx": "preserve",
"noImplicitAny": false
"noImplicitAny": false,
"incremental": true
},
"include": [
"next-env.d.ts",

View file

@ -1,5 +1,7 @@
import axios from 'axios'
import useSWR, { cache, Key, useSWRInfinite } from 'swr'
import useSWRInfinite from 'swr/infinite'
import type { OdAPIResponse } from '../types'
import { getStoredToken } from './protectedRouteHandler'
@ -17,22 +19,6 @@ export async function fetcher(url: string, token?: string): Promise<any> {
throw { status: err.response.status, message: err.response.data }
}
}
/**
* Use stale SWR instead of revalidating on each request. Not ideal for this scenario but have to do
* if fetching serverside props from component instead of pages.
* @param url request url
* @returns useSWR instance
*/
export function useStaleSWR({ url, path = '' }: { url: Key; path?: string }) {
const revalidationOptions = {
revalidateOnMount: !(cache.has(`arg@"${url}"@null`) || cache.has(url)),
revalidateOnFocus: false,
revalidateOnReconnect: true,
}
const hashedToken = getStoredToken(path)
return useSWR([url, hashedToken], fetcher, revalidationOptions)
}
/**
* Paging with useSWRInfinite + protected token support
@ -49,7 +35,7 @@ export function useProtectedSWRInfinite(path: string = '') {
* @param path Directory path
* @returns API to the next page
*/
function getNextKey(pageIndex, previousPageData): (string | null)[] | null {
function getNextKey(pageIndex: number, previousPageData: OdAPIResponse): (string | null)[] | null {
// Reached the end of the collection
if (previousPageData && !previousPageData.folder) return null
@ -60,11 +46,12 @@ export function useProtectedSWRInfinite(path: string = '') {
return [`/api?path=${path}&next=${previousPageData.next}`, hashedToken]
}
// Disable auto-revalidate, these options are equivalent to useSWRImmutable
// https://swr.vercel.app/docs/revalidation#disable-automatic-revalidations
const revalidationOptions = {
revalidateOnMount: !(cache.has(`arg@"/api?path=${path}"@null`) || cache.has(`/api?path=${path}`)),
revalidateIfStale: false,
revalidateOnFocus: false,
revalidateOnReconnect: true,
}
// return useSWRInfinite(getNextKey, fetcher)
return useSWRInfinite(getNextKey, fetcher, revalidationOptions)
}