Problem/Motivation
The Prelinker module currently supports resource restriction via page paths. However, real-world performance optimization often requires more granular control. For example:
- Preloading specific fonts only for certain languages (e.g., Greek or Cyrillic character sets).
- Preconnecting to a payment gateway only on specific Content Types (e.g., "Product" or "Webform").
- Restricting resources based on User Roles or Themes.
Currently, the path-based restriction is insufficient for complex or multilingual Drupal sites.
Proposed resolution
Implement the standard Drupal Condition API for both Preload and Preconnect configuration entities. This brings the full flexibility of the Drupal block visibility system to Prelinker.
Key Features:
- Standard Visibility UI: Adds a "Visibility" vertical tabs section to the entity forms, allowing users to configure conditions (Language, Role, Content Type, Path, etc.).
- Condition Evaluation: Uses the
ConditionManagerto evaluate visibility at runtime. Multiple conditions are combined using AND logic. - Readable Summaries: Replaces the generic "Restricted" label in the admin listing with a human-readable summary of active conditions (e.g., Language: el, es).
- Cacheability Metadata: Ensures that visibility conditions correctly bubble up the necessary cache contexts to the page response.
Use case example
A multilingual site uses a standard font but switches to a heavy "Extended" font for Greek (el) and Spanish (es) pages. With this feature, the heavy font preload can be restricted only to those language contexts, preventing unnecessary asset discovery and bandwidth usage for all other visitors.
User interface changes
- Entity Forms: Preload/Preconnect add and edit forms gain a collapsible "Visibility" section identical to the Block configuration UI.
- Admin Listing: The "Visibility" column now displays a summary of the specific conditions applied to an entry.
API changes
- Configuration Schema: Updated
prelinker.preload.*andprelinker.preconnect.*to include avisibilityproperty for storing condition configurations. - Dependency Handling: Entities now track dependencies on the modules providing the conditions (e.g.,
node,user). - Logic: The service responsible for injecting links now evaluates
Conditionplugins before adding the resource to the page head.
| Comment | File | Size | Author |
|---|---|---|---|
| #3 | prelinker-3586269-visibilty-conditions.png | 77.14 KB | frouco |
Issue fork prelinker-3586269
Show commands
Start within a Git clone of the project using the version control instructions.
Or, if you do not have SSH keys set up on git.drupalcode.org:
Comments
Comment #3
frouco commentedThe new feature is ready for community testing.
I have implemented the Condition API for granular visibility and included a
hook_update_Ninprelinker.install. This update hook automatically migrates existing path-based restrictions to the new configuration format, ensuring a seamless upgrade.Comment #4
lpeidro commentedIt works as expected, I merge the branch.