Skip to content

Commit

Permalink
Updated with-sitemap example for App Router (#66995)
Browse files Browse the repository at this point in the history
Hello

This PR updates the with-sitemap example to use:

1. App Router
2. TypeScript
3. sitemap.js

---------

Co-authored-by: Sam Ko <sam@vercel.com>
  • Loading branch information
archanaagivale30 and samcx authored Jun 20, 2024
1 parent fe0368f commit b5f97ca
Show file tree
Hide file tree
Showing 9 changed files with 80 additions and 40 deletions.
2 changes: 1 addition & 1 deletion examples/with-sitemap/README.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# With Sitemap example

This example shows how to generate a `sitemap.xml` file based on the pages in your [Next.js](https://nextjs.org/) app. The sitemap will be generated and saved in the `public` directory after starting the development server or by making a build.
This example shows how to generate a `sitemap.xml` file based on the pages in your [Next.js](https://nextjs.org/) app.

## Deploy your own

Expand Down
Original file line number Diff line number Diff line change
@@ -1,13 +1,6 @@
import Head from "next/head";

export default function Home() {
return (
<div className="container">
<Head>
<title>Create Next App</title>
<link rel="icon" href="/favicon.ico" />
</Head>

<main>
<h1 className="title">
Welcome to <a href="https://nextjs.org">Next.js!</a>
Expand All @@ -27,7 +20,7 @@ export default function Home() {
</a>
</footer>

<style jsx>{`
<style>{`
.container {
min-height: 100vh;
padding: 0 0.5rem;
Expand Down Expand Up @@ -109,7 +102,7 @@ export default function Home() {
}
`}</style>

<style jsx global>{`
<style>{`
html,
body {
padding: 0;
Expand Down
19 changes: 19 additions & 0 deletions examples/with-sitemap/app/layout.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
export const metadata = {
title: "Create Next App",
description: "Generated by Next.js",
icons: {
icon: "/favicon.ico",
},
};

export default function RootLayout({
children,
}: Readonly<{
children: React.ReactNode;
}>) {
return (
<html lang="en">
<body>{children}</body>
</html>
);
}
Original file line number Diff line number Diff line change
@@ -1,20 +1,13 @@
import Head from "next/head";

export default function Home() {
return (
<div className="container">
<Head>
<title>Create Next App</title>
<link rel="icon" href="/favicon.ico" />
</Head>

<main>
<h1 className="title">
Welcome to <a href="https://nextjs.org">Next.js!</a>
</h1>

<p className="description">
Get started by editing <code>pages/index.js</code>
Get started by editing <code>app/page.tsx</code>
</p>

<div className="grid">
Expand Down Expand Up @@ -61,7 +54,7 @@ export default function Home() {
</a>
</footer>

<style jsx>{`
<style>{`
.container {
min-height: 100vh;
padding: 0 0.5rem;
Expand Down Expand Up @@ -201,7 +194,7 @@ export default function Home() {
}
`}</style>

<style jsx global>{`
<style>{`
html,
body {
padding: 0;
Expand Down
24 changes: 24 additions & 0 deletions examples/with-sitemap/app/sitemap.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
const globby = require("globby");

function addPage(page: string) {
const path = page
.replace("app", "")
.replace(".tsx", "")
.replace(".mdx", "")
.replace("/page", "");
return path;
}
export default async function sitemap() {
const pages = await globby([
"app/**/*{.js,jsx,ts,tsx,.mdx}",
"!app/_*.js",
"!app/{sitemap,layout}.{js,jsx,ts,tsx}",
"!app/api",
]);
const routes = pages.map((page: string) => ({
url: `${process.env.WEBSITE_URL}${addPage(page)}`,
lastModified: new Date().toISOString(),
}));

return [...routes];
}
10 changes: 1 addition & 9 deletions examples/with-sitemap/next.config.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,2 @@
/** @type {import('next').NextConfig} */
module.exports = {
webpack: (config, { isServer }) => {
if (isServer) {
require("./scripts/generate-sitemap");
}

return config;
},
};
module.exports = {};
6 changes: 5 additions & 1 deletion examples/with-sitemap/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,10 @@
"react-dom": "^18.2.0"
},
"devDependencies": {
"globby": "^11.0.1"
"globby": "^11.0.1",
"@types/node": "^18.11.5",
"@types/react": "^18.0.23",
"@types/react-dom": "^18.0.7",
"typescript": "^4.8.4"
}
}
10 changes: 0 additions & 10 deletions examples/with-sitemap/public/sitemap.xml

This file was deleted.

25 changes: 25 additions & 0 deletions examples/with-sitemap/tsconfig.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
{
"compilerOptions": {
"target": "es5",
"lib": ["dom", "dom.iterable", "esnext"],
"allowJs": true,
"skipLibCheck": true,
"strict": true,
"forceConsistentCasingInFileNames": true,
"noEmit": true,
"esModuleInterop": true,
"module": "esnext",
"moduleResolution": "node",
"resolveJsonModule": true,
"isolatedModules": true,
"jsx": "preserve",
"incremental": true,
"plugins": [
{
"name": "next"
}
]
},
"include": ["next-env.d.ts", "**/*.ts", "**/*.tsx", ".next/types/**/*.ts"],
"exclude": ["node_modules"]
}

0 comments on commit b5f97ca

Please sign in to comment.