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

Usage of native node-module Buffer increase bundle size with 19kb #142

Open
gerhardsletten opened this issue Sep 13, 2021 · 4 comments
Open

Comments

@gerhardsletten
Copy link

This simple line in cache.ts will make webpack bundle an extra 19 kb shim for client-side scripts:

export const convertActionToBase64 = (action: Action) => {
  return Buffer.from(JSON.stringify(action)).toString('base64');
};

image

Would it not be better to use a simple string concat like this:

const convertActionToBase64 = (action) => {
  return `${action.method}${action.endpoint}`
}
@marcin-piela
Copy link
Owner

Good idea, however, we need to save more information, something like:

JSON.stringify({...action, body: undefined})

🤔

@gerhardsletten
Copy link
Author

gerhardsletten commented Sep 13, 2021

@marcin-piela To me it looks like the convertActionToBase64 function is only used to create a hash-key for the request, like here where a new item is added to the cache:

const add = (action: Action, value: T) => {
    if (isCacheable(action)) {
      items[convertActionToBase64(action)] = { ...value, timestamp: Date.now() };
    }
  };

But yes, maybe more info should be used in the key, but it could be that it would be better that an action could contain a cache key, like how react-query and SWR do.

The big problem with the current solution is that even when creating your own CacheProvider, direct imports of convertActionToBase64 in other parts in the library will still make the Buffer-shim to be included in bundle.

@FrameMuse
Copy link

It also creates a problem on 5.0.0 version of react-scripts because it relies on react-scripts polyfill of Buffer

@FrameMuse
Copy link

FrameMuse commented Apr 9, 2022

Good idea, however, we need to save more information, something like:

JSON.stringify({...action, body: undefined})

🤔

You're not saying why you need to save more information

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

No branches or pull requests

3 participants