Hi there,

i have a custom block extending BlockBase that displays the username added to the navbar with bootstrap theme.

    public function build() {
        $currentUser = \Drupal::currentUser();
        return array(
            'usersNumber' => $currentUser->getAccountName(),
        );
    }

HTML output if this is:
<p class="name navbar-text">david</p>
and it sits nicely next to the Drupal branding.

When i add

    public function getCacheContexts() {
        return ['user'];
    }

to the block code the HTML will change to:

<div>
    <p class="name navbar-text">david</p>
</div>

which will place the name under the Drupal branding.

I'd expect it to be the same. Also adding the cache context array in the build-methods render array does not work anymore either, it did however in 8.3.4.

Comments

TrippleDave created an issue. See original summary.

TrippleDave’s picture

Issue summary: View changes
Berdir’s picture

Status: Active » Closed (duplicate)
Issue tags: -custom block; caching

That's because by adding the user cache context, you trigger big_pipe which will insert that snippet through JS after initial rendering.

The div wrapper is added by the ajax system, which makes this a duplicate of #736066: ajax.js insert command sometimes wraps content in a div, potentially producing invalid HTML and other bugs.