wix-router.WixRouterRequest userAgent

I can’t get this to work. I am trying to capture the useragent.

I posted the sample code into my page code, but nothing happens:

export function myRouter_Router(request)
{ let userAgent = request.userAgent;
//then some more code to execute based on userAgent, like:
if(userAgent === “somevalue”) {wixLocation.to(“/somepage”)};
}

But it does not execute. This code is after the ending } from the page’s
$w.onReady(function () {

Anyone know how to get that userAgent value and then do stuff based on it?

Hi yogasleepaway.

wix-router is a backend implementation, wix-location can be used only on frontend scripts. For redirection response from a router you can use WixRouterResponse - Velo API Reference - Wix.com

Regards,
Genry.

Genry. I guess I don’t get the concept of front end vs backend. Are you saying I can capture the user agent but can’t load it to a variable to see it? You are right I want to redirect depending on the user agent but not sure how to put it all together.

Backend - is the code that runs on server.
Frontend - is the code that runs on the browser on client machine.

Routers can work only residing in routers.js file under the backend section.

User-Agent is a header which is sent from the browser to the server (backend) on each request.

Can you please describe the functionality you are trying to achieve?

Regards,
Genry.

Yes. Based on what you are saying I guess my need is for the server to evaluate the user agent and if it is facebooks internal mobile browser to redirect to a different page rather than to the one that the user clicked on in a link button or menu choice.

You can achieve that, just pay attention, that routers will answer the URL path under the convention the router function name is written.

For example:

export function myRouter_Router(request) {
 ...
}

Will be called for any requests for URLs:
https:///myRouter/…
https://.wixsite.com//myRouter/…

Which might be different than your page urls.

Regards,
Genry.

I think I get you. It sounds like I need a routers.js file that I will store a list of the pages that I want the “myRouter_router” function to be executed on. I’ll try that. In there words, the routers.js file will be something like:

https://mysite.com/home/
https://mysite.com/stuff/
https://mysite.com/things/

assuming I want that function to run when those pages are being accessed. Is that right?

Hi.

Please follow the article Velo: Creating a Router | Help Center | Wix.com

You can create a router for every page or create several pages on the same router.
Please pay attention, that those pages will not appear automatically on the menu, so you will need to add a link to the pages manually using the menu settings page.

Regards,
Genry.

Thanks Genry - I think I understand it now - appreciate your help. I’ll give it a whirl this week sometime.