Skip to content

Preserve Sprockets context dependency metadata - #217

Open
OskarEichler wants to merge 1 commit into
slim-template:masterfrom
OskarEichler:codex/asset-dependency-metadata
Open

Preserve Sprockets context dependency metadata#217
OskarEichler wants to merge 1 commit into
slim-template:masterfrom
OskarEichler:codex/asset-dependency-metadata

Conversation

@OskarEichler

Copy link
Copy Markdown

Summary

Preserve Sprockets context metadata when rendering a Slim asset so depend_on changes invalidate the compiled asset cache. The transformer currently returns only the rendered string and drops dependency metadata accumulated by the context.

The return value now follows Sprockets' processor contract, as its ERB processor does: context.metadata.merge(data: rendered_html).

Reproduction

With Sprockets 4.4.1, put these files on the asset load path:

- depend_on 'payload.txt'
p = File.read('/absolute/path/to/payload.txt')

Compile page.html using this transformer and a Sprockets::Cache::MemoryStore. Change payload.txt from first to second, advancing its mtime, then create a fresh environment sharing that cache and compile again. Before: <p>first</p> remains cached. After: <p>second</p>, changed digest, and payload dependency retained.

Verification

  • Unmodified upstream suite: 14 runs / 54 assertions, zero failures/errors/skips on Ruby 3.2.11 and 4.0.6, Rails 8.1.3.1.
  • 100 focused checks across 20 independent real Sprockets cache cases on both Rubies: initial compile, dependency metadata, unchanged cache, changed output and digest.
  • Standard lint and Ruby syntax pass. Source-only patch; no test files changed.

Compatibility / limitations

No public rendering API break or dependency upgrade intended. The internal Transformer.call return becomes a processor Hash instead of String; direct users of that internal method must account for this. Assets still expose rendered HTML. Sprockets 2/3 paths are unchanged; only Sprockets 4.4.1 was exercised here. The release-based consumer package retains version 4.0.0 and dependency requirements.

context = input[:environment].context_class.new(input)
Slim::Template.new(input[:name]) { input[:data] }.render(context)
data = Slim::Template.new(input[:name]) { input[:data] }.render(context)
context.metadata.merge(data: data)

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nice, thanks, could you confirm that the return value of self.call is not used anywhere (ie. it is discarded after call)?

@OskarEichler

Copy link
Copy Markdown
Author

Confirmed the return value is consumed by Sprockets as the processor result; it is not otherwise called directly in this repository. Returning context.metadata merged with data follows that processor contract and preserves the dependency metadata instead of discarding it.

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