Skip to content

Request style.css only when a child theme is active - #54

Closed
khan-rafin-ahmed wants to merge 1 commit into
rupok:devfrom
khan-rafin-ahmed:perf/style-css-child-only
Closed

khan-rafin-ahmed wants to merge 1 commit into
rupok:devfrom
khan-rafin-ahmed:perf/style-css-child-only

Conversation

@khan-rafin-ahmed

Copy link
Copy Markdown
Collaborator

Closes #53

Problem

Without a child theme, flexia_styles() enqueued the parent style.css, which holds only the theme header comment and no rules. The result was an empty render-blocking stylesheet request on every front-end page.

Change

flexia-style is still enqueued with the same dependencies and version. Its source is:

  • get_stylesheet_uri() when is_child_theme(), so the child stylesheet loads exactly as before;
  • false otherwise. WordPress then prints any inline CSS attached to the handle, without a <link>.

Tests

tests/wordpress.php gains three assertions:

  • Without a child theme the handle has no source.
  • Inline CSS added with wp_add_inline_style( 'flexia-style', … ) still prints, and no <link> tag is emitted.
  • With the existing simulated child theme active, the handle's source is the child's get_stylesheet_uri().

Run locally:

  • php -l on changed files
  • python3 tools/check-theme.py (832 block declarations, 0 errors)
  • python3 -m unittest discover -s tests -p 'test_*.py' (4 OK)

tests/wordpress.php runs in the disposable-WordPress CI job.

🤖 Generated with Claude Code

The parent style.css only carries the theme header, so enqueueing it added an
empty render-blocking stylesheet request to every page. Keep the flexia-style
handle (so inline styles and dependants keep working) but give it a source only
when a child theme's stylesheet needs loading.

Closes 53

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@khan-rafin-ahmed

Copy link
Copy Markdown
Collaborator Author

Landed on dev as 3b8b6d5 (rebased onto cad2d87). It also fixes a bug in this branch: wp_enqueue_style() skips registration when the source is false, so the handle and any inline CSS on it were dropped. The handle is now registered first. The WordPress runtime test (655 checks) passes with the fix and fails the three style assertions without 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.

1 participant