Tips from open-source: How redirect in Next.js works under the hood
Tags: javascript, opensource, nextjs, react
Published: 2024-05-13 22:29:40

In this article, you will learn how redirect in next.js works under the hood by looking at the Next.js source code around redirect functionality.

My approach:
------------

1.  Start from the \`import { redirect } from "next/navigation"'
2.  Back trace the redirect function to redirect.ts file
3.  redirect.ts explained
4.  How does throwing an error make the redirect work?
5.  Check redirect.test.ts to understand the full context
6.  redirect-boundary.ts has the redirect functionality

[![Best practices in open source](https://media.licdn.com/dms/image/D4E12AQHbdLayHhmJrg/article-inline_image-shrink_1000_1488/0/1715638020703?e=1721260800&v=beta&t=nxuVu4WUQrAgbrRr7HTy31fOHCFu56v_zY5n707F-uI)](https://tthroo.com)


1\. Start from the import:
--------------------------

Redirect function is imported from next/navigation. You can read more about redirect at: [https://nextjs.org/docs/app/api-reference/functions/redirect](https://nextjs.org/docs/app/api-reference/functions/redirect).

But where do I find the next/navigation code? You can find this code in [navigation.js](https://github.com/vercel/next.js/blob/c1f8d9317588e51a8a31240f6add36b5f2c9f9bf/packages/next/navigation.js). This file basically exports the [navigation.ts](https://github.com/vercel/next.js/blob/c1f8d9317588e51a8a31240f6add36b5f2c9f9bf/packages/next/src/client/components/navigation.ts) that is [/packages/next/src/client/components](https://github.com/vercel/next.js/tree/c1f8d9317588e51a8a31240f6add36b5f2c9f9bf/packages/next/src/client/components).

2\. Back trace the redirect function to redirect.ts file
--------------------------------------------------------

Although, [navigation.ts](https://github.com/vercel/next.js/blob/c1f8d9317588e51a8a31240f6add36b5f2c9f9bf/packages/next/src/client/components/navigation.ts) has 272 lines of code at the time of writing this article, what we are interested in is the export of “redirect” at the end of the file as shown below. Focus on what you are looking for, don’t let the other code distract you.

![](https://media.licdn.com/dms/image/D4E12AQGOioxXwf4iSQ/article-inline_image-shrink_1500_2232/0/1715638018067?e=1721260800&v=beta&t=5WmJJeCCju1CJs_C67F-qn1vNEpYexcYvgr9Z_Yldas)

As you can see, redirect is an export from navigation.react-server.ts. Inside the [navigation.react-server.ts](https://github.com/vercel/next.js/blob/c1f8d9317588e51a8a31240f6add36b5f2c9f9bf/packages/next/src/client/components/navigation.react-server.ts), you will find that redirect function is, in fact, from redirect.ts.

![](https://media.licdn.com/dms/image/D4E12AQEzs7BiI5HoEQ/article-inline_image-shrink_1500_2232/0/1715638018333?e=1721260800&v=beta&t=XkXOAHpyK_Y3-VZwiOG6sJbh_qoSsTYBulGE3KYT_Dk)

I did not know nested exports. Interesting.

3\. Render.ts explained
-----------------------

“Invoking the redirect() function throws a NEXT\_REDIRECT error and terminates rendering of the route segment in which it was thrown.” — [Nextjs docs](https://nextjs.org/docs/app/api-reference/functions/redirect#server-component)

Let’s look at the code snippet that throws the error inside [redirect.ts](https://github.com/vercel/next.js/blob/c1f8d9317588e51a8a31240f6add36b5f2c9f9bf/packages/next/src/client/components/redirect.ts)

![](https://media.licdn.com/dms/image/D4E12AQEolwLxhEgPiA/article-inline_image-shrink_1000_1488/0/1715638022015?e=1721260800&v=beta&t=o9lOEXw5VEl-Sn4sObx80EZDy0oy8aVdq-vHNrtjeOI)

4\. How does throwing an error make the redirect work?
------------------------------------------------------

Redirect function throws an error that sets the error.digest, but it was not obvious as to how throwing an error makes the redirect work.

I searched long and hard, my initial thoughts were around trying to find that catch block since redirect throws an error. I scoured the server related files but of no use.

I kept at it until it started to make sense when I looked at redirect.test.ts

5\. Check redirect.test.ts to understand the full context
---------------------------------------------------------

![](https://media.licdn.com/dms/image/D4E12AQF9zH8GRtOcig/article-inline_image-shrink_1500_2232/0/1715638019011?e=1721260800&v=beta&t=yFZV4Qz3BZ6m-xPZF5AvD9U0ivphxw9jvFgHiN34j-w)

This one time I read somewhere that you could use tests to understand the context of a function. Hence the reason why I checked the code in [redirect.test.ts](https://github.com/vercel/next.js/blob/canary/packages/next/src/client/components/redirect.test.ts)

It is evident from the test code that getURLFromRedirectError(err) is called. You will find this function [react-boundary.ts](https://github.com/vercel/next.js/blob/c1f8d9317588e51a8a31240f6add36b5f2c9f9bf/packages/next/src/client/components/redirect-boundary.tsx#L8)

Okay, test calls this getURLFromRedirectError function but what’s the execution flow? What happens after the error is thrown? Making a search based on getURLFromRedirectError function, the following are the results:

![](https://media.licdn.com/dms/image/D4E12AQFDSuLAo3YEvw/article-inline_image-shrink_1000_1488/0/1715638021596?e=1721260800&v=beta&t=UT-T3XLwzZMijSue0o9kRcvtvHeoFMKchYk-54jc_Pk)

6\. redirect-boundary.ts has the redirect functionality
-------------------------------------------------------

The answer is Next.js redirect uses useRouter hook and depending on redirect type, it either pushes or replaces the url

![](https://media.licdn.com/dms/image/D4E12AQHLVQRp5Ykksw/article-inline_image-shrink_1000_1488/0/1715638019214?e=1721260800&v=beta&t=nsgw663dLQOcd5SC6g5qAIal-IoJVW1FSHnGjRnAnlE)

This redirect-boundary is part of app-router code as shown below

![](https://media.licdn.com/dms/image/D4E12AQFWgZwrl08U_w/article-inline_image-shrink_1000_1488/0/1715638021476?e=1721260800&v=beta&t=0ipL1PJvxnCjzOC-2-P1C7OJvYHxssFoEW0xh40KLOQ)

when the error is thrown from redirect, it is caught by the redirect-boundary and redirect occurs. It is quite fascinating to find that redirect function itself does not do the redirect but rather redirect-boundary inside app-router handles the routing logic.

Conclusion:
-----------

I do not know why this API is designed this way but we could use this pattern of throwing an error from your npm package and use a boundary , preferably at the root level, to catch that error and do your thing.

If it wasn’t for me checking the redirect.test.ts code I would not have been able to figure out how the redirect works because I was hoping to find some sort out catch block in the redirect.ts but that is not the case.

Feel free to reach out to me at [ramu.narasinga@gmail.com](mailto:ramu.narasinga@gmail.com) if you have any questions about this article.

Further reading:
----------------

1.  [https://github.com/vercel/next.js/blob/c1f8d9317588e51a8a31240f6add36b5f2c9f9bf/packages/next/src/client/components/navigation.react-server.ts](https://github.com/vercel/next.js/blob/c1f8d9317588e51a8a31240f6add36b5f2c9f9bf/packages/next/src/client/components/navigation.react-server.ts)
2.  [https://github.com/vercel/next.js/blob/c1f8d9317588e51a8a31240f6add36b5f2c9f9bf/packages/next/navigation.js](https://github.com/vercel/next.js/blob/c1f8d9317588e51a8a31240f6add36b5f2c9f9bf/packages/next/navigation.js)
3.  [https://github.com/vercel/next.js/blob/c1f8d9317588e51a8a31240f6add36b5f2c9f9bf/packages/next/src/client/components/redirect.ts](https://github.com/vercel/next.js/blob/c1f8d9317588e51a8a31240f6add36b5f2c9f9bf/packages/next/src/client/components/redirect.ts)
4.  [https://github.com/vercel/next.js/blob/canary/packages/next/src/client/components/redirect.test.ts](https://github.com/vercel/next.js/blob/canary/packages/next/src/client/components/redirect.test.ts)
5.  [https://github.com/vercel/next.js/blob/c1f8d9317588e51a8a31240f6add36b5f2c9f9bf/packages/next/src/client/components/redirect-boundary.tsx#L8](https://github.com/vercel/next.js/blob/c1f8d9317588e51a8a31240f6add36b5f2c9f9bf/packages/next/src/client/components/redirect-boundary.tsx#L8)