Skip to content

Implement conditional asset loading for improved performance#124

Open
davidperezgar wants to merge 1 commit intotrunkfrom
claude/resolve-issue-123-mMni7
Open

Implement conditional asset loading for improved performance#124
davidperezgar wants to merge 1 commit intotrunkfrom
claude/resolve-issue-123-mMni7

Conversation

@davidperezgar
Copy link
Contributor

Summary

This PR refactors the plugin's asset loading strategy from unconditional enqueueing to conditional loading. Assets are now registered on the init hook and only enqueued when their corresponding blocks are actually rendered on the page, reducing unnecessary script and style loading.

Key Changes

  • Plugin_Main.php: Changed from unconditional script enqueueing to conditional loading via render_block filter for accordion scripts
  • Carousel.php: Added register_frontend_assets() method and conditional enqueueing in block render filters; assets now load only when carousel blocks are detected
  • Animations.php: Converted enqueue_scripts() to register_scripts() using wp_register_* functions; added conditional enqueueing in add_animation_classes_to_blocks() filter
  • Headline.php: Replaced wp_enqueue_scripts hook with render_block filters for headline/text blocks; marquee script now only loads when marquee feature is active
  • ShapeAnimations.php: Changed to registration pattern with conditional enqueueing in add_animation_classes_to_shape() filter
  • ContainerEdgeAlignment.php: Converted to registration pattern with conditional enqueueing in add_edge_alignment_classes() filter
  • StickyColumn.php: Changed to registration pattern with conditional enqueueing in add_sticky_attributes_to_grid_block() filter
  • GravityFormsInline.php: Converted to registration pattern with conditional enqueueing when inline feature is active
  • InsertPost.php: Changed to registration pattern with conditional enqueueing in render_insert_post_block() callback

Implementation Details

  • All frontend assets are now registered during the init hook using wp_register_style() and wp_register_script()
  • Assets are conditionally enqueued using render_block filters or block render callbacks when their corresponding blocks are detected
  • Prevents duplicate enqueueing by checking wp_style_is() and wp_script_is() before enqueueing
  • Maintains backward compatibility while reducing page load impact for pages that don't use specific features

https://claude.ai/code/session_01HViCm5WRkqZFKcKRrkJRZN

Resolves #123 by refactoring all frontend script enqueueing to follow
WordPress best practices: scripts are now registered during init and
only enqueued when their corresponding block is actually rendered on
the page.

Changes per module:
- Plugin_Main: register carousel/accordion scripts; enqueue accordion
  only when an accordion block is rendered via render_block filter.
- Carousel: register frontend assets; enqueue glide.min.js and carousel
  JS/CSS only when a carousel/slider block attribute is detected.
- Animations: register CSS/JS; enqueue only when a block with frbl
  animation, glass-effect, or hover-scale attributes is rendered.
- StickyColumn: register CSS/JS; enqueue only when frblStickyEnabled
  is true on a generateblocks/grid block.
- ContainerEdgeAlignment: register CSS/JS; enqueue only when
  frblEdgeAlignment attribute is present on a container block.
- ShapeAnimations: register CSS/JS/Lottie; enqueue only when
  frblCustomSvgAnimationEnabled is true on a shape block.
- GravityFormsInline: register CSS/JS; enqueue only when
  frblGfInlineEnabled is true on a gravityforms/form block.
- Headline: remove global wp_enqueue_scripts hook; enqueue styles on
  any generateblocks/text or headline block render, and enqueue
  marquee JS only when frblMarqueeSpeed is set.
- InsertPost: register CSS; enqueue inside render_callback so it only
  loads on pages where the frontblocks/insert-post block is used.

https://claude.ai/code/session_01HViCm5WRkqZFKcKRrkJRZN
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