Tutorial
Redirections

Redirections

Redirecting to a different page

What if there is a Pokémon among all of them that should be considered the GOAT? What we are going to do right now is creating a new route /pokemons/GOAT that points to the best Pokémon of the first generation.

First, let's create a new route by just creating a new file /pokemons/GOAT.rs and pasting the following code:

// src/routes/pokemons/GOAT.rs
use tuono_lib::{Request, Response};

#[tuono_lib::handler]
async fn redirect_to_goat(_req: Request) -> Response {
    // Of course the GOAT is mewtwo - feel free to select your favourite 😉
    Response::Redirect("/pokemons/mewtwo".to_string())
}

Now let's create the button in the home page to actually point to it!

// src/routes/index.tsx

      <ul style={{ flexWrap: 'wrap', display: 'flex', gap: 10 }}>
++      <PokemonLink pokemon={{ name: 'GOAT' }} id={0} />
++
        {data.results.map((pokemon, i) => (
          <PokemonLink key={i + 1} name={pokemon.name} id={i + 1} />
        ))}
      </ul>

Now at http://localhost:3000/ You will find a new link at the beginning of the list. Click on it and see the application automatically redirecting you to your favourite pokemon's route!

Edit page