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: do not serialize methods of objects passed to styles #6932

Conversation

jakovljevic-mladen
Copy link
Contributor

What is it?

  • Feature / enhancement
  • Bug fix
  • Docs / tests / types / typos
  • Infra

Description

It is completely valid to write a code like this:

export const cmp = component$(() => {
  const style = {
    hasOwnProperty: () => false,
    foo: () => {
      return 'bar';
    },
    color: 'red',
  };

  return <a style={style} href="/some-link">Link</a>;
});

This gets serialized to DOM like this:

image

To prove this wrong, I added some tests for stringifyStyle function that converts whatever it gets from user to a usable string value that later gets stored to DOM. Without this fix, tests "should ignore object methods" and "should stringify object with custom hasOwnProperty method" would fail.

Side note: since I wanted to add a lot of other tests for stringifyStyle (and setValueForStyle as well), I noticed that error with code QError_stringifyClassOrStyle does not mention style property, so I updated error message in packages/qwik/src/core/error/error.ts. Does that make any problem for public facing APIs? If not, then I'd like it to stay because it mentions style as well.

Checklist:

  • My code follows the developer guidelines of this project
  • I have performed a self-review of my own code
  • I have ran pnpm change and documented my changes
  • I have made corresponding changes to the Qwik docs
  • Added new tests to cover the fix / functionality

@jakovljevic-mladen jakovljevic-mladen requested a review from a team as a code owner October 3, 2024 12:14
Copy link

changeset-bot bot commented Oct 3, 2024

🦋 Changeset detected

Latest commit: e7d826c

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

This PR includes changesets to release 4 packages
Name Type
@builder.io/qwik Patch
eslint-plugin-qwik Patch
@builder.io/qwik-city Patch
create-qwik 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

@jakovljevic-mladen jakovljevic-mladen force-pushed the dont-allow-styles-to-serialize-methods branch from baed452 to e7d826c Compare October 3, 2024 12:15
Copy link

pkg-pr-new bot commented Oct 3, 2024

Open in Stackblitz

npm i https://pkg.pr.new/@builder.io/qwik@6932
npm i https://pkg.pr.new/@builder.io/qwik-city@6932
npm i https://pkg.pr.new/eslint-plugin-qwik@6932
npm i https://pkg.pr.new/create-qwik@6932

commit: e7d826c

Copy link
Contributor

github-actions bot commented Oct 3, 2024

built with Refined Cloudflare Pages Action

⚡ Cloudflare Pages Deployment

Name Status Preview Last Commit
qwik-docs ✅ Ready (View Log) Visit Preview e7d826c

@wmertens
Copy link
Member

wmertens commented Oct 3, 2024

First of all, thank you for your PR!

But, we're about ready to release v2 alpha and we'd need to re-implement this for v2. The code is on the build/v2 branch and serialization all happens in shared-serialization.ts.

Also I am not sure that approach will work there, it will probably strip all functions from objects instead of just for style objects. Normally, qwik should refuse to SSR when it encounters a function but right now we're just ignoring them because we're serializing too much state.

How common is this problem? Is there a workaround?

@jakovljevic-mladen
Copy link
Contributor Author

@wmertens,

How common is this problem? Is there a workaround?

Not sure how common the problem is, but people can certainly encounter it. I don't think this issue is problematic at all, but this serialization doesn't have to happen, that's all.

Also, I tested this against changes on V2 branch, and this issue is present there as well:

image

And I went to the v2 branch and I noticed the same util functions exist there as well, so since they're not removed, they're probably still being used in v2 as well. Anyway, since this issue is present in v1, this patch can still be considered for v1.

@wmertens wmertens merged commit 54d6a24 into QwikDev:main Oct 6, 2024
19 checks passed
@jakovljevic-mladen jakovljevic-mladen deleted the dont-allow-styles-to-serialize-methods branch October 6, 2024 15:39
@github-actions github-actions bot mentioned this pull request Oct 6, 2024
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.

2 participants