Skip to content

[codex] fix foreach memo style cache#6513

Merged
masenf merged 1 commit into
mainfrom
codex/fix-foreach-accordion-styles
May 14, 2026
Merged

[codex] fix foreach memo style cache#6513
masenf merged 1 commit into
mainfrom
codex/fix-foreach-accordion-styles

Conversation

@Alek99
Copy link
Copy Markdown
Member

@Alek99 Alek99 commented May 14, 2026

Summary

  • clear cached render/compiler artifacts when components are shallow-copied for compile-time mutation
  • clear those caches after recursive style application so memoized snapshot bodies render styled output
  • add a regression test for accordion styles inside rx.foreach memo output

Fixes #6512

Testing

  • uv run pytest tests/units/compiler/test_memoize_plugin.py tests/units/components/test_component.py::test_add_style_foreach
  • uv run ruff check packages/reflex-base/src/reflex_base/components/component.py tests/units/compiler/test_memoize_plugin.py
  • uv run pyright packages/reflex-base/src/reflex_base/components/component.py tests/units/compiler/test_memoize_plugin.py
  • verified the issue repro in an actual Reflex app: foreach accordions and standalone accordion all rendered at 400px with matching default accordion styles

@codspeed-hq
Copy link
Copy Markdown

codspeed-hq Bot commented May 14, 2026

Merging this PR will not alter performance

✅ 24 untouched benchmarks
⏩ 2 skipped benchmarks1


Comparing codex/fix-foreach-accordion-styles (f852cb8) with main (d6618b2)

Open in CodSpeed

Footnotes

  1. 2 benchmarks were skipped, so the baseline results were used instead. If they were deleted from the codebase, click here and archive them to remove them from the performance reports.

@Alek99 Alek99 marked this pull request as ready for review May 14, 2026 22:43
@Alek99 Alek99 requested a review from a team as a code owner May 14, 2026 22:43
@greptile-apps
Copy link
Copy Markdown
Contributor

greptile-apps Bot commented May 14, 2026

Greptile Summary

This PR fixes a bug where rx.foreach memo bodies rendered unstyled output because a cached render snapshot from the auto-memo preview persisted through the style application phase. Two targeted changes address the root cause.

  • component.py: Extracts cache-clearing logic into _clear_compile_caches(), calls it from __copy__ (so cloned components drop stale render/vars/hooks caches), and adds a call at the end of _add_style_recursive (so the updated self.style is reflected on the next render rather than serving the pre-style cached result).
  • test_memoize_plugin.py: Adds a regression test that compiles a page containing rx.foreach with accordion components, runs compile_memo_components, and asserts that accordion-specific styles (width, borderRadius, justifyContent) are present in the generated foreach memo file.

Confidence Score: 5/5

This PR is safe to merge; changes are limited to cache-clearing logic in component.py and a new regression test with no risky side-effects.

The fix is minimal and targeted: _clear_compile_caches() only removes opt-in cached attributes that are lazily recomputed on next access, so clearing them can never corrupt state — only trigger a fresh recompute. The recursive style application already correctly propagates through children. The regression test confirms the exact failure mode from the bug report is resolved, and no existing tests are touched.

No files require special attention.

Important Files Changed

Filename Overview
packages/reflex-base/src/reflex_base/components/component.py Refactors cache-clearing into _clear_compile_caches() and calls it in __copy__ and after _add_style_recursive updates self.style; logic is minimal and correct.
tests/units/compiler/test_memoize_plugin.py Adds a targeted regression test that verifies accordion styles propagate into the compiled foreach memo file; assertions are concrete and test the exact failure scenario from the bug report.

Sequence Diagram

sequenceDiagram
    participant Compiler
    participant ForeachMemo
    participant Component
    participant Cache

    Compiler->>ForeachMemo: preview snapshot (auto-memo)
    ForeachMemo->>Component: __copy__()
    Component->>Cache: _clear_compile_caches()
    Note over Cache: _cached_render_result cleared

    Compiler->>Component: _add_style_recursive(style)
    Component->>Component: "self.style = new_style"
    Component->>Cache: _clear_compile_caches()
    Note over Cache: stale pre-style render discarded

    loop children
        Component->>Component: child._add_style_recursive(style)
        Component->>Cache: child._clear_compile_caches()
    end

    Compiler->>ForeachMemo: compile_memo_components()
    ForeachMemo->>Component: render()
    Component->>Cache: recompute with updated style
    Cache-->>ForeachMemo: styled render result ✓
Loading

Reviews (1): Last reviewed commit: "fix foreach memo style cache" | Re-trigger Greptile

@masenf masenf merged commit fe51a33 into main May 14, 2026
70 checks passed
@masenf masenf deleted the codex/fix-foreach-accordion-styles branch May 14, 2026 22:57
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.

Accordian in rx.foreach has broken styling

2 participants