Skip to content

Conversation

@dancastellon
Copy link
Contributor

@dancastellon dancastellon commented Aug 7, 2018

Resolves #4516
Impact: minor
Type: performance

Issue

libphonenumber-js was being included in the client bundle. It's only use is in formatPhoneNumber.

Solution

Turn formatPhoneNumber into an async function that dynamically imports libphonenumber-js. In a separate PR, update the plugin that uses it (SMS in Reaction Contrib repo)

Breaking changes

None

Testing

  1. Add the following code to /client/api/index.js:
    import { formatPhoneNumber } from "/lib/api/helpers";
    async function formatTest() {
    const num = await formatPhoneNumber("5555555555", "US");
    console.log("Formated phone number:", num);
    }
    formatTest();
  2. Start the app and confirm you see in browser console: Formated phone number: 15555555555
  3. Confirm bundle size is improved by running visualizer: export TOOL_NODE_FLAGS='--max-old-space-size=4096' && METEOR_DISABLE_OPTIMISTIC_CACHING=1 meteor run --production --extra-packages bundle-visualizer

Before

libphone-before

After

libphone-after

@brent-hoover
Copy link
Collaborator

I don't think we should remove this. This is functionality we should provide. Would be nice if it could be dynamically imported though

@dancastellon dancastellon changed the title #4516: Remove unused libphonenumber-js NPM module WIP #4516: Remove unused libphonenumber-js NPM module Aug 7, 2018
@dancastellon dancastellon changed the title WIP #4516: Remove unused libphonenumber-js NPM module WIP #4516: Make formatPhoneNumber load libphonenumber-js asynchronously to reduce bundle size Aug 8, 2018
@dancastellon dancastellon changed the title WIP #4516: Make formatPhoneNumber load libphonenumber-js asynchronously to reduce bundle size WIP #4516: Dynamically import libphonenumber-js to reduce bundle size Aug 8, 2018
@dancastellon dancastellon changed the title WIP #4516: Dynamically import libphonenumber-js to reduce bundle size #4516: Dynamically import libphonenumber-js to reduce bundle size Aug 8, 2018
@dancastellon
Copy link
Contributor Author

@zenweasel Thanks, I've updated this to use a dynamic import instead of removing it altogether. @ajporlante @Akarshit - this is ready for review.

Copy link
Collaborator

@brent-hoover brent-hoover left a comment

Choose a reason for hiding this comment

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

I think we might want to update the version but this works and is ready to go

package.json Outdated
"jquery-i18next": "^1.2.1",
"later": "^1.2.0",
"libphonenumber-js": "^1.0.24",
"libphonenumber-js": "1.0.24",
Copy link
Collaborator

Choose a reason for hiding this comment

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

The current version of this is 1.4.2, I wonder if we shouldn't update this now

Copy link
Contributor Author

@dancastellon dancastellon Aug 9, 2018

Choose a reason for hiding this comment

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

@zenweasel Updated libphonenumber-js and re-tested.

@aldeed
Copy link
Contributor

aldeed commented Aug 14, 2018

@dancastellon @zenweasel As far as I can tell, only SMS pkg is using this, and it uses it from server code. Going forward, the preferred way for clients to get a formatted phone number would be for it to be made available through GraphQL (e.g., a displayPhone field) already formatted by the server.

So given these facts, how about we move this function to /imports/plugins/core/core/server/util? That will prevent it from being included in the client bundle, so you could then keep it as a normal non-dynamic import.

If we want to keep SMS pkg working, you could update that one line that imports it in that repo.

@aldeed aldeed self-requested a review August 14, 2018 21:16
Copy link
Contributor

@aldeed aldeed left a comment

Choose a reason for hiding this comment

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

See my comment

@dancastellon dancastellon changed the title #4516: Dynamically import libphonenumber-js to reduce bundle size WIP #4516: Move formatPhoneNumber (and libphonenumber-js) server-side to reduce client bundle Aug 15, 2018
@dancastellon
Copy link
Contributor Author

Thanks @aldeed, PR updated

@dancastellon dancastellon changed the title WIP #4516: Move formatPhoneNumber (and libphonenumber-js) server-side to reduce client bundle #4516: Move formatPhoneNumber (and libphonenumber-js) server-side to reduce client bundle Aug 15, 2018
@dancastellon dancastellon changed the title #4516: Move formatPhoneNumber (and libphonenumber-js) server-side to reduce client bundle WIP #4516: Move formatPhoneNumber (and libphonenumber-js) server-side to reduce client bundle Aug 16, 2018
@aldeed aldeed merged commit 08ea0a4 into release-1.15.0 Aug 16, 2018
@aldeed aldeed deleted the perf-4516-remove-libphonenumber-js branch August 16, 2018 20:42
@dancastellon dancastellon changed the title WIP #4516: Move formatPhoneNumber (and libphonenumber-js) server-side to reduce client bundle #4516: Move formatPhoneNumber (and libphonenumber-js) server-side to reduce client bundle Aug 16, 2018
@spencern spencern mentioned this pull request Aug 22, 2018
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants