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 # 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 ## 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";
import Code from "../components/Code"; import Code from "../_components/Code";
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";
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() { type NewsProps = {
const { asPath } = useRouter(); params: { slug: [] };
};
export default function News({ params }: NewsProps) {
return ( return (
<div className={styles.container}> <div className={styles.container}>
<div className={styles.card}> <div className={styles.card}>
<h1>Path: {asPath}</h1> <h1>Path: {`/news/${params.slug.join("/")}`}</h1>
<hr className={styles.hr} /> <hr className={styles.hr} />
<p> <p>
The response contains a custom header{" "} The response contains a custom header{" "}
......
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 Index() { export default function Index() {
return ( return (
...@@ -11,21 +11,21 @@ export default function Index() { ...@@ -11,21 +11,21 @@ export default function Index() {
<p> <p>
The links below are examples of{" "} The links below are examples of{" "}
<Link <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 legacyBehavior
> >
<> <span>
custom <Code>headers</Code> custom <Code>headers</Code>
</> </span>
</Link>{" "} </Link>{" "}
added to your Next.js app. added to your Next.js app.
</p> </p>
<nav> <nav>
<ul className={styles.list}> <ul className={styles.list}>
<li> <li>
<a href="/about"> <Link href="/about">
Visit /about (it contains a X-About-Custom-Header) Visit /about (it contains a X-About-Custom-Header)
</a> </Link>
</li> </li>
<li> <li>
<Link href="/news/123"> <Link href="/news/123">
......
...@@ -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": "^18.14.11",
"@types/react": "^18.0.23", "@types/react": "^18.3.3",
"@types/react-dom": "^18.0.7", "@types/react-dom": "^18.3.0",
"typescript": "^4.8.4" "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