How can I use the entry UID in the entry URI The entries in my Craft app need a unique ‘non-guessable’ URL, so I figured I could use the UID in the URI of the entry.

So in the section, where I need to provide the URI format I entered "section-name/{uid}".

But this is giving me issues, since when I initially save an entry, the URI is "section-name". I think this is because the UID is not available on creation of the URI.

If I save the element for the second time, my problem is solved and the correct URI is generated.

Is there a way to prevent this issue from happening?

I already thought about creating a route, but that means that I need to generate the URI myself in every template, and this is prone to error and not future proof if we would like to change the URI format.

I also thought about creating a module which saves the entry for a second time when a new entry is saved for that section, but I can't find a way to create the correct event-listener.

Does anyone have a better solution or can help me with the event listener in a module?
I think this can be a starting solution. It rewrite slug of Entry after the Entry is saved to database. The URI is constructed from it. You can try it in any plugin, only register the EVENT_AFTER_SAVE_ELEMENT in init() function of Plugin.

use craft\events\ElementEvent;
use craft\services\Elements;

Event::on(
    Elements::class,
    Elements::EVENT_AFTER_SAVE_ELEMENT,
    function (ElementEvent $event){
        //identify the right Entry for example test sectionId, typeId, authorId
        if ($event-&gt;element instanceof craft\elements\Entry) {  
            $entry = $event-&gt;element;
            //only newly created 
            if($event-&gt;isNew){                  
                $entry-&gt;slug = $entry-&gt;uid;
                Craft::$app-&gt;elements-&gt;saveElement($entry);                 
            }
        }
    }
);