Unverified Commit 064a5249 authored by SamPhillemon's avatar SamPhillemon Committed by GitHub

Examples: Updated the example of headers to utilize the App Router (#67920)

This PR updates the headers 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 `/`, `/about`, and `/news/[...slug]` 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 file.

cc @samcx

---------
Co-authored-by: 's avatarsamcx <sam@vercel.com>
parent 76b9e52a
# Headers Example
This example shows how to use [headers in Next.js](https://nextjs.org/docs/api-reference/next.config.js/headers) to add custom HTTP headers into your Next.js app.
This example shows how to use [headers in Next.js](https://nextjs.org/docs/app/api-reference/next-config-js/headers) to add custom HTTP headers into your Next.js app.
The index page ([`pages/index.js`](pages/index.js)) has a list of links to pages with custom headers set up 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 to pages with custom headers set up in [`next.config.js`](next.config.js). Run or deploy the app to see how it works!
## Deploy your own
......
import styles from "../styles.module.css";
import styles from "/styles.module.css";
type CodeProps = {
children: React.ReactNode;
......
import Link from "next/link";
import styles from "../styles.module.css";
import Code from "../components/Code";
import styles from "/styles.module.css";
import Code from "../_components/Code";
export default function About() {
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";
import Link from "next/link";
import styles from "../../styles.module.css";
import Code from "../../components/Code";
import styles from "/styles.module.css";
import Code from "../../_components/Code";
export default function News() {
const { asPath } = useRouter();
type NewsProps = {
params: { slug: [] };
};
export default function News({ params }: NewsProps) {
return (
<div className={styles.container}>
<div className={styles.card}>
<h1>Path: {asPath}</h1>
<h1>Path: {`/news/${params.slug.join("/")}`}</h1>
<hr className={styles.hr} />
<p>
The response contains a custom header{" "}
......
import Link from "next/link";
import styles from "../styles.module.css";
import Code from "../components/Code";
import styles from "/styles.module.css";
import Code from "./_components/Code";
export default function Index() {
return (
......@@ -11,21 +11,21 @@ export default function Index() {
<p>
The links below are examples of{" "}
<Link
href="https://nextjs.org/docs/api-reference/next.config.js/headers"
href="https://nextjs.org/docs/app/api-reference/next-config-js/headers"
legacyBehavior
>
<>
<span>
custom <Code>headers</Code>
</>
</span>
</Link>{" "}
added to your Next.js app.
</p>
<nav>
<ul className={styles.list}>
<li>
<a href="/about">
<Link href="/about">
Visit /about (it contains a X-About-Custom-Header)
</a>
</Link>
</li>
<li>
<Link href="/news/123">
......
......@@ -7,13 +7,13 @@
},
"dependencies": {
"next": "latest",
"react": "^18.2.0",
"react-dom": "^18.2.0"
"react": "^18.3.1",
"react-dom": "^18.3.1"
},
"devDependencies": {
"@types/node": "^18.11.5",
"@types/react": "^18.0.23",
"@types/react-dom": "^18.0.7",
"typescript": "^4.8.4"
"@types/node": "^18.14.11",
"@types/react": "^18.3.3",
"@types/react-dom": "^18.3.0",
"typescript": "^5.5.3"
}
}
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