Skip to content

Commit

Permalink
Skip revalidate behavior on the server side
Browse files Browse the repository at this point in the history
  • Loading branch information
oliverlin committed Feb 2, 2021
1 parent 91da848 commit bb99118
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 2 deletions.
19 changes: 19 additions & 0 deletions spec/index.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,12 @@ import createApiMiddleware, {
CHAIN_API
} from '../src'

import * as utils from '../src/utils'

jest.mock('../src/log', () => ({
error: jest.fn()
}))

jest.mock('../src/log', () => ({
error: jest.fn()
}))
Expand All @@ -24,6 +30,7 @@ describe('Middleware::Api', () => {
dispatch = jest.fn()
getState = jest.fn()
next = jest.fn()
utils.window = {}
})

describe('when called with [CHAIN_API]', () => {
Expand Down Expand Up @@ -200,6 +207,18 @@ describe('Middleware::Api', () => {
expect(nockScope.isDone()).toBe(false)
})

it('sends request only for the first call when revalidate is "never"', async () => {
utils.window = null
revalidate = 'never'
await apiMiddleware({ dispatch, getState })(next)(action)
expect(nockScope.isDone()).toBe(true)

resetNockScope()
MockDate.set(currentime + (6 * 1000))
await apiMiddleware({ dispatch, getState })(next)(action)
expect(nockScope.isDone()).toBe(true)
})

it('sends request only after revalidate time when revalidate is defined', async () => {
revalidate = 5
await apiMiddleware({ dispatch, getState })(next)(action)
Expand Down
4 changes: 2 additions & 2 deletions src/index.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import Promise from 'es6-promise'
import createRequestPromise from './createRequestPromise'
import { paramsExtractor } from './utils'
import { paramsExtractor, window } from './utils'

export const CALL_API = Symbol('CALL_API')
export const CHAIN_API = Symbol('CHAIN_API')
Expand Down Expand Up @@ -41,7 +41,7 @@ export default ({
const promiseCreators = action[CHAIN_API].map((createCallApiAction) => {
const apiAction = createCallApiAction()[CALL_API]
const now = Math.floor(new Date().getTime() / 1000)
if (!!apiAction.revalidate) {
if (!!apiAction.revalidate && !!window) {
const revalidationKey = _getRevalidationKey(apiAction)
const lastRevalidateTime = lastRevalidateTimeMap[revalidationKey] || 0
if(apiAction.revalidate === 'never' && !!lastRevalidateTime){
Expand Down
5 changes: 5 additions & 0 deletions src/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -64,3 +64,8 @@ export function paramsExtractor ({ baseUrl }) {
}
}
}

const _window = typeof window === 'undefined' ? null : window

export { _window as window }

0 comments on commit bb99118

Please sign in to comment.