Skip to content

Conversation

@titoBouzout
Copy link
Contributor

@titoBouzout titoBouzout commented Jul 10, 2025

Fixes a couple of edge cases when using /* @once */:

  1. @once now works in nested objects, <div style={/* @once */ { width: props.width, height: props.height }} /> - Problem was that when the @once expression was found, it was removed, so any nested object became dynamic again.
  2. works with object properties as <div style={/* @once */prop.style}/> - problem was that classList and style were hardcoded to be dynamic, not respecting the @once mark.
  3. works with spreads <div { /* @once */ ... props}/> but only top level - this was just never considered.

A note about spreads, consider the following:

const template28 = (
  <div {/* @once */ ...propsSpread} data-static={/* @once */ color()} data-dynamic={color()} />
);

What should happen is that propsSpread and color() for data-static should be static.

If we look at the generated code, it becomes more evident that the issue is mergeProps rather than spread, as spread has no option but to add the effect for the props that are actually dynamic.

const template28 = (() => {
  var _el$34 = _tmpl$3();
  _$spread(
    _el$34,
    _$mergeProps(propsSpread, {
      get ["data-dynamic"]() {
        return color();
      },
      "data-static": color()
    }),
    false,
    false
  );
  return _el$34;
})();

As a compromise, the spread value is now spreaded (when used with @once), meaning that mergeProps won't track the top level of the object. If access happens in any sub-objects then it will track. This solution is simple enough and possibly covers the immediate expectation.

const template28 = (() => {
  var _el$34 = _tmpl$3();
  _$spread(
    _el$34,
    _$mergeProps({ ...propsSpread }, {
      get ["data-dynamic"]() {
        return color();
      },
      "data-static": color()
    }),
    false,
    false
  );
  return _el$34;
})();

Related:

@titoBouzout titoBouzout changed the title WIP - fix /* @once */ static marker Fix /* @once */ static marker Jul 10, 2025
@titoBouzout titoBouzout marked this pull request as ready for review July 10, 2025 20:18
@titoBouzout
Copy link
Contributor Author

cc @lxsmnsyc @edemaine

@titoBouzout titoBouzout changed the title Fix /* @once */ static marker Fix edge cases for /* @once */ static marker Jul 10, 2025
@titoBouzout titoBouzout marked this pull request as draft July 13, 2025 13:09
@titoBouzout titoBouzout marked this pull request as ready for review July 13, 2025 19:17
@ryansolid ryansolid merged commit e0694f5 into ryansolid:main Aug 6, 2025
2 checks passed
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