Skip to content

Commit

Permalink
ref: Move warning to url-encoding, add tests
Browse files Browse the repository at this point in the history
  • Loading branch information
franky47 committed Nov 12, 2024
1 parent 190a34f commit f5cec5c
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 20 deletions.
11 changes: 10 additions & 1 deletion packages/nuqs/src/url-encoding.test.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { describe, expect, test } from 'vitest'
import { describe, expect, test, vi } from 'vitest'
import { encodeQueryValue, renderQueryString } from './url-encoding'

describe('url-encoding/encodeQueryValue', () => {
Expand Down Expand Up @@ -124,6 +124,15 @@ describe('url-encoding/renderQueryString', () => {
'?a %26b%3Fc%3Dd%23e%f%2Bg"h\'i`j<k>l(m)n*o,p.q:r;s/t=value'
)
})
test('emits a warning if the URL is too long', () => {
const search = new URLSearchParams()
search.set('a', 'a'.repeat(2000))
const warn = console.warn
console.warn = vi.fn()
renderQueryString(search)
expect(console.warn).toHaveBeenCalledTimes(1)
console.warn = warn
})
})

test.skip('encodeURI vs encodeURIComponent vs custom encoding', () => {
Expand Down
25 changes: 21 additions & 4 deletions packages/nuqs/src/url-encoding.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { warnIfURLIsTooLong } from './utils'
import { error } from './errors'

export function renderQueryString(search: URLSearchParams) {
if (search.size === 0) {
Expand All @@ -16,9 +16,9 @@ export function renderQueryString(search: URLSearchParams) {
.replace(/\?/g, '%3F')
query.push(`${safeKey}=${encodeQueryValue(value)}`)
}
const joinedQuery = query.join('&')
warnIfURLIsTooLong(joinedQuery)
return '?' + joinedQuery
const queryString = '?' + query.join('&')
warnIfURLIsTooLong(queryString)
return queryString
}

export function encodeQueryValue(input: string) {
Expand All @@ -44,3 +44,20 @@ export function encodeQueryValue(input: string) {
.replace(/>/g, '%3E')
)
}

// Note: change error documentation (NUQS-414) when changing this value.
export const URL_MAX_LENGTH = 2000

export function warnIfURLIsTooLong(queryString: string) {
if (process.env.NODE_ENV === 'production') {
return
}
if (typeof location === 'undefined') {
return
}
const url = new URL(location.href)
url.search = queryString
if (url.href.length > URL_MAX_LENGTH) {
console.warn(error(414))
}
}
15 changes: 0 additions & 15 deletions packages/nuqs/src/utils.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,6 @@
import { warn } from './debug'
import { error } from './errors'
import type { Parser } from './parsers'

// Change error documentation after changing this value.
export const URL_MAX_LENGTH = 2000

export function safeParse<T>(
parser: Parser<T>['parse'],
value: string,
Expand Down Expand Up @@ -42,14 +38,3 @@ export function getDefaultThrottle() {
return 320
}
}

export function warnIfURLIsTooLong(queryString: string) {
if (process.env.NODE_ENV != 'development') {
return
}
const url = new URL(window.location.href)
url.search = queryString
if (url.href.length > URL_MAX_LENGTH) {
console.warn(error(414))
}
}

0 comments on commit f5cec5c

Please sign in to comment.