Router always gives a 500 time out error

Hello,

I am trying to make a router system that checks a collection to see if the user has the permission to view the page.

However, any time I use the beforeRouter function, I get a 500 time out error. Here’s my code.

import {redirect, next} from "wix-router";

export function private_beforeRouter(request) {
 return next()
}

Even this simple code doesn’t work for some reason.

I tried using Router too, however even that gives a 500 time out. Here’s the code for that.

import {ok} from "wix-router";

export function private_Router(request) {
 return ok(request.path[0])
}

How is this possible? Am I doing something wrong?

Thanks,
Miro.

And what’s the URL you’re using?

What exactly do you mean by that? The prefix that I’m using for the router is “private”

@virtahepoville I just wanted to confirm you were using the prefix and path correctly (because otherwise I don’t have an idea why it doesn’t work for you :slight_smile: ).

@jonatandor35 Ah ok, do you know any steps I could perhaps do to troubleshoot this problem?

@virtahepoville first I’m not sure what you’re trying to do.
If you wish to redirect to anther page then you should use
.redirect() and not .ok().
For example:

import {redirect} from "wix-router";
export function private_Router(request) {
 return redirect("/"  +request.path[0]);
}

and If you wish to continue to the same page, then you should use:

return ok("private-page");

@jonatandor35 I’m trying to continue to the same page. I just tried this but it still is giving me a time out error. Redirects work fine, it’s just the ok() and next() that don’t work.

import {ok} from "wix-router";

export function private_Router(request) { 
 return ok("private-page");
}

I managed to fix the problem. It was due to the fact that I didn’t know how the format of the page string worked in the ok function. I couldn’t find any clear documentation on how it worked. The page name I used had special characters (ö and ä).

So for the page name ‘Pyörä’, I thought the correct format would be ok(‘pyora’) or ok(‘private-pyora’) even though it actually is ok(‘Pyörä’). This made the ok(request.path[0]) not work because the request.path[0] was ‘pyora’. I wish the page format could be simpler, it should work like in the redirect function which would make the usage of this function a lot easier. (Now, I have to add if statements to check if the request.path[0] is ‘pyora’ just to change it to ‘Pyörä’.)

As for the private_beforeRouter function, it did not work because when I used it, I deleted the private_Router function. This resulted in the following error which I hadn’t spotted in my testing:

‘private_Router’ function not found in ‘backend/router.js’

I had no idea that the Router function always had to be present. I wish it didn’t, because now I have to add an useless Router function that always calls ok(). Same for the Sitemap function, this occasionally leads to the same error.