Skip to content

Commit

Permalink
fix: fix appendForceRefresh not working with local urls
Browse files Browse the repository at this point in the history
  • Loading branch information
dziraf committed Dec 4, 2020
1 parent dc47c69 commit dafddc0
Show file tree
Hide file tree
Showing 2 changed files with 65 additions and 3 deletions.
53 changes: 53 additions & 0 deletions src/frontend/components/actions/utils/append-force-refresh.spec.ts
@@ -0,0 +1,53 @@
import { expect } from 'chai'

import { appendForceRefresh } from './append-force-refresh'

describe('appendForceRefresh', () => {
it('should add ?refresh=true to url if url has no search params', () => {
const oldUrl = '/resources/Test'

const newUrl = appendForceRefresh(oldUrl)

expect(newUrl).to.equal('/resources/Test?refresh=true')
})

it('should add &refresh=true to url if url already has search params', () => {
const oldUrl = '/resources/Test?param=test'

const newUrl = appendForceRefresh(oldUrl)

expect(newUrl).to.equal('/resources/Test?param=test&refresh=true')
})

it('should add &refresh=true to url if url already has search params but custom search is passed', () => {
const oldUrl = '/resources/Test?param=test'

const newUrl = appendForceRefresh(oldUrl, 'other_param=test2')

expect(newUrl).to.equal('/resources/Test?other_param=test2&refresh=true')
})

it('should add ?refresh=true to url if url is a full url with no search params', () => {
const oldUrl = 'http://example.com/resources/Test'

const newUrl = appendForceRefresh(oldUrl)

expect(newUrl).to.equal('http://example.com/resources/Test?refresh=true')
})

it('should add &refresh=true to url if url is a full url with search params', () => {
const oldUrl = 'http://example.com/resources/Test?param=test'

const newUrl = appendForceRefresh(oldUrl)

expect(newUrl).to.equal('http://example.com/resources/Test?param=test&refresh=true')
})

it('should add &refresh=true to url if url is a full url with search params but custom search is passed', () => {
const oldUrl = 'http://example.com/resources/Test?param=test'

const newUrl = appendForceRefresh(oldUrl, 'other_param=test2')

expect(newUrl).to.equal('http://example.com/resources/Test?other_param=test2&refresh=true')
})
})
15 changes: 12 additions & 3 deletions src/frontend/components/actions/utils/append-force-refresh.ts
Expand Up @@ -9,12 +9,21 @@ export const REFRESH_KEY = 'refresh'
* @private
*/
export const appendForceRefresh = (url: string, search?: string): string => {
const urlObject = new URL(url)
const oldParams = search ?? urlObject.search ?? window.location.search
const searchParamsIdx = url.lastIndexOf('?')
const urlSearchParams = searchParamsIdx !== -1
? url.substring(searchParamsIdx + 1)
: null

const oldParams = search ?? urlSearchParams ?? window.location.search
const newParams = new URLSearchParams(oldParams)

newParams.set(REFRESH_KEY, 'true')
return `${urlObject.origin}${urlObject.pathname}?${newParams.toString()}`

const newUrl = searchParamsIdx !== -1
? url.substring(0, searchParamsIdx)
: url

return `${newUrl}?${newParams.toString()}`
}

export const hasForceRefresh = (search: string): boolean => {
Expand Down

0 comments on commit dafddc0

Please sign in to comment.