rsnext/test
Tim Neutkens 48948022c7
Catch redirect() in root layout (#47345)
### What?

Adds the error boundary used to catch `redirect()` above the root
layout.

### Why?

Currently calling `redirect()` in the root layout causes `NEXT_REDIRECT`
to bubble up to the error boundary because we didn't have a
redirect-boundary above the place where the root layout is rendered
client-side.

### How?

- Moved redirect-boundary into a separate file
- Added redirect-boundary around the `cache.subTreeData` in app-router
(around the root layout)

fix NEXT-315 ([link](https://linear.app/vercel/issue/NEXT-315))
Fixes #42556


I've verified the reproduction shown in #42556 has been fixed earlier,
there was another report about calling `redirect()` in the root layout
and that's what this PR fixes.

Since that issue has many comments here's some additional context:
- `redirect()` can only be called during React component rendering.
- This means you can't run `redirect()` in `onClick` or `useEffect()`
handlers.
- Calling `redirect()` in a server component or during prerendering of
client components it will add the right meta tag to trigger a redirect
	- `<meta httpEquiv="refresh" content={`0;url=${redirectUrl}`}>`
- Because of streaming rendering by the time your code runs the browser
will have already received the start of the stream and headers will have
been sent, as such you can't modify the headers, hence why the meta tag
is used instead of `location`.
- Calling `redirect()` in client components is supported while
rendering, e.g. if you have a condition:
	 ```tsx
	'use client'
    import { useState } from 'react'
    import { redirect } from 'next/navigation'
	export function ClientComponent() {
		const [clicked, setClicked] = useState()
		if(clicked) {
			redirect('/another-page')
		}

		return <>
			<button onClick={() => setClicked(true)}>Click to redirect</button>
		</>
	}
	``` 


<!-- Thanks for opening a PR! Your contribution is much appreciated.
To make sure your PR is handled as smoothly as possible we request that
you follow the checklist sections below.
Choose the right checklist for the change(s) that you're making:

## For Contributors

### Improving Documentation or adding/fixing Examples

- The "examples guidelines" are followed from our contributing doc
https://github.com/vercel/next.js/blob/canary/contributing/examples/adding-examples.md
- Make sure the linting passes by running `pnpm build && pnpm lint`. See
https://github.com/vercel/next.js/blob/canary/contributing/repository/linting.md

### Fixing a bug

- Related issues linked using `fixes #number`
- Tests added. See:
https://github.com/vercel/next.js/blob/canary/contributing/core/testing.md#writing-tests-for-nextjs
- Errors have a helpful link attached, see
https://github.com/vercel/next.js/blob/canary/contributing.md

### Adding a feature

- Implements an existing feature request or RFC. Make sure the feature
request has been accepted for implementation before opening a PR. (A
discussion must be opened, see
https://github.com/vercel/next.js/discussions/new?category=ideas)
- Related issues/discussions are linked using `fixes #number`
- e2e tests added
(https://github.com/vercel/next.js/blob/canary/contributing/core/testing.md#writing-tests-for-nextjs
- Documentation added
- Telemetry added. In case of a feature if it's used or not.
- Errors have a helpful link attached, see
https://github.com/vercel/next.js/blob/canary/contributing.md



## For Maintainers

- Minimal description (aim for explaining to someone not on the team to
understand the PR)
- When linking to a Slack thread, you might want to share details of the
conclusion
- Link both the Linear (Fixes NEXT-xxx) and the GitHub issues
- Add review comments if necessary to explain to the reviewer the logic
behind a change

### What?

### Why?

### How?

Closes NEXT-
Fixes #

-->

---------

Co-authored-by: kodiakhq[bot] <49736102+kodiakhq[bot]@users.noreply.github.com>
2023-03-22 09:34:50 +01:00
..
.stats-app Remove serverComponents from next.conf.js because it's unused (#43805) 2022-12-07 15:57:03 +01:00
__mocks__ Update Edge Runtime (#38862) 2022-07-21 18:29:19 +00:00
development Fix loader rule with wrong matching conditions (#47261 2023-03-17 23:35:02 +01:00
e2e Catch redirect() in root layout (#47345) 2023-03-22 09:34:50 +01:00
examples feat(cli): introduce --tailwind flag (#46927) 2023-03-16 16:06:27 +01:00
integration fix: add pretty error for exportPathMap with app dir (#47371) 2023-03-21 22:22:34 +00:00
lib fix(cli): handle Tailwind CSS + src/ correctly (#47238) 2023-03-20 14:21:29 -07:00
production [next/jest] Support path aliases from tsconfig/jsconfig (#45815) 2023-03-01 10:52:32 +01:00
unit Skip warning if there is app directory for no-html-link-for-pages rule (#42516) 2023-03-22 02:15:52 +00:00
.gitignore Fix server html insertion target (#42591) 2022-11-07 18:16:13 +01:00
jest-setup-after-env.ts Adds tests to ensure eslint-plugin-next's available rules are properly exported and recommended rules are correctly defined. (#38183) 2022-06-30 11:31:33 -05:00
jest.d.ts Adds tests to ensure eslint-plugin-next's available rules are properly exported and recommended rules are correctly defined. (#38183) 2022-06-30 11:31:33 -05:00
readme.md refactor: split up CONTRIBUTING.md (#40515) 2022-09-16 14:54:58 -07:00
test-file.txt

See Testing for more information on how you can run/write/debug tests for Next.js.