Skip to content

Reverse middleware execution order #1360

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

Merged
merged 1 commit into from
Aug 20, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion src/utils/resolve-args.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ export default function withArgs<SWRType>(hook: any) {
let next = hook
const { middlewares } = config
if (middlewares) {
for (let i = 0; i < middlewares.length; i++) {
for (let i = middlewares.length; i-- > 0; ) {
next = middlewares[i](next)
}
}
Expand Down
61 changes: 57 additions & 4 deletions test/use-swr-middlewares.test.tsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
import { act, render, screen } from '@testing-library/react'
import React, { useState, useEffect, useRef } from 'react'
import useSWR, { Middleware, SWRConfig } from 'swr'
import { withMiddleware } from '../src/utils/with-middleware'

import { createResponse, sleep, createKey } from './utils'

describe('useSWR - middlewares', () => {
Expand Down Expand Up @@ -99,12 +101,63 @@ describe('useSWR - middlewares', () => {
screen.getByText('hello,')
await screen.findByText('hello, data')
expect(mockConsoleLog.mock.calls.map(call => call[0])).toEqual([
0,
1,
2,
1,
0,
2,
1,
2
0
])
})

it('should use the correct middleware order in `withMiddleware`', async () => {
const key = createKey()
const mockConsoleLog = jest.fn((_, s) => s)
const createLoggerMiddleware = (id: number): Middleware => useSWRNext => (
k,
fn,
config
) => {
mockConsoleLog(id + '-enter', k)
const swr = useSWRNext(k, fn, config)
mockConsoleLog(id + '-exit', k)
return swr
}

const customSWRHook = withMiddleware(useSWR, useSWRNext => (...args) => {
mockConsoleLog('0-enter', args[0])
const swr = useSWRNext(...args)
mockConsoleLog('0-exit', args[0])
return swr
})

function Page() {
const { data } = customSWRHook(key, () => createResponse('data'), {
middlewares: [createLoggerMiddleware(1)]
})
return <div>hello, {data}</div>
}

render(
<SWRConfig value={{ middlewares: [createLoggerMiddleware(2)] }}>
<Page />
</SWRConfig>
)
screen.getByText('hello,')
await screen.findByText('hello, data')
expect(mockConsoleLog.mock.calls.map(call => call[0])).toEqual([
'2-enter',
'1-enter',
'0-enter',
'0-exit',
'1-exit',
'2-exit',
'2-enter',
'1-enter',
'0-enter',
'0-exit',
'1-exit',
'2-exit'
])
})

Expand Down Expand Up @@ -166,6 +219,6 @@ describe('useSWR - middlewares', () => {

render(<Page />)
screen.getByText('hello,')
await screen.findByText(`hello, !#${key}#!`)
await screen.findByText(`hello, #!${key}!#`)
})
})