Problem/Motivation

The MailchimpSignupSubscribeBlock directly embeds a form using $this->formBuilder->getForm() in its build() method. Forms contain unique security tokens (form_build_id and form_token) that must be generated fresh on every request, which prevents Dynamic Page Cache from caching any page containing this block.

This impacts site performance, as pages with the subscribe block cannot be served from Dynamic Page Cache, requiring full rendering on every page load.

Steps to reproduce

  1. Place a Mailchimp Signup block on a page
  2. Enable Dynamic Page Cache module
  3. Check response headers - you'll see X-Drupal-Dynamic-Cache UNCACHEABLE (poor cacheability) on every request
  4. The page is never cached due to the form tokens

Proposed resolution

Convert the form rendering to use a lazy builder, which allows the block content to be cached while the form (with its unique tokens) is rendered as a placeholder after cache retrieval.

Issue fork mailchimp-3559374

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

loze created an issue. See original summary.

loze’s picture

Issue summary: View changes
loze’s picture

Issue summary: View changes

loze’s picture

Status: Active » Needs review
xenophyle’s picture

Status: Needs review » Needs work

Thanks for the MR! I made some suggestions.

loze’s picture

Status: Needs work » Needs review

@xenophyle, I have made some changes addressing your comments.

The one outstanding issue would be, do we want to leave the static lazy builder callback in the function with \Drupal::service() calls
Or would you rather create a new lazy builder service, where we can use dependency injection, and move the callback to there?

loze’s picture

Perhaps it should be a service, this way we could lazy load both the block and page forms using the same service.

loze’s picture

Ok, so I ended up making the lazy builder service and and using it on both the block and page forms.
This should be ready for review now.

xenophyle’s picture

Status: Needs review » Fixed

Looks like this works now, thanks!

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.

Status: Fixed » Closed (fixed)

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