I need some help implementing my router!

Hi, I have coded my router on the back end of my site. It is for redirecting visitors weather they are a desktop viewer or mobile, and each have a separate home page.

I currently have a landing page handling this similar task, with hidden buttons- but I created the router hoping this will handle the redirect more seamlessly, and I can remove the landing page. Better for SEO purposes as well.

Now that I have the router, what do I need to change on my pages end, for it to function properly? Should I have my desktop home page set at the site “home” since my code redirects mobile visitors? Do I need to “add pages” under the router page in my menu? Can’t seem to find articles on this process.

See my router code below, might help give some context. (Sample URL) Currently, my landing page is the “home” page.

import {redirect} from ‘wix-router’;

export function myRouter_Router(request) {

let formFactor = request.formFactor;//“desktop”){

if (formFactor=== “mobile”)

return redirect(“https://www.mysite.com/mobilehome”);

}

Hi,

In order to present different pages you need to set one of them as your router page, because the router page is the one that get the data from the backend router function.
You can add a getRouterData() function in the onReady() function of your page in order to determine if the user is going to see the current page or open a new page (using wixLocation.to() function).

Refer to your code, what you need to return from the router function is a value that let you now if the user is on mobile or desktop (like boolean value). After that, add the condition in the onReady() function and navigate the user.

For more info:
https://support.wix.com/en/article/corvid-creating-a-router
https://www.wix.com/corvid/reference/wix-location.html#to

Best,
Sapir

Hi Sapir, thanks for your response I really appreciate it. I have a couple of questions:

1: How do I set my page as the router page? “Add page to router” in the pages menu?
2: To be clear, the code I have above what I should use and there needs to be an additional wixLocation.to() function on the page I assign to the router?
3: I am unclear what you mean by the last thought “Refer to your code, what you need to return from the router function is a value that let you now if the user is on mobile or desktop (like boolean value). After that, add the condition in the onReady() function and navigate the user.”

Isn’t that what my router code is for?

Thank you again!