Skip to content

chore(clerk-js): Replace qs library with custom implementation#3430

Merged
nikosdouvlis merged 13 commits intomainfrom
emmanouela/sdk-873-attempt-to-replace-qs-with-query-string
Jun 12, 2024
Merged

chore(clerk-js): Replace qs library with custom implementation#3430
nikosdouvlis merged 13 commits intomainfrom
emmanouela/sdk-873-attempt-to-replace-qs-with-query-string

Conversation

@EmmanouelaPothitou
Copy link
Copy Markdown
Contributor

@EmmanouelaPothitou EmmanouelaPothitou commented May 23, 2024

Description

Removing the qs library and using the native URLSearchParams API instead reduced the bundle size of clerk.browser.js by 13.5% (from 70.14KB to 60.62KB) and clerk.headless.js by 22% (from 46.5KB to 36.5KB).

Checklist

  • npm test runs as expected.
  • npm run build runs as expected.
  • (If applicable) JSDoc comments have been added or updated for any package exports
  • (If applicable) Documentation has been updated

Type of change

  • 🐛 Bug fix
  • 🌟 New feature
  • 🔨 Breaking change
  • 📖 Refactoring / dependency upgrade / documentation
  • other:

@changeset-bot
Copy link
Copy Markdown

changeset-bot Bot commented May 23, 2024

🦋 Changeset detected

Latest commit: 371c22d

The changes in this PR will be included in the next version bump.

This PR includes changesets to release 3 packages
Name Type
@clerk/clerk-js Patch
@clerk/chrome-extension Patch
@clerk/clerk-expo Patch

Not sure what this means? Click here to learn what changesets are.

Click here if you're a maintainer who wants to add another changeset to this PR

Comment thread packages/clerk-js/package.json
Comment thread packages/clerk-js/src/core/fapiClient.ts Outdated
Comment thread packages/clerk-js/src/core/fapiClient.ts Outdated
Comment thread packages/clerk-js/src/core/fapiClient.ts Outdated
Comment thread packages/clerk-js/src/ui/router/index.tsx Outdated
Comment thread packages/clerk-js/src/utils/querystring.ts Outdated
Comment thread packages/clerk-js/src/utils/querystring.ts
@nikosdouvlis
Copy link
Copy Markdown
Member

Good job so far. Please go through the PR comments and once you're ready, don't forget to:

  1. Update the PRs description to explain why we're doing this change. Also, can you calculate the change in bundle size?
  2. Add a few unit tests that cover fapiClient and the encoders used

@EmmanouelaPothitou EmmanouelaPothitou marked this pull request as ready for review June 5, 2024 10:01
Comment thread packages/clerk-js/src/core/fapiClient.ts Outdated
Comment thread packages/clerk-js/src/utils/__tests__/querystring.test.ts Outdated
Comment thread packages/clerk-js/src/utils/__tests__/querystring.test.ts
Comment thread packages/clerk-js/src/utils/querystring.ts Outdated
@nikosdouvlis nikosdouvlis force-pushed the emmanouela/sdk-873-attempt-to-replace-qs-with-query-string branch from e8c83ab to 4878289 Compare June 5, 2024 18:04
@nikosdouvlis nikosdouvlis deleted the emmanouela/sdk-873-attempt-to-replace-qs-with-query-string branch June 5, 2024 18:07
@nikosdouvlis nikosdouvlis reopened this Jun 5, 2024
@nikosdouvlis
Copy link
Copy Markdown
Member

@EmmanouelaPothitou This looks good to me. Can you include the size reduction in the description?

@nikosdouvlis nikosdouvlis force-pushed the emmanouela/sdk-873-attempt-to-replace-qs-with-query-string branch from f95319b to 773d959 Compare June 5, 2024 18:50
@nikosdouvlis nikosdouvlis changed the title chore(clerk-js): Remove qs library chore(clerk-js): Replace qs library with custom implementation Jun 6, 2024
@nikosdouvlis nikosdouvlis requested review from anagstef and dimkl June 6, 2024 10:00
@nikosdouvlis nikosdouvlis force-pushed the emmanouela/sdk-873-attempt-to-replace-qs-with-query-string branch from f43bc02 to cb77adf Compare June 6, 2024 10:34
Copy link
Copy Markdown
Contributor

@dimkl dimkl left a comment

Choose a reason for hiding this comment

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

🤔 In case the stringifyQueryParams is being used in the request url, i think we should also urlEncode the values to avoid cases of strings containing invalid url symbols (eg =).
Could we test how the qs behaves in those cases?

Comment thread packages/clerk-js/src/utils/querystring.ts Outdated
Comment thread packages/clerk-js/src/utils/querystring.ts Outdated
@EmmanouelaPothitou EmmanouelaPothitou force-pushed the emmanouela/sdk-873-attempt-to-replace-qs-with-query-string branch from 2b83e76 to 34313aa Compare June 10, 2024 11:44
@EmmanouelaPothitou
Copy link
Copy Markdown
Contributor Author

EmmanouelaPothitou commented Jun 10, 2024

🤔 In case the stringifyQueryParams is being used in the request url, i think we should also urlEncode the values to avoid cases of strings containing invalid url symbols (eg =). Could we test how the qs behaves in those cases?

Using the append method on a URLSearchParams instance automatically encodes the value, so I think we don't need to use urlEncode.

https://url.spec.whatwg.org/#example-constructing-urlsearchparams

@EmmanouelaPothitou EmmanouelaPothitou force-pushed the emmanouela/sdk-873-attempt-to-replace-qs-with-query-string branch from 34313aa to 11a5fcc Compare June 10, 2024 12:50
@clerk clerk deleted a comment from EmmanouelaPothitou Jun 10, 2024
@clerk clerk deleted a comment from EmmanouelaPothitou Jun 11, 2024
@EmmanouelaPothitou
Copy link
Copy Markdown
Contributor Author

!snapshot

@clerk-cookie
Copy link
Copy Markdown
Collaborator

Hey @EmmanouelaPothitou - the snapshot version command generated the following package versions:

Package Version
@clerk/chrome-extension 1.0.18-snapshot.v11a5fcc
@clerk/clerk-js 5.6.1-snapshot.v11a5fcc
@clerk/clerk-expo 1.2.1-snapshot.v11a5fcc
gatsby-plugin-clerk 5.0.0-beta.45

Tip: Use the snippet copy button below to quickly install the required packages.
@clerk/chrome-extension

npm i @clerk/chrome-extension@1.0.18-snapshot.v11a5fcc --save-exact

@clerk/clerk-js

npm i @clerk/clerk-js@5.6.1-snapshot.v11a5fcc --save-exact

@clerk/clerk-expo

npm i @clerk/clerk-expo@1.2.1-snapshot.v11a5fcc --save-exact

gatsby-plugin-clerk

npm i gatsby-plugin-clerk@5.0.0-beta.45 --save-exact

@nikosdouvlis
Copy link
Copy Markdown
Member

Will be released once #3518 is out

@nikosdouvlis
Copy link
Copy Markdown
Member

Great job @EmmanouelaPothitou!

@nikosdouvlis nikosdouvlis merged commit 352afca into main Jun 12, 2024
// The return value isn't as expected.
// The buildUrl function converts an undefined value to the string 'undefined'
// and includes it in the search parameters.
it.skip('parses search params when value is undefined', () => {
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Can we unskip this?

Copy link
Copy Markdown
Contributor Author

@EmmanouelaPothitou EmmanouelaPothitou Jun 12, 2024

Choose a reason for hiding this comment

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

The buildUrl function converts an undefined value to the string 'undefined' and includes it in the search parameters. This issue existed before removing the qs library, so I didn't address it in this PR.
The qs library used to exclude keys with undefined values from the search parameter string. The stringifyQueryParams function should behave like qs.

I will now create a task in Linear to investigate it further. Therefore, I think we should skip this test for now.

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.

5 participants