I think it'll be better to just hard code it, but if you think the url may be dynamic sometimes, you can use JavaScript to handle it.
I don't have an answer to whether it can be done on the server side.
But, I think you should checkout Monica Lent's SEO for Devs course if you want a proper understanding.
Nemanja Mitic
Can I set url with Javasript or it must be hardcoded or done server-side? Will it affect SEO?
<meta property="og:url" content={window?.location.href} />