forked from vercel/next.js
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[Multi Zones] Update the with-zones example to use App Router (vercel…
…#67636) This pull request updates the `with-zones` example to demonstrate how to use zones with App Router. --------- Co-authored-by: Delba de Oliveira <[email protected]>
- Loading branch information
1 parent
9c55b45
commit 70a023b
Showing
16 changed files
with
170 additions
and
35 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
export const metadata = { | ||
title: "Next.js - Blog Zone", | ||
description: "Next.js example for Multi Zones", | ||
}; | ||
|
||
export default function RootLayout({ | ||
children, | ||
}: { | ||
children: React.ReactNode; | ||
}) { | ||
return ( | ||
<html lang="en"> | ||
<body>{children}</body> | ||
</html> | ||
); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
import Link from "next/link"; | ||
|
||
export default function Post({ params: { id } }) { | ||
return ( | ||
<div> | ||
<h3>Post #{id}</h3> | ||
<p>Lorem ipsum</p> | ||
<Link href="/blog">Back to blog</Link> | ||
</div> | ||
); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,14 @@ | ||
/** @type {import('next').NextConfig} */ | ||
const nextConfig = { | ||
basePath: "/blog", | ||
experimental: { | ||
// Next.js will automatically prefix `basePath` to client side links which | ||
// is useful when all links are relative to the `basePath` of this | ||
// application. This option opts out of that behavior, which can be useful | ||
// if you want to link outside of your zone, such as linking to | ||
// "/" from "/blog" (the `basePath` for this application). | ||
manualClientBasePath: true, | ||
}, | ||
}; | ||
|
||
module.exports = nextConfig; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,5 @@ | ||
{ | ||
"name": "blog", | ||
"private": true, | ||
"scripts": { | ||
"dev": "next dev -p 4000", | ||
|
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
export const metadata = { | ||
title: "Next.js - Home Zone", | ||
description: "Next.js example for Multi Zones", | ||
}; | ||
|
||
export default function RootLayout({ | ||
children, | ||
}: { | ||
children: React.ReactNode; | ||
}) { | ||
return ( | ||
<html lang="en"> | ||
<body>{children}</body> | ||
</html> | ||
); | ||
} |
4 changes: 1 addition & 3 deletions
4
examples/with-zones/home/pages/index.tsx → examples/with-zones/home/app/page.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
const nextJest = require("next/jest"); | ||
|
||
const createJestConfig = nextJest({ | ||
// Provide the path to your Next.js app to load next.config.js and .env files in your test environment | ||
dir: "./", | ||
}); | ||
|
||
// Add any custom config to be passed to Jest | ||
const customJestConfig = { | ||
testEnvironment: "jsdom", | ||
testMatch: ["./**/*.test.{ts,tsx}"], | ||
}; | ||
|
||
// createJestConfig is exported this way to ensure that next/jest can load the Next.js config which is async | ||
module.exports = createJestConfig(customJestConfig); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,19 +1,24 @@ | ||
{ | ||
"name": "home", | ||
"private": true, | ||
"scripts": { | ||
"dev": "next dev", | ||
"build": "next build", | ||
"start": "next start" | ||
"start": "next start", | ||
"test": "jest --watch" | ||
}, | ||
"dependencies": { | ||
"next": "latest", | ||
"react": "^18.2.0", | ||
"react-dom": "^18.2.0" | ||
}, | ||
"devDependencies": { | ||
"@types/jest": "29.5.11", | ||
"@types/node": "^18.11.9", | ||
"@types/react": "^18.0.25", | ||
"@types/react-dom": "^18.0.9", | ||
"jest": "29.7.0", | ||
"path-to-regexp": "6.2.1", | ||
"typescript": "^4.9.3" | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,64 @@ | ||
/** | ||
* Test file for the rewrites in next.config.js. This is useful to test the logic for rewriting | ||
* paths to multi zones to make sure that the logic is correct before deploying the application. | ||
*/ | ||
|
||
import { type MatchResult, compile, match } from "path-to-regexp"; | ||
import nextConfig from "../next.config.js"; | ||
|
||
function getDestination(destination: string, pathMatch: MatchResult): string { | ||
const hasDifferentHost = destination.startsWith("https://"); | ||
if (hasDifferentHost) { | ||
const destinationUrl = new URL(destination); | ||
destinationUrl.pathname = compile(destinationUrl.pathname, { | ||
encode: encodeURIComponent, | ||
})(pathMatch.params); | ||
return destinationUrl.toString(); | ||
} | ||
return compile(destination, { | ||
encode: encodeURIComponent, | ||
})(pathMatch.params); | ||
} | ||
|
||
const BLOG_URL = "https://with-zones-blog.vercel.app"; | ||
|
||
describe("next.config.js test", () => { | ||
describe("rewrites", () => { | ||
let rewrites: Awaited<ReturnType<NonNullable<typeof nextConfig.rewrites>>>; | ||
|
||
beforeAll(async () => { | ||
process.env.BLOG_URL = BLOG_URL; | ||
rewrites = await nextConfig.rewrites!(); | ||
}); | ||
|
||
function getRewrittenUrl(path: string): string | undefined { | ||
const allRewrites = | ||
"beforeFiles" in rewrites | ||
? [...rewrites.beforeFiles, ...rewrites.afterFiles] | ||
: rewrites; | ||
for (const rewrite of allRewrites) { | ||
if (rewrite.has?.length) { | ||
continue; | ||
} | ||
const rewriteMatch = match(rewrite.source)(path); | ||
if (rewriteMatch) { | ||
return getDestination(rewrite.destination, rewriteMatch); | ||
} | ||
} | ||
return undefined; | ||
} | ||
|
||
it("non blog pages are not rewritten", () => { | ||
expect(getRewrittenUrl("/")).toEqual(undefined); | ||
expect(getRewrittenUrl("/blog-not")).toEqual(undefined); | ||
expect(getRewrittenUrl("/blog2")).toEqual(undefined); | ||
}); | ||
|
||
it("/blog is rewritten to child zone", () => { | ||
expect(getRewrittenUrl("/blog")).toEqual(`${BLOG_URL}/blog`); | ||
expect(getRewrittenUrl("/blog/post/1")).toEqual( | ||
`${BLOG_URL}/blog/post/1`, | ||
); | ||
}); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters