From c7b8f10169c69ab914db7e68d35a43552f31dd16 Mon Sep 17 00:00:00 2001 From: Francois Best Date: Mon, 11 Nov 2024 11:07:52 +0100 Subject: [PATCH] ref: Move warning to url-encoding, add tests --- packages/nuqs/src/url-encoding.test.ts | 11 ++++++++++- packages/nuqs/src/url-encoding.ts | 25 +++++++++++++++++++++---- packages/nuqs/src/utils.ts | 15 --------------- 3 files changed, 31 insertions(+), 20 deletions(-) diff --git a/packages/nuqs/src/url-encoding.test.ts b/packages/nuqs/src/url-encoding.test.ts index 762298e2..88115b8c 100644 --- a/packages/nuqs/src/url-encoding.test.ts +++ b/packages/nuqs/src/url-encoding.test.ts @@ -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', () => { @@ -124,6 +124,15 @@ describe('url-encoding/renderQueryString', () => { '?a %26b%3Fc%3Dd%23e%f%2Bg"h\'i`jl(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', () => { diff --git a/packages/nuqs/src/url-encoding.ts b/packages/nuqs/src/url-encoding.ts index 84d23001..b9b8a30b 100644 --- a/packages/nuqs/src/url-encoding.ts +++ b/packages/nuqs/src/url-encoding.ts @@ -1,4 +1,4 @@ -import { warnIfURLIsTooLong } from './utils' +import { error } from './errors' export function renderQueryString(search: URLSearchParams) { if (search.size === 0) { @@ -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) { @@ -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)) + } +} diff --git a/packages/nuqs/src/utils.ts b/packages/nuqs/src/utils.ts index 441aa818..c19797ee 100644 --- a/packages/nuqs/src/utils.ts +++ b/packages/nuqs/src/utils.ts @@ -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( parser: Parser['parse'], value: string, @@ -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)) - } -}