Skip to content

3.0.0.beta1

Pre-release
Pre-release

Choose a tag to compare

@joeldrapper joeldrapper released this 25 Nov 18:01
· 9 commits to main since this release
0a5f40b

Highlights

The first beta of Phlex 3. Compared with 2.4.3, the big change is a new compiler. It turns your components into faster Ruby and renders exactly the same output. Rendering without it is faster too, and there's an optional C extension for attributes.

The compiler is on by default

Every component compiles the first time it renders. The compiler rewrites your templates into plain Ruby string appends. A compiled component renders byte for byte what it would have uncompiled. If the compiler can't reproduce some code exactly, it leaves that code uncompiled and the component keeps rendering as normal. (#1001, #1009, #1093)

  • Turn it off for a class and its subclasses with compile false, or back on with compile true. A gem can opt its own components out without the app's choice affecting them.
  • Phlex::Compiler.disable! turns it off everywhere.
  • Phlex::Compiler.precompile(component) compiles a component ahead of time, for example at boot before forking. Phlex::Compiler.forget(component) is for code reloaders.
  • Phlex::Compiler.source(component) shows the Ruby a component compiles to, and explain tells you what didn't compile and why. (#1094)
  • Subclasses loaded later, modules included afterwards and re-registered elements trigger a recompile, so they take effect just as they would uncompiled. Files edited after loading are refused rather than compiled. (#1009, #1070, #1102)
  • Mixin methods aren't compiled yet. That only costs speed. (#1069)

Faster rendering, even without the compiler

  • Fewer allocations when generating attributes, and a larger default attribute cache (1 MiB). (#990, #991, #1005)
  • Faster attribute cache misses, CSV, Kit calls, capture and component allocation. (#1065)
  • Component rendering no longer leaves YJIT on every render. (#1103)
  • Phlex::SGML.new is no longer overridden, so Ruby 4 can inline it. (#1104)
  • FIFOCacheStore hits on plain data no longer deserialise on every read, about 30% faster for cache. (#1107)

Optional C extension

Phlex now includes a C attribute serialiser. It's built when you install the gem on CRuby 4.0 or later. Anywhere it can't be built (JRuby, TruffleRuby, older Rubies, no compiler), installation carries on and Phlex uses Ruby. Set PHLEX_NATIVE=false at install time to skip it. (#1068, #1095)

Ractors

Components render on any Ractor, including compiled components, kits, tag, SVG, fragments, cache and CSV, with the same output as the main Ractor. Compiling, defining components and register_element still happen on the main Ractor. (#1064, #1067)

Also new

  • call(rerenderable: true) lets you render the same component instance more than once. (#1076)
  • parent is public and returns the component that rendered this one, or nil. (#1090)
  • with_content builds a component that keeps its content block, e.g. Card.with_content(title:) { "content" }. (#1104)
  • FIFOCacheStore#fetch accepts cache options such as expires_in:, so it works anywhere Rails.cache does. The options are ignored. (#1040)
  • The Ruby LSP add-on supports ruby-lsp 0.27, reads custom tag: names, and follows Kit calls like Card(title:) for go-to-definition and hover. (#1091)

Breaking changes

  • Ruby 3.3 or later is required. (#1071)
  • new no longer takes a content block. A block passed to new goes to initialize like any Ruby class. Pass content to render or call, or use with_content. Kit calls like Card { } are unchanged. (#1104)
  • Phlex::CSV deprecations are removed. A custom yielder raises; rename it to around_row and change yield to super. A CSV that only defines view_template no longer uses it as the row template. (#1090)
  • A fragment that starts inside an open fragment of the same name raises Phlex::ArgumentError. Sibling fragments can still share a name. (#1082)
  • plain raises when given a block. It used to ignore it. (#1028)
  • Rendering a class that isn't a component raises instead of rendering nothing. (#1039)
  • Custom element names must start with a lowercase letter, as the HTML spec requires. (#1052)
  • An empty attribute name raises. (#1071)
  • FIFOCacheStore stores values with Marshal instead of JSON, and hits on plain data return a frozen copy. Code that mutates a cached value gets a FrozenError. (#1062, #1107)
  • New dependencies: refract ~> 2.0.0, erb >= 5.0.2 (the first with a Ractor-safe ERB::Escape) and rake (to build the C extension).

Fixes

  • With phlex-rails, raw(safe(...)), a block returning safe(...) and output helpers like csrf_meta_tags are no longer lost. A block's return value is no longer added as output after the block wrote something. (#1056)
  • mix merges Symbol tokens, and string keys ending in ! are left as they are. (#1058, #1030)
  • false is skipped in class: and style: collections, so cond && "token" works. (#1059)
  • Date, Time and to_h values work in nested attributes like data:. (#1043)
  • Empty style entries are skipped, and an empty style attribute is left out. Leading empty tokens no longer add a separator, e.g. in srcset. (#1047, #1044)
  • safe(...) attribute values hit the attribute cache. The cache now matches attribute order and safety exactly. (#1060, #1071)
  • Nested attribute hashes can't spell an unsafe attribute name. Character references to invalid code points decode to U+FFFD instead of raising. (#1071)
  • tag finds elements registered on component classes, normalises img, link and input attributes, and closes its tag when content raises. (#1063, #1033, #1032)
  • Fragments are found inside whitespace and comment blocks, keep working after an exception, and are tracked correctly with capture and nested caches. Symbol and String fragment names select the same fragment. (#1061, #1035, #1038, #1079, #1027)
  • Implicit block output survives a flush. (#1036)
  • FIFOCacheStore handles non-UTF-8 content, and FIFO caches no longer keep empty values. (#1062, #1031)
  • Phlex::CSV clears a partial row left by a previous call that raised. (#1041)
  • No more Ruby 4.0 block warnings from before_template and after_template, or verbose warnings when loading elements. (#986, #994)

What's Changed

Plus many more compiler, performance and bug-fix PRs by @joeldrapper, listed in the full changelog.

New Contributors

Full Changelog: 2.4.3...3.0.0.beta1