Wrong slug on blog page

This code is used after <head> and before <body> and is supposed to
get current page’s slug. It works great, however it will get the slug
of first blog post when on “general” blog page.

$post is going to be set to the first post in the Loop when the page loads. On “page” pages with only one post in the Loop that is going to be the page data you expect. On archive pages it will be the first post in the set of posts on the page. This is what appears to be happening. You can’t trust $post explicitly like that– that is, without understanding exactly when it is set to what. (Most loops, even secondary ones, will alter this variable as well)

To get the page data itself you need get_queried_object() but be careful with that too as it will be different on various types of pages– sometimes a post object, sometimes a user object, sometimes null.