Unverified Commit fb36def4 authored by SamPhillemon's avatar SamPhillemon Committed by GitHub

Example: Updated the example of rewrites to utilize the App Router (#69495)

This PR updates the rewrites example to use the App Router. Here are the
changes that have been made:

- Renamed the "pages" folder to the "app" folder.
- Updated the routing for /, /news/[...slug] & /about files to align
with the App Router.
- Added the layout.tsx file as part of the App Router.
- Updated the package.json file.
- Updated the Readme.md file

CC: @samcx

---------
Co-authored-by: 's avatarsamcx <sam@vercel.com>
parent e8512b31
# Rewrites Example # Rewrites Example
This example shows how to use [rewrites in Next.js](https://nextjs.org/docs/api-reference/next.config.js/rewrites) to map an incoming request path to a different destination path. This example shows how to use [rewrites in Next.js](https://nextjs.org/docs/app/api-reference/next-config-js/rewrites) to map an incoming request path to a different destination path.
The index page ([`pages/index.js`](pages/index.js)) has a list of links that match the rewrites 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.ts)) has a list of links that match the rewrites 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;
......
"use client";
import { useState, useEffect } from "react"; import { useState, useEffect } from "react";
import { useRouter } from "next/router"; 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 About() { export default function About() {
const { asPath, route } = useRouter(); const pathname = usePathname();
const [path, setPath] = useState<string | null>(null); const [path, setPath] = useState<string | null>(null);
// `asPath` is always `/about` in Node.js (server render), because the page is statically generated useEffect(() => {
// so we wait for the browser to load, and use the updated `asPath`, which may be a path setPath(pathname);
// other than `/about` when using a rewrite. This way we can avoid a content mismatch }, [pathname]);
useEffect(() => setPath(asPath), [asPath]);
return ( return (
<div className={styles.container}> <div className={styles.container}>
...@@ -20,7 +20,7 @@ export default function About() { ...@@ -20,7 +20,7 @@ export default function About() {
<hr className={styles.hr} /> <hr className={styles.hr} />
<p> <p>
{" "} {" "}
This page was rendered by <Code>{`pages${route}.js`}</Code>. This page was rendered by <Code>{`app/about.js`}</Code>.
</p> </p>
<Link href="/">&larr; Back home</Link> <Link href="/">&larr; Back home</Link>
</div> </div>
......
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";
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 }: any) {
return ( return (
<div className={styles.container}> <div className={styles.container}>
<div className={styles.card}> <div className={styles.card}>
<h1>Path: {asPath}</h1> <h1>{`Path: pages/${params.slug.join("/")}`}</h1>
<hr className={styles.hr} /> <hr className={styles.hr} />
<p> <p>
This page was rendered by <Code>{`pages${route}.js`}</Code>. This page was rendered by <Code>{`app/news/[...slug]/page.js`}</Code>.
</p> </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>
); );
} }
// Use SSR for this page as currently rewrites don't work with dynamic pages without SSR
export async function getServerSideProps() {
return {
props: {},
};
}
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 Index() {
return ( return (
...@@ -11,12 +11,12 @@ export default function Index() { ...@@ -11,12 +11,12 @@ export default function Index() {
<p> <p>
The links below are{" "} The links below are{" "}
<Link <Link
href="https://nextjs.org/docs/api-reference/next.config.js/rewrites" href="https://nextjs.org/docs/app/api-reference/next-config-js/rewrites"
legacyBehavior legacyBehavior
> >
<> <span>
custom <Code>rewrites</Code> custom <Code>rewrites</Code>
</> </span>
</Link>{" "} </Link>{" "}
that map an incoming request path to a different destination path. that map an incoming request path to a different destination path.
</p> </p>
......
...@@ -2,4 +2,4 @@ ...@@ -2,4 +2,4 @@
/// <reference types="next/image-types/global" /> /// <reference types="next/image-types/global" />
// NOTE: This file should not be edited // NOTE: This file should not be edited
// see https://nextjs.org/docs/pages/building-your-application/configuring/typescript for more information. // see https://nextjs.org/docs/app/building-your-application/configuring/typescript for more information.
...@@ -7,13 +7,13 @@ ...@@ -7,13 +7,13 @@
}, },
"dependencies": { "dependencies": {
"next": "latest", "next": "latest",
"react": "^18.2.0", "react": "^18.3.1",
"react-dom": "^18.2.0" "react-dom": "^18.3.1"
}, },
"devDependencies": { "devDependencies": {
"@types/node": "^18.11.5", "@types/node": "^22.5.1",
"@types/react": "^18.0.23", "@types/react": "^18.3.5",
"@types/react-dom": "^18.0.7", "@types/react-dom": "^18.3.0",
"typescript": "^4.8.4" "typescript": "^5.5.4"
} }
} }
...@@ -13,8 +13,13 @@ ...@@ -13,8 +13,13 @@
"resolveJsonModule": true, "resolveJsonModule": true,
"isolatedModules": true, "isolatedModules": true,
"jsx": "preserve", "jsx": "preserve",
"incremental": true "incremental": true,
"plugins": [
{
"name": "next"
}
]
}, },
"include": ["next-env.d.ts", "**/*.ts", "**/*.tsx"], "include": ["next-env.d.ts", "**/*.ts", "**/*.tsx", ".next/types/**/*.ts"],
"exclude": ["node_modules"] "exclude": ["node_modules"]
} }
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