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

Article autor
September 9, 2025
How to redirect back to previous page in Elixir & Phoenix?
Elixir Newsletter
Join Elixir newsletter

Subscribe to receive Elixir news to your inbox every two weeks.

Oops! Something went wrong while submitting the form.

Table of contents

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

Related posts

Dive deeper into this topic with these related posts

No items found.

You might also like

Discover more content from this category

Why & when you should use PostgreSQL deferred uniqueness constraints

Learn a trick that will allow you to manage item order in Postgres tables easier & faster.

Implicit try in Elixir

In the world of Elixir programming, there are numerous features and syntactic constructs that contribute to the language's elegance and expressiveness. One such hidden gem is the concept of "implicit try".

How to check if a set contains exact values with Jest in JS?

TLDR: With jest-extended package you can write: expect([...set]).toIncludeSameMembers([value1, value2]);. If you are looking to a native, but longer solution scroll down a bit.