Drupal 10 Help: Drupal 10 Upkeep and Help Service Early Rendering Drupal 10 Upkeep and Help Service A Lesson in Debugging 8

I got here throughout Drupal Developer following error Drupal Developer different day on a shopper’s 8 web site Drupal 10 Upkeep and Help Service LogicException Drupal 10 Upkeep and Help Service Drupal Developer controller consequence claims to be offering related cache metadata, however leaked metadata was detected. Please guarantee you aren’t rendering content material too early. Leaked? That sounded unhealthy. Rendering content material too early? I did not know what that meant, nevertheless it additionally sounded unhealthy. Worst of all, this was inflicting a PHP deadly error together with a 500 response code. Happily, I caught Drupal Developer error throughout development, so there was time to determine precisely what was happening. In so doing, I realized some issues that may deepen our understanding of ’s cache API. Down Drupal Developer rabbit gap I knew that this error was being attributable to our code. We have been writing a customized RestResource plugin, which is meant to fetch some knowledge from Drupal Developer entity API and return that knowledge, able to be serialized and full with cacheability metadata. This tradition RestResource was Drupal Developer solely route that may set off Drupal Developer error, and it solely began taking place half means by development as Drupal Developer codebase grew complicated. It had been working effective, till Drupal Developer error famous above, which I embrace right here in full with a stack hint Drupal 10 Upkeep and Help Service Drupal Developer web site encountered an sudden error. Please attempt once more later. LogicException Drupal 10 Upkeep and Help Service Drupal Developer controller consequence claims to be offering related cache metadata, however leaked metadata was detected. Please guarantee you aren’t rendering content material too early. Returned object class Drupal 10 Upkeep and Help Service restResourceResponse. in CoreEventSubscriberEarlyRenderingControllerWrapperSubscriber->wrapControllerExecutionInRenderContext() (line 154 of core/lib//Core/EventSubscriber/EarlyRenderingControllerWrapperSubscriber.php). CoreEventSubscriberEarlyRenderingControllerWrapperSubscriber->CoreEventSubscriber{closure}() (Line Drupal 10 Upkeep and Help Service 135) SymfonyComponentHttpKernelHttpKernel->handleRaw(Object, 1) (Line Drupal 10 Upkeep and Help Service 57) SymfonyComponentHttpKernelHttpKernel->deal with(Object, 1, 1) (Line Drupal 10 Upkeep and Help Service 57) CoreStackMiddlewareSession->deal with(Object, 1, 1) (Line Drupal 10 Upkeep and Help Service 47) CoreStackMiddlewareKernelPreHandle->deal with(Object, 1, 1) (Line Drupal 10 Upkeep and Help Service 119) cdnStackMiddlewareDuplicateContentPreventionMiddleware->deal with(Object, 1, 1) (Line Drupal 10 Upkeep and Help Service 47) CoreStackMiddlewareReverseProxyMiddleware->deal with(Object, 1, 1) (Line Drupal 10 Upkeep and Help Service 50) CoreStackMiddlewareNegotiationMiddleware->deal with(Object, 1, 1) (Line Drupal 10 Upkeep and Help Service 23) StackStackedHttpKernel->deal with(Object, 1, 1) (Line Drupal 10 Upkeep and Help Service 663) CoreKernel->deal with(Object) (Line Drupal 10 Upkeep and Help Service 19) I used to be confused that our code did not seem in Drupal Developer stack hint; that is all core code. We have to go deeper. As I do when this type of state of affairs arises, I took to Drupal Developer debugger. I set a breakpoint at Drupal Developer place in core the place Drupal Developer exception was being thrown searching for clues. Right here have been my instant environment Drupal 10 Upkeep and Help Service // … elseif ($response instanceof AttachmentsInterface || $response instanceof CacheableResponseInterface || $response instanceof CacheableDependencyInterface) { throw new LogicException(sprintf(‘Drupal Developer controller consequence claims to be offering related cache metadata, however leaked metadata was detected. Please guarantee you aren’t rendering content material too early. Returned object class Drupal 10 Upkeep and Help Service %s.’, get_class($response))); } // … International land. Figuring out a smidgen about Drupal Developer Cache API in 8 and Drupal Developer context of what we have been making an attempt to do, I understood that we have been ending up right here partially as a result of we have been returning a response object that has cacheability metadata on it. That’s, we have been returning a ResourceResponse object that implements CacheableResponseInterface, together with Drupal Developer related cacheability metadata with it. I may see from Xdebug that Drupal Developer $response variable in Drupal Developer snippet above corresponded to Drupal Developer ResourceResponse object we have been returning, and it was filled with our knowledge object and able to be serialized.  undefined So so far as I knew, I used to be taking part in good and including cacheability metadata like a very good developer ought to. What provides? Seeing Drupal Developer forest for Drupal Developer timber It was at this level I felt myself getting misplaced in Drupal Developer weeds. I wanted to take a step again and reread Drupal Developer error message. Once I did, I noticed that I did not perceive what “early rendering” was. I knew it had some connection to caching, so I began by studying by all Drupal Developer Cache API docs on Drupal 10.org. I’ve learn these a number of occasions in Drupal Developer previous, nevertheless it’s simply a kind of subjects, at the very least for me, that requires fixed reinforcement. One other related doc I discovered was CachebleResponseInterface. These supplied a very good background and laid out some terminology for me, however nothing right here talks about early rendering. I additionally reviewed Drupal Developer Render API docs however once more, no point out of early rendering, and nothing getting me nearer to a decision. So then I zoomed again in a little bit bit, to Drupal Developer mum or dad class of Drupal Developer code which threw Drupal Developer error Drupal 10 Upkeep and Help Service CoreEventSubscriberEarlyRenderingControllerWrapperSubscriber As is commonly Drupal Developer case in 8 core code, there was a wonderful and descriptive doc block for Drupal Developer class. I usually discover this to be key to understanding 8. Core committers take nice care to doc Drupal Developer code they write which makes it value getting snug with studying by core and contrib code. When controllers name Drupal 10_render() (RendererInterface Drupal 10 Upkeep and Help Service Drupal 10 Upkeep and Help Servicerender()) outdoors of a render context, we name that “early rendering.” Controllers ought to return solely render arrays, however we can’t stop controllers from doing early rendering. Drupal Developer drawback with early rendering is that Drupal Developer bubbleable metadata (cacheability & attachments) are misplaced. Ultimately a definition for early rendering! Nevertheless, our code wasn’t (at the very least immediately) inside a controller, it by no means known as Drupal 10_render() that I may inform, and what in Drupal Developer world is a render context? No person accountable Nonetheless, in want of some context for understanding what was happening right here, I checked out git blame to search out out the place Drupal Developer code that was throwing Drupal Developer error about early rendering got here from. Ever since I began to do 8 development, I’ve at all times discovered it helpful to make use of a clone of regionally for such events. PHPStorm makes utilizing git blame fairly simple. In Drupal Developer file you’re all in favour of—opened in Drupal Developer editor—good click on Drupal Developer line numbers column and click on Annotate. As soon as Drupal Developer annotations show, click on Drupal Developer one which corresponds to Drupal Developer line that you simply’re all in favour of to see Drupal Developer commit message.  undefined Most, if not all, core commits may have a difficulty quantity in Drupal Developer description, on this case, here’s what I discovered Drupal 10 Upkeep and Help Service Drupal 10 search engine optimisation #2450993 by Wim Leers, Fabianx, Crell, dawehner, effulgentsia Drupal 10 Upkeep and Help Service Rendered Cache Metadata created throughout Drupal Developer most important controller request will get misplaced Loading up Drupal Developer situation, I’m confronted with a wall of textual content, 159 feedback. Though I did ultimately wade by it out of morbid curiosity, what I instantly do when confronted with a large closed core situation, is test for a change document. Drupal Developer 8 dev cycle has been actually wonderful about documenting modifications, and alter information have actually helped in Drupal Developer transition from earlier 7 ideas and explaining new ideas in 8. For any core situation, first, have a look in Drupal Developer proper sidebar of Drupal Developer situation for “Change information for this situation”, and comply with any which can be linked to get a birds-eye view of Drupal Developer change. For those who haven’t already, it’s additionally useful to bookmark Drupal Developer Change information for core itemizing because it’s a fantastic place to look whenever you’re caught on one thing 8. undefined Drupal Developer change document was very useful, so if you happen to’re , I like to recommend you undoubtedly give it a learn. Briefly, early rendering was once rampant (in core and contrib), and this was an issue as a result of cacheability metadata was misplaced. Drupal Developer change launched a strategy to wrap all controllers, detect early rendering, catch and merge Drupal Developer cacheability metadata into Drupal Developer controllers’ return (often a render array). That’s all nicely and good, however wait! You may assume, “If it’s dealing with Drupal Developer cacheabillity metadata from early rendering, why is it nonetheless throwing an error!?” Effectively, going again to Drupal Developer snippet the place Drupal Developer exception is thrown from earlier Drupal 10 Upkeep and Help Service // … elseif ($response instanceof AttachmentsInterface || $response instanceof CacheableResponseInterface || $response instanceof CacheableDependencyInterface) { throw new LogicException(sprintf(‘Drupal Developer controller consequence claims to be offering related cache metadata, however leaked metadata was detected. Please guarantee you aren’t rendering content material too early. Returned object class Drupal 10 Upkeep and Help Service %s.’, get_class($response))); } // … What this boils right down to is that if your controller is returning a response object of kind, AttachementsInterfaceCacheableResponseInterface, or CacheableDependencyInterface, doesn’t offer you a cross, nor does it deal with cacheability metadata from early rendering for you. takes Drupal Developer place that since you’re returning this sort of response, you also needs to be accountable, concentrate on and deal with early rendering your self. From Drupal Developer change log Drupal 10 Upkeep and Help Service Because you’re returning responses, you wish to totally management what is distributed, so that you also needs to be a accountable citizen and never do any early rendering. I solemnly swear to not early render Okay, so no early rendering, bought it. However, what if it’s out of our management? In our case, Drupal Developer code we have been working in did not have any direct calls to Drupal 10_render() (RendererInterface Drupal 10 Upkeep and Help Service Drupal 10 Upkeep and Help Servicerender()). My subsequent tactic was to grasp extra about what was triggering early rendering.  To do that, I set a breakpoint in Drupal Developer sole implementation of RendererInterface Drupal 10 Upkeep and Help Service Drupal 10 Upkeep and Help Servicerender() after which hit Drupal Developer REST endpoint that was triggering Drupal Developer error. Xdebug instantly broke at that line, and inspecting Drupal Developer stack hint, we noticed a few of our code! Proof that we broke it! Progress.  undefined Because it seems, some code in one other customized Drupal 10 module was being known as. This code is supposed to wrap entity queries, massaging Drupal Developer return knowledge into one thing extra palpable and concise for Drupal Developer development crew that wrote it. Deep on this code, in processing node entities, it was together with a name to $node→url(), the place $node is a nodeEntityNode object. Seems, that triggers early rendering. To this, you may ask, “Why would one thing as innocuous as getting Drupal Developer URL for a node set off early rendering?” Drupal Developer reply, and I’m solely 80% certain after finding out this for some time (do right me if I’m mistaken), is that URLs can differ by context primarily based on language or Drupal Developer web site URL. They’ll even have dependencies, similar to Drupal Developer language configuration. Lastly, URLs can have CSRF tokens embedded in them, which differ by session. All of which is essential cacheability metadata that you simply wish to be included in Drupal Developer response. OK, so what’s a accountable developer to do? Drupal Developer full (and verbose) answer, courtesy of ohthehugemanatee (certainly), is to switch your $node→url() name with one thing like Drupal 10 Upkeep and Help Service // 1. Complicated Drupal 10 Upkeep and Help Service Drupal Developer methodology is named toString, but passing TRUE for Drupal Developer first param nets you a CoreGeneratedUrl object. $url = $node->toUrl()->toString(TRUE); // 2. Drupal Developer generated URL string, as earlier than. $url_string = $url->getGeneratedUrl(); // Add Drupal Developer $url object as a dependency of no matter you are returning. Perhaps a response? $response = new CacheableResponse($url_string, Response Drupal 10 Upkeep and Help Service Drupal 10 Upkeep and Help ServiceHTTP_OK); $response->addCacheableDependency($url); return $response; That’s loads, and it’ll be totally different relying on what you are doing. It’s damaged down into 3 elements. First, you wish to name $node->toUrl()->toString(TRUE);. It will primarily inform to trace any cacheability metadata, which is a part of producing Drupal Developer URL, and return an object from which you will get that cacheability metadata so you may take care of it. Drupal Developer second half is simply getting Drupal Developer precise URL string, $url_string = $url->getGeneratedUrl();, to do with as you please. Lastly, you want to account for any encountered cacheability metadata. In Drupal Developer context of a response as above, which means including Drupal Developer $url object as a cacheable dependency. In Drupal Developer context of a render array, it would imply merging Drupal Developer $url cacheability metadata into Drupal Developer render array. (eg. CacheableMetadata Drupal 10 Upkeep and Help Service Drupal 10 Upkeep and Help ServicecreateFromObject($url)→applyTo($render_array)) Wrap it up OK so now I understood the place Drupal Developer exception was coming from and why. I additionally perceive how I’d change Drupal Developer code that’s triggering an early rendering. However as I discussed earlier than, what if you happen to don’t management Drupal Developer code that’s triggering an early rendering? Is all hope misplaced? Not fairly. What you are able to do is wrap Drupal Developer code triggering Drupal Developer early render in a render context. Let’s take a look at some code Drupal 10 Upkeep and Help Service $context = new RenderContext(); /* @var CoreCacheCacheableDependencyInterface $consequence */ $consequence = Drupal 10 Upkeep and Help Service Drupal 10 Upkeep and Help Serviceservice(‘renderer’)->executeInRenderContext($context, perform() { // do_things() triggers Drupal Developer code that we do not do not management that in flip triggers early rendering. return do_things(); }); // Deal with any bubbled cacheability metadata. if (!$context->isEmpty()) { $bubbleable_metadata = $context->pop(); BubbleableMetadata Drupal 10 Upkeep and Help Service Drupal 10 Upkeep and Help ServicecreateFromObject($consequence) ->merge($bubbleable_metadata); } Let’s break this down Drupal 10 Upkeep and Help Service $context = new RenderContext(); Right here, I instantiate a brand new render context. A render context is a stack containing bubbleable rendering metadata. It’s a mechanism for amassing cacheability metadata recursively, aggregating or “effervescent” all of it up. By creating and passing it in Drupal Developer subsequent line, Drupal Developer render context is ready to seize what would have in any other case been misplaced cacheability metadata. /* @var CoreCacheCacheableDependencyInterface $consequence */ $consequence = Drupal 10 Upkeep and Help Service Drupal 10 Upkeep and Help Serviceservice(‘renderer’)->executeInRenderContext($context, perform() { // do_things() triggers Drupal Developer code that we do not do not management that in flip triggers early rendering. return do_things(); }); Right here I run some arbitrary code inside Drupal Developer render context I created. Drupal Developer arbitrary code, someplace alongside its execution path that we have now no management over, triggers early rendering. When that early rendering happens, since I’m wrapping Drupal Developer code in a render context, Drupal Developer cacheability metadata will bubble as much as Drupal Developer render context I setup and permit me to do one thing with it. // Deal with any bubbled cacheability metadata. if (!$context->isEmpty()) { $bubbleable_metadata = $context->pop(); BubbleableMetadata Drupal 10 Upkeep and Help Service Drupal 10 Upkeep and Help ServicecreateFromObject($consequence) ->merge($bubbleable_metadata); } Now I test if Drupal Developer context is non-empty. In different phrases, did it catch some cacheability metadata from one thing that did early rendering? If it did, I get Drupal Developer captured cacheability metadata with $context→pop() and merge it with my CoreCacheCacheableDependencyInterface object which can be returned. BubbleableMetadata is a helper class for coping with cacheability metadata. This merge half could look totally different relying in your context, however Drupal Developer concept is to include it into Drupal Developer response someway. Check out Drupal Developer static strategies in CoreRenderBubbleableMetadata  and its mum or dad class forCoreCacheCacheableMetadata some helpers to merge your cacheability metadata. Actually wrapping up That was a heavy, lengthy, complicated debug session. I realized loads digging into it and I hope you probably did as nicely. Let me know in Drupal Developer feedback if you happen to’ve ever run into one thing comparable and got here to a decision otherwise. I’d like to proceed furthering my understanding. Whereas it was nice to determine this out, I used to be left wanting a greater DX. Particularly, enhancing Drupal Developer indisputable fact that auto-magically handles early rendering in some instances, however not others. There’s additionally Drupal Developer odd workaround to seize cacheability metadata when cacheability metadata when calling $node→url() that would use some work. A fast search on Drupal Developer situation queue advised me I wasn’t alone. Hopefully, with time and consideration, this may be made higher. Definitely, there are good causes for Drupal Developer complexity, however it could be nice to steadiness that in opposition to Drupal Developer DX to keep away from extra epic debug periods. Acknowledgements Technical suggestions by Juampy NR Hero picture by Vladimir Kramer on Unsplash Drupal 10 Growth and Help

This article was republished from its original source.
Call Us: 1(800)730-2416

Pixeldust is a 20-year-old web development agency specializing in Drupal and WordPress and working with clients all over the country. With our best in class capabilities, we work with small businesses and fortune 500 companies alike. Give us a call at 1(800)730-2416 and let’s talk about your project.

FREE Drupal SEO Audit

Test your site below to see which issues need to be fixed. We will fix them and optimize your Drupal site 100% for Google and Bing. (Allow 30-60 seconds to gather data.)

Powered by

Drupal 10 Help: Drupal 10 Upkeep and Help Service Early Rendering Drupal 10 Upkeep and Help Service A Lesson in Debugging 8

On-Site Drupal SEO Master Setup

We make sure your site is 100% optimized (and stays that way) for the best SEO results.

With Pixeldust On-site (or On-page) SEO we make changes to your site’s structure and performance to make it easier for search engines to see and understand your site’s content. Search engines use algorithms to rank sites by degrees of relevance. Our on-site optimization ensures your site is configured to provide information in a way that meets Google and Bing standards for optimal indexing.

This service includes:

  • Pathauto install and configuration for SEO-friendly URLs.
  • Meta Tags install and configuration with dynamic tokens for meta titles and descriptions for all content types.
  • Install and fix all issues on the SEO checklist module.
  • Install and configure XML sitemap module and submit sitemaps.
  • Install and configure Google Analytics Module.
  • Install and configure Yoast.
  • Install and configure the Advanced Aggregation module to improve performance by minifying and merging CSS and JS.
  • Install and configure Schema.org Metatag.
  • Configure robots.txt.
  • Google Search Console setup snd configuration.
  • Find & Fix H1 tags.
  • Find and fix duplicate/missing meta descriptions.
  • Find and fix duplicate title tags.
  • Improve title, meta tags, and site descriptions.
  • Optimize images for better search engine optimization. Automate where possible.
  • Find and fix the missing alt and title tag for all images. Automate where possible.
  • The project takes 1 week to complete.