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 ConditionManager to 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.* and prelinker.preconnect.* to include a visibility property 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 Condition plugins before adding the resource to the page head.

Issue fork prelinker-3586269

Command icon 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

frouco created an issue. See original summary.

frouco’s picture

Assigned: frouco » Unassigned
Status: Active » Needs review
StatusFileSize
new77.14 KB

The new feature is ready for community testing.

I have implemented the Condition API for granular visibility and included a hook_update_N in prelinker.install. This update hook automatically migrates existing path-based restrictions to the new configuration format, ensuring a seamless upgrade.

lpeidro’s picture

Status: Needs review » Fixed

It works as expected, I merge the branch.

Now that this issue is closed, review the contribution record.

As a contributor, attribute any organization that helped you, or if you volunteered your own time.

Maintainers, credit people who helped resolve this issue.

  • frouco committed 688a37e6 on 2.0.x
    Issue #3586269: Added unit tests for the visibility conditions feature
    

  • frouco committed 1f7a965e on 2.0.x
    Issue #3586269: Added visibility conditions to preload and preconnect...

Status: Fixed » Closed (fixed)

Automatically closed - issue fixed for 2 weeks with no activity.