Function before page loads

Hello,
I have a some actions I need to trigger before the initial page load, that means, before “$w.onReady( function () {”
This actions are:

let url = wixLocation.url;
let browserLocale = wixWindow.browserLocale;
if ((url === 'https://www.xxx.com') && ((browserLocale === 'en-US') || (browserLocale === 'es-ES'))) {
wixLocation.to('https://www.xxx.com/?lang=es');

If I let those actions occur after the initial page load, what happens is the page ends loading and then redirect based on browser language. That is uncomfortable for the users and confusing. This NEEDS to happen before “$w.onReady(function () {”, in other words: “before the page loads”. Is this possible?

Thanks for the help,

You need to look into the wix-router API for this.