How to redirect back to previous page in Elixir & Phoenix?

In this post, you'll learn how to easily redirect users to the previous path using the Navigation History plug.
It's super easy, first add the plug to dependencies:
def deps do
[
...,
{:navigation_history, "~> 0.3"}]
end
Then, add it to your pipeline of choice (in most cases it's gonna be :browser):
pipeline :browser do
...
plug NavigationHistory.Tracker
end
Now, you can use use available NavigationHistory.last_path/1 function to access the previous path:
redirect(conn, to: NavigationHistory.last_path(conn))
That's it! You can also exclude specific paths from navigation history:
plug NavigationHistory.Tracker, excluded_paths: ["/login", ~r(/admin.*)]
Find more details about this lib here: https://github.com/danhper/plug-navigation-history.
Until next time!
Check this article: Why Elixir & Phoenix is a great choice for your web app in 2022
Work with a team that keeps learning and building better software every day.
Related posts
Dive deeper into this topic with these related posts
You might also like
Discover more content from this category
Sometimes we want to store some piece of information while using a terminal, for example, a result of an executed command. We usually save it into some temporary file which is going to be deleted after all. There’s a better way.
It's easy to contain absolute positioned elements. Things get a little trickier when you want to contain a fixed positioned element without changing its stylings.
So you don’t know what’s the type of struct you’re passing somewhere? Maybe it can be one of few types and you have to distinguish them? Or any other reason… But it’s about checking the struct type. Just use one of the coolest Elixir features - pattern matching!
