File tree 3 files changed +31
-20
lines changed
3 files changed +31
-20
lines changed Original file line number Diff line number Diff line change 1
- import { describe , expect , test } from 'vitest'
1
+ import { describe , expect , test , vi } from 'vitest'
2
2
import { encodeQueryValue , renderQueryString } from './url-encoding'
3
3
4
4
describe ( 'url-encoding/encodeQueryValue' , ( ) => {
@@ -124,6 +124,15 @@ describe('url-encoding/renderQueryString', () => {
124
124
'?a %26b%3Fc%3Dd%23e%f%2Bg"h\'i`j<k>l(m)n*o,p.q:r;s/t=value'
125
125
)
126
126
} )
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
+ } )
127
136
} )
128
137
129
138
test . skip ( 'encodeURI vs encodeURIComponent vs custom encoding' , ( ) => {
Original file line number Diff line number Diff line change 1
- import { warnIfURLIsTooLong } from './utils '
1
+ import { error } from './errors '
2
2
3
3
export function renderQueryString ( search : URLSearchParams ) {
4
4
if ( search . size === 0 ) {
@@ -16,9 +16,9 @@ export function renderQueryString(search: URLSearchParams) {
16
16
. replace ( / \? / g, '%3F' )
17
17
query . push ( `${ safeKey } =${ encodeQueryValue ( value ) } ` )
18
18
}
19
- const joinedQuery = query . join ( '&' )
20
- warnIfURLIsTooLong ( joinedQuery )
21
- return '?' + joinedQuery
19
+ const queryString = '?' + query . join ( '&' )
20
+ warnIfURLIsTooLong ( queryString )
21
+ return queryString
22
22
}
23
23
24
24
export function encodeQueryValue ( input : string ) {
@@ -44,3 +44,20 @@ export function encodeQueryValue(input: string) {
44
44
. replace ( / > / g, '%3E' )
45
45
)
46
46
}
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
+ }
Original file line number Diff line number Diff line change 1
1
import { warn } from './debug'
2
- import { error } from './errors'
3
2
import type { Parser } from './parsers'
4
3
5
- // Change error documentation after changing this value.
6
- export const URL_MAX_LENGTH = 2000
7
-
8
4
export function safeParse < T > (
9
5
parser : Parser < T > [ 'parse' ] ,
10
6
value : string ,
@@ -42,14 +38,3 @@ export function getDefaultThrottle() {
42
38
return 320
43
39
}
44
40
}
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
- }
You can’t perform that action at this time.
0 commit comments