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

[🐞] Unable to serialize object for worker which includes underscore-prefixed member names #629

Open
westonruter opened this issue Sep 29, 2024 · 0 comments
Labels
bug Something isn't working

Comments

@westonruter
Copy link
Contributor

Describe the bug

In WordPress/performance#1563 I'm attempting to integrate Partytown with popular WordPress plugins that add Google Analytics to the page. One popular plugin, WooCommerce, needs to access wp.i18n from the main thread specifically to obtain the translation string via wp.i18n.__(). However, this is currently blocked by Partytown in serializeObjectForWorker() because isValidMemberName() forbids serializing any properties beginning with _:

export const isValidMemberName = (memberName: string) =>
  !(
    startsWith(memberName, 'webkit') ||
    startsWith(memberName, 'toJSON') ||
    startsWith(memberName, 'constructor') ||
    startsWith(memberName, 'toString') ||
    startsWith(memberName, '_')
  );

The members of wp.i18n are:

  • __
  • _n
  • _nx
  • _x
  • createI18n
  • defaultI18n
  • getLocaleData
  • hasTranslation
  • isRTL
  • resetLocaleData
  • setLocaleData
  • sprintf
  • subscribe

It seems that serializeObjectForWorker() should be updated to only check isValidMemberName() for specific object types and not all objects.

Reproduction

https://partytown-wp-i18n-failure.glitch.me/

Steps to reproduce

On a page add the following:

<script>
var my_i18n = {
    __: ( str ) => { return str; }
};
</script>

Then in the Partytown config, make this my_i18n object available to workers via the mainWindowAccessors configuration.

Then attempt to call my_i18n.__('...') from a script that is running in a Worker.

Browser Info

Chrome

Additional Information

No response

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

1 participant