Inject metadata with onPageProcessed, but issues with cache

hi everyone

I m facing an issue with cache, i have a function in my theme onPageProcessed that insert open graph in head the vars come from my modular blueprints, all working ok with no cache

So if save a specific page, the others page “loose” their metadata
hope i’m clear maybe related to that thread!/plugin-development:header-injection

this is excerpt of my function
— php
public function onPageProcessed(Event $e)
$page = $e[‘page’];
$header = $page->header();
$metadata = $page->metadata();
$uri = $this->grav[‘uri’]->path();
$route = $page->route();

if (($uri == $route)) {
// Facebook Url
if (isset($header->seofacebookurl)) {
$seofacebookurl = $header->seofacebookurl;
$property = ‘og:url’;
$metadata[$property] = [ ‘property’ => $property, ‘content’ => $seofacebookurl ];
} else {
$seofacebookurl = $this->grav[‘uri’]->url;
$property = ‘og:url’;
$metadata[$property] = [ ‘property’ => $property, ‘content’ => $seofacebookurl ];

sorry to bump old thread. I’m still having that issue with the cache, each time i save the page the metadatas are injected with onPageProcessed. Hope someone can put me on the right track

for info: i inject with $page->metadata($metadata); in my public function onPageProcessed(Event $e)

i try onPageInitialized instead of onPageProcessed and inside onPageInitialized function i call a private function with all my meta stuff, it seems to work

Only recently I began to understand how to work with dynamic content and have caching enabled. The reason why your previous method using onPageProcessed didn’t work and using onPageInitialized does, is explained in Caching and Events.
The downside of using onPageInitialized is that the code in it is run on every page request. I’m curious why other pages loose their metadata when one page is saved? Do you clear the full cache somewhere?