Skip to content

Commit

Permalink
refactor: inject the devtools middleware as a built-in middleware
Browse files Browse the repository at this point in the history
  • Loading branch information
koba04 committed Jun 26, 2022
1 parent f05d933 commit 74a03ef
Show file tree
Hide file tree
Showing 4 changed files with 11 additions and 9 deletions.
7 changes: 3 additions & 4 deletions _internal/utils/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,7 @@ import type {
} from '../types'
import { stableHash } from './hash'
import { initCache } from './cache'
import { preset as webPreset } from './web-preset'
import { preset as devtoolsPreset } from './devtools'
import { preset } from './web-preset'
import { slowConnection } from './env'
import { isUndefined, noop, mergeObjects } from './helper'

Expand Down Expand Up @@ -74,6 +73,6 @@ export const defaultConfig: FullConfiguration = mergeObjects(
mutate,
fallback: {}
},
// use web and devtools preset by default
mergeObjects(webPreset, devtoolsPreset)
// use web preset by default
preset
)
8 changes: 5 additions & 3 deletions _internal/utils/devtools.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
import React from 'react'
import { isWindowDefined } from './helper'

export const preset = {
export const middleware =
// @ts-expect-error
use: isWindowDefined ? window.__SWR_DEVTOOLS_USE__ : []
}
isWindowDefined && window.__SWR_DEVTOOLS_USE__
? // @ts-expect-error
window.__SWR_DEVTOOLS_USE__
: []

export const setupDevTools = () => {
if (isWindowDefined) {
Expand Down
3 changes: 2 additions & 1 deletion _internal/utils/middleware-preset.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { middleware as devtoolsMiddleware } from './devtools'
import { middleware as preload } from './preload'

export const BUILT_IN_MIDDLEWARE = [preload]
export const BUILT_IN_MIDDLEWARE = devtoolsMiddleware.concat(preload)
2 changes: 1 addition & 1 deletion test/use-swr-devtools.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ describe('useSWR - devtools', () => {
;({ createKey, createResponse, renderWithConfig } = require('./utils'))
useSWR = require('swr').default
})
it('window.__SWR_DEVTOOLS_USE__ should be set as middlewares', async () => {
it('window.__SWR_DEVTOOLS_USE__ should be set as middleware', async () => {
const key = createKey()
function Page() {
const { data } = useSWR(key, () => createResponse('ok'))
Expand Down

0 comments on commit 74a03ef

Please sign in to comment.