Routers: redirect() with queryString

I have follow problem with Routers redirect function:

import {redirect, ok, notFound} from "wix-router";

export async function apartments_Router(request) {
    if(request.path[0] == '') 
        return ok('apartments-page', request);
         
    let queryString = '?floor=1&ap=Apartment17';
    return redirect('/apartments/'+queryString, '302');
}

When I run url https://…/apartments/Apartment17/floor2
It brings me to “/apartments/” url without my queryString params. Why?

try to remove the last / from /apartements/

Does that solve the issue?

  • You’re better use the falsy value:
import {redirect, ok, notFound} from "wix-router";
export function apartments_Router(request) { 
    if(!request.path) {
        return ok('apartments-page', request);
         }
    let queryString = '?floor=1&ap=Apartment17';
    return redirect('/apartments'+ queryString, '302');
}

Everything after ? cut out by redirect() function((((
with slash or without(((

Try to play with it a little bit in order to locate the problem.

  1. Does the redirection work well if you remove the first condition?

  2. Does the redirection work if you use the full URL (https://domain.com/…)?

  3. Add console.log(path), and console.log(‘step1’) etc… to your code and look at the logs in the site logs.

Does the redirection work if you use the full URL (https://domain.com/…)?
YES!!!
Thanks a lot for this hack!!!