Unverified Commit 8bd92d9c authored by archanaagivale30's avatar archanaagivale30 Committed by GitHub

Update redirects example for App router (#67065)

Hello

This PR updates the redirects example to use App Router.

Migrated from `next/router` which is not supported in latest nextJS to
`next/navigation`.
Co-authored-by: 's avatarSam Ko <sam@vercel.com>
parent 16cc5fab
...@@ -2,7 +2,7 @@ ...@@ -2,7 +2,7 @@
This example shows how to use [redirects in Next.js](https://nextjs.org/docs/api-reference/next.config.js/redirects) to redirect an incoming request path to a different destination path. This example shows how to use [redirects in Next.js](https://nextjs.org/docs/api-reference/next.config.js/redirects) to redirect an incoming request path to a different destination path.
The index page ([`pages/index.tsx`](pages/index.tsx)) has a list of links that match the redirects defined in [`next.config.js`](next.config.js). Run or deploy the app to see how it works! The index page ([`app/page.tsx`](app/page.tsx)) has a list of links that match the redirects defined in [`next.config.js`](next.config.js). Run or deploy the app to see how it works!
## Deploy your own ## Deploy your own
......
import styles from "../styles.module.css"; import styles from "../../styles.module.css";
type CodeProps = { type CodeProps = {
children: React.ReactNode; children: React.ReactNode;
......
import Link from "next/link"; import Link from "next/link";
import styles from "../styles.module.css"; import styles from "../../styles.module.css";
export default function About() { export default function About() {
return ( return (
......
export const metadata = {
title: "Next.js",
description: "Generated by Next.js",
};
export default function RootLayout({
children,
}: {
children: React.ReactNode;
}) {
return (
<html lang="en">
<body>{children}</body>
</html>
);
}
import { useRouter } from "next/router"; "use client";
import { usePathname } from "next/navigation";
import Link from "next/link"; import Link from "next/link";
import styles from "../../styles.module.css"; import styles from "../../../styles.module.css";
import Code from "../../components/Code"; import Code from "../../_components/Code";
export default function News() {
const { asPath, route, query } = useRouter();
export default function News({ params }: Params) {
const pathname = usePathname();
return ( return (
<div className={styles.container}> <div className={styles.container}>
<div className={styles.card}> <div className={styles.card}>
<h1>Path: {asPath}</h1> <h1>Path: {pathname}</h1>
<hr className={styles.hr} /> <hr className={styles.hr} />
<p>
This page was rendered by <Code>{`pages${route}.tsx`}</Code>.
</p>
<p> <p>
The query <Code>slug</Code> for this page is:{" "} The query <Code>slug</Code> for this page is:{" "}
<Code>{JSON.stringify(query.slug)}</Code> <Code>{JSON.stringify(params.slug)}</Code>
</p> </p>
<Link href="/">&larr; Back home</Link> <Link href="/">&larr; Back home</Link>
</div> </div>
</div> </div>
); );
} }
type Params = {
params: {
slug: string;
};
};
import styles from "../styles.module.css"; import styles from "../styles.module.css";
import Link from "next/link"; import Link from "next/link";
import Code from "../components/Code"; import Code from "./_components/Code";
export default function Index() { export default function Home() {
return ( return (
<div className={styles.container}> <div className={styles.container}>
<div className={styles.card}> <div className={styles.card}>
<h1>Redirects with Next.js</h1> <h1 className="text-white">Redirects with Next.js</h1>
<hr className={styles.hr} /> <hr className={styles.hr} />
<p> <p>
The links below are{" "} The links below are custom
<Link <Link
href="https://nextjs.org/docs/api-reference/next.config.js/redirects" href="https://nextjs.org/docs/api-reference/next.config.js/redirects"
legacyBehavior legacyBehavior
> >
<> <>
custom <Code>redirects</Code> <Code>redirects</Code>
</> </>
</Link>{" "} </Link>{" "}
that redirect an incoming request path to a different destination that redirect an incoming request path to a different destination
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment