Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: Log an error in development if URI size of 2000 characters is exceeded. #747

Merged
merged 13 commits into from
Nov 12, 2024

Conversation

niklasbec
Copy link
Contributor

@niklasbec niklasbec commented Nov 6, 2024

As discussed here.

Error logging for when a user stores more than 2000 characters of state in a URI.

nuqs-e2e-test-bench.mp4

Copy link

vercel bot commented Nov 6, 2024

Someone is attempting to deploy a commit to the 47ng Team on Vercel.

A member of the Team first needs to authorize it.

@niklasbec
Copy link
Contributor Author

@franky47 wasn't to sure about the placement of the log, let me know.

Copy link
Member

@franky47 franky47 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks!

One global thing to change in addition to individual suggestions is the term URL rather than URI, to be consistent with the rest of the codebase (URLSearchParams, window.location etc).

errors/NUQS-414.md Outdated Show resolved Hide resolved
errors/NUQS-414.md Outdated Show resolved Hide resolved
packages/nuqs/src/errors.ts Outdated Show resolved Hide resolved
errors/NUQS-414.md Outdated Show resolved Hide resolved
packages/nuqs/src/update-queue.ts Outdated Show resolved Hide resolved
@@ -153,6 +153,9 @@ function flushUpdateQueue(
scroll: options.scroll,
shallow: options.shallow
})
if (URIIsTooLong() && process.env.NODE_ENV === 'development') {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

question: Does process.env.NODE_ENV work everywhere? Eg: in Vite, other bundlers, or in non-Node.js runtimes (Deno / Bun).

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I know that Bun is designed with Node.js in mind so they expose the node env over process.env just like node does. AFAIK deno doesn't support this.

It works for vite and most other bundlers use plugins to expose process.env aswell.

Copy link
Contributor Author

@niklasbec niklasbec Nov 8, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We can remove the second check but then you would have those warnings in prod builds.

Comment on lines 45 to 47
export function URIIsTooLong() {
return window.location.href.length > URI_MAX_LENGTH
}
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Reading from window.location is not guaranteed to have the correct search params values, the updateUrl function called beforehand may call async APIs where the URL will be updated later.

suggestion: Optimistically apply the search params to the current URL and check on that (this function should probably live in url-encoding.ts):

Suggested change
export function URIIsTooLong() {
return window.location.href.length > URI_MAX_LENGTH
}
export function URIIsTooLong(search: URLSearchParams) {
const url = new URL(window.location)
url.search = renderQueryString(search)
return url.href.length > URI_MAX_LENGTH
}

Copy link

vercel bot commented Nov 8, 2024

The latest updates on your projects. Learn more about Vercel for Git ↗︎

Name Status Preview Comments Updated (UTC)
nuqs ✅ Ready (Inspect) Visit Preview 💬 Add feedback Nov 12, 2024 1:46pm

@niklasbec
Copy link
Contributor Author

@franky47 I refactored according to your remarks, please have a look if this works for you.

@franky47 franky47 added this to the 🪵 Backlog milestone Nov 11, 2024
@franky47 franky47 changed the title Log an error in development if URI size of 2000 characters is exceeded. fix: Log an error in development if URI size of 2000 characters is exceeded. Nov 12, 2024
@franky47 franky47 enabled auto-merge (squash) November 12, 2024 13:41
@franky47 franky47 enabled auto-merge (squash) November 12, 2024 13:41
@franky47 franky47 merged commit 3017660 into 47ng:next Nov 12, 2024
23 checks passed
@franky47 franky47 removed this from the 🪵 Backlog milestone Nov 12, 2024
@franky47 franky47 added this to the 🚀 Shipping next milestone Nov 12, 2024
Copy link

🎉 This PR is included in version 2.1.2 🎉

The release is available on:

Your semantic-release bot 📦🚀

@franky47 franky47 removed this from the 🚀 Shipping next milestone Nov 12, 2024
@franky47 franky47 mentioned this pull request Nov 12, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants