Skip to content

Commit f5cec5c

Browse files
committed
ref: Move warning to url-encoding, add tests
1 parent 190a34f commit f5cec5c

File tree

3 files changed

+31
-20
lines changed

3 files changed

+31
-20
lines changed

packages/nuqs/src/url-encoding.test.ts

+10-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { describe, expect, test } from 'vitest'
1+
import { describe, expect, test, vi } from 'vitest'
22
import { encodeQueryValue, renderQueryString } from './url-encoding'
33

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

129138
test.skip('encodeURI vs encodeURIComponent vs custom encoding', () => {

packages/nuqs/src/url-encoding.ts

+21-4
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { warnIfURLIsTooLong } from './utils'
1+
import { error } from './errors'
22

33
export function renderQueryString(search: URLSearchParams) {
44
if (search.size === 0) {
@@ -16,9 +16,9 @@ export function renderQueryString(search: URLSearchParams) {
1616
.replace(/\?/g, '%3F')
1717
query.push(`${safeKey}=${encodeQueryValue(value)}`)
1818
}
19-
const joinedQuery = query.join('&')
20-
warnIfURLIsTooLong(joinedQuery)
21-
return '?' + joinedQuery
19+
const queryString = '?' + query.join('&')
20+
warnIfURLIsTooLong(queryString)
21+
return queryString
2222
}
2323

2424
export function encodeQueryValue(input: string) {
@@ -44,3 +44,20 @@ export function encodeQueryValue(input: string) {
4444
.replace(/>/g, '%3E')
4545
)
4646
}
47+
48+
// Note: change error documentation (NUQS-414) when changing this value.
49+
export const URL_MAX_LENGTH = 2000
50+
51+
export function warnIfURLIsTooLong(queryString: string) {
52+
if (process.env.NODE_ENV === 'production') {
53+
return
54+
}
55+
if (typeof location === 'undefined') {
56+
return
57+
}
58+
const url = new URL(location.href)
59+
url.search = queryString
60+
if (url.href.length > URL_MAX_LENGTH) {
61+
console.warn(error(414))
62+
}
63+
}

packages/nuqs/src/utils.ts

-15
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,6 @@
11
import { warn } from './debug'
2-
import { error } from './errors'
32
import type { Parser } from './parsers'
43

5-
// Change error documentation after changing this value.
6-
export const URL_MAX_LENGTH = 2000
7-
84
export function safeParse<T>(
95
parser: Parser<T>['parse'],
106
value: string,
@@ -42,14 +38,3 @@ export function getDefaultThrottle() {
4238
return 320
4339
}
4440
}
45-
46-
export function warnIfURLIsTooLong(queryString: string) {
47-
if (process.env.NODE_ENV != 'development') {
48-
return
49-
}
50-
const url = new URL(window.location.href)
51-
url.search = queryString
52-
if (url.href.length > URL_MAX_LENGTH) {
53-
console.warn(error(414))
54-
}
55-
}

0 commit comments

Comments
 (0)