-
Notifications
You must be signed in to change notification settings - Fork 1.1k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Breadcrumbs example #914
Open
goncy
wants to merge
4
commits into
main
Choose a base branch
from
breadcrumbs-example
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Breadcrumbs example #914
Changes from 2 commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Large diffs are not rendered by default.
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
{ | ||
"root": true, | ||
"extends": "next/core-web-vitals" | ||
} |
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,42 @@ | ||
# See https://help.github.com/articles/ignoring-files/ for more about ignoring files. | ||
|
||
# Dependencies | ||
/node_modules | ||
/.pnp | ||
.pnp.js | ||
|
||
# Testing | ||
/coverage | ||
|
||
# Next.js | ||
/.next/ | ||
/out/ | ||
next-env.d.ts | ||
|
||
# Production | ||
build | ||
dist | ||
|
||
# Misc | ||
.DS_Store | ||
*.pem | ||
|
||
# Debug | ||
npm-debug.log* | ||
yarn-debug.log* | ||
yarn-error.log* | ||
|
||
# Local ENV files | ||
.env.local | ||
.env.development.local | ||
.env.test.local | ||
.env.production.local | ||
|
||
# Vercel | ||
.vercel | ||
|
||
# Turborepo | ||
.turbo | ||
|
||
# typescript | ||
*.tsbuildinfo |
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,33 @@ | ||
# parallel-routes-navbar example | ||
|
||
This example shows how ------------------------- | ||
|
||
## Demo | ||
|
||
https://solutions-parallel-routes-navbar.vercel.app | ||
|
||
## How to Use | ||
|
||
You can choose from one of the following two methods to use this repository: | ||
|
||
### One-Click Deploy | ||
|
||
Deploy the example using [Vercel](https://vercel.com?utm_source=github&utm_medium=readme&utm_campaign=vercel-examples): | ||
|
||
[![Deploy with Vercel](https://vercel.com/button)](https://vercel.com/new/clone?repository-url=https://github.com/vercel/examples/tree/main/solutions/parallel-routes-navbar&project-name=parallel-routes-navbar&repository-name=parallel-routes-navbar) | ||
|
||
### Clone and Deploy | ||
|
||
Execute [`create-next-app`](https://github.com/vercel/next.js/tree/canary/packages/create-next-app) with [npm](https://docs.npmjs.com/cli/init) or [Yarn](https://yarnpkg.com/lang/en/docs/cli/create/) to bootstrap the example: | ||
|
||
```bash | ||
pnpm create next-app --example https://github.com/vercel/examples/tree/main/solutions/parallel-routes-navbar | ||
``` | ||
|
||
Next, run Next.js in development mode: | ||
|
||
```bash | ||
pnpm dev | ||
``` | ||
|
||
Deploy it to the cloud with [Vercel](https://vercel.com/new?utm_source=github&utm_medium=readme&utm_campaign=edge-middleware-eap) ([Documentation](https://nextjs.org/docs/deployment)). |
26 changes: 26 additions & 0 deletions
26
solutions/parallel-routes-navbar/app/(demo)/demo/@breadcrumb/[category]/[slug]/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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
const DATA = { | ||
'key-lime-pie': 'Key Lime Pie', | ||
'pumpkin-pie': 'Pumpkin Pie', | ||
'apple-pie': 'Apple Pie', | ||
} | ||
|
||
export default async function BreadcrumbPage({ | ||
params: { category, slug }, | ||
}: { | ||
params: { category: string; slug: string } | ||
}) { | ||
const displayName = await new Promise<string>((resolve) => | ||
setTimeout( | ||
() => resolve(DATA[slug as keyof typeof DATA] || 'Secret recipe'), | ||
1000 | ||
) | ||
) | ||
Comment on lines
+12
to
+17
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. minor nit: I would move this as a |
||
|
||
return ( | ||
<nav className="flex gap-4 text-sm opacity-80"> | ||
<span className="capitalize">{` > ${category}`}</span> | ||
<span> / </span> | ||
<span>{displayName}</span> | ||
</nav> | ||
) | ||
} |
59 changes: 59 additions & 0 deletions
59
solutions/parallel-routes-navbar/app/(demo)/demo/[category]/[slug]/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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
export default function SlugPage() { | ||
return ( | ||
<main> | ||
Lorem ipsum dolor sit amet, consectetur adipiscing elit. Morbi porttitor | ||
accumsan vestibulum. Mauris dictum hendrerit ex. Ut lacus libero, feugiat | ||
sed turpis non, vehicula eleifend ex. Nulla in augue interdum, suscipit | ||
diam eu, aliquam magna. Etiam convallis lacus mattis nulla aliquam, quis | ||
tempor ante accumsan. Aenean non libero tempus mi eleifend pulvinar id ac | ||
nulla. Nullam at nisi sed augue mattis varius ac quis purus. Vivamus | ||
mollis arcu placerat venenatis commodo. Donec et dui iaculis, tristique | ||
diam vel, commodo erat. In vel orci quis orci feugiat laoreet. Aliquam et | ||
ante lacus. Vestibulum fermentum, ex sed elementum interdum, velit risus | ||
elementum elit, nec malesuada enim dolor id ante. Nam justo velit, | ||
imperdiet sed magna quis, lacinia faucibus lectus. In ut risus | ||
ullamcorper, dignissim ligula ut, pharetra lacus. Etiam venenatis felis | ||
sit amet elit pretium mollis. Sed condimentum nec dui ut efficitur. Duis | ||
ornare lorem nec hendrerit auctor. Cras a porttitor turpis, tempor | ||
facilisis mauris. Curabitur et luctus lacus, non lacinia elit. Aliquam | ||
imperdiet rutrum leo eget fermentum. Nullam non massa fringilla tortor | ||
eleifend posuere sed sit amet nisi. Mauris aliquet maximus fermentum. | ||
Donec sit amet mauris et libero dapibus fringilla non sollicitudin nulla. | ||
Curabitur non nunc et ex faucibus facilisis. Duis id arcu ac quam | ||
tincidunt tincidunt in eget massa. Aenean ultrices eros lorem, a | ||
scelerisque mi imperdiet eget. Maecenas justo nulla, ullamcorper id ligula | ||
vitae, dictum tincidunt purus. Vestibulum pretium in neque eu fermentum. | ||
Vestibulum ante ipsum primis in faucibus orci luctus et ultrices posuere | ||
cubilia curae; Etiam elit libero, consequat ut eros a, vulputate rutrum | ||
risus. Donec euismod fringilla augue vel suscipit. Suspendisse malesuada | ||
lobortis molestie. Praesent efficitur luctus sodales. Morbi magna erat, | ||
consequat ut eleifend sit amet, varius id nunc. Nunc vehicula diam eget | ||
vestibulum efficitur. Vestibulum nec luctus urna. Aliquam laoreet odio id | ||
dui tempus, non eleifend turpis lacinia. Maecenas lacinia, enim quis | ||
hendrerit rutrum, velit est aliquam sem, eu accumsan erat erat quis enim. | ||
Aliquam dolor dolor, porta et dolor sit amet, tempus hendrerit justo. | ||
Donec dictum sed nisl ut viverra. Duis ornare magna malesuada leo aliquam | ||
posuere. Aliquam in nunc ut ipsum pellentesque vehicula. Nulla ornare | ||
scelerisque turpis, ut posuere felis. Morbi quis felis pretium urna | ||
consectetur eleifend id eu metus. Proin at vulputate nibh, quis semper mi. | ||
Nullam tincidunt maximus ex nec efficitur. Vestibulum id turpis vitae | ||
massa venenatis lacinia. Nullam posuere massa eget leo consectetur | ||
hendrerit. Donec sed augue eget ante dignissim pellentesque consectetur | ||
quis enim. Maecenas ornare vitae magna ut aliquam. Nam enim elit, commodo | ||
non vestibulum nec, convallis et nibh. Etiam facilisis, velit in venenatis | ||
tincidunt, augue tellus pellentesque ligula, ac varius massa nunc ac | ||
tellus. Curabitur porta eu nisl in placerat. Curabitur viverra et turpis | ||
sagittis porttitor. Nam molestie, nisi in aliquam venenatis, diam nibh | ||
pharetra tellus, ac tempus enim ante in erat. Fusce imperdiet urna sit | ||
amet ante varius pellentesque. Aenean cursus maximus ligula, vitae | ||
faucibus dui. Donec aliquam ipsum sed velit consequat, et egestas enim | ||
vehicula. Curabitur auctor sapien at cursus venenatis. Nunc eget convallis | ||
augue, condimentum malesuada lectus. Donec et scelerisque nulla. Morbi a | ||
convallis velit. Suspendisse leo turpis, iaculis vestibulum pulvinar eget, | ||
dapibus sit amet justo. Aliquam erat volutpat. Integer et lacus orci. | ||
Integer non libero vestibulum, convallis erat ac, euismod mauris. Etiam | ||
auctor nulla sed neque egestas condimentum. Duis sed metus malesuada, | ||
cursus massa vitae, feugiat est. | ||
</main> | ||
) | ||
} |
34 changes: 34 additions & 0 deletions
34
solutions/parallel-routes-navbar/app/(demo)/demo/layout.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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
import type { ReactNode } from 'react' | ||
|
||
import '@vercel/examples-ui/globals.css' | ||
import Link from 'next/link' | ||
|
||
export default function RootLayout({ | ||
children, | ||
breadcrumb, | ||
}: { | ||
children: ReactNode | ||
breadcrumb: ReactNode | ||
}) { | ||
return ( | ||
<html lang="en"> | ||
<body className="container max-w-screen-sm m-auto grid gap-4 p-4"> | ||
<header> | ||
<ul className="flex gap-4 font-medium underline"> | ||
<li> | ||
<Link href="/demo/pastries/key-lime-pie">Key Lime Pie</Link> | ||
</li> | ||
<li> | ||
<Link href="/demo/cakes/pumpkin-pie">Pumpkin Pie</Link> | ||
</li> | ||
<li> | ||
<Link href="/demo/cakes/apple-pie">Apple Pie</Link> | ||
</li> | ||
</ul> | ||
{breadcrumb} | ||
</header> | ||
<main>{children}</main> | ||
</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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
import type { ReactNode } from 'react' | ||
import { Layout, getMetadata } from '@vercel/examples-ui' | ||
|
||
import '@vercel/examples-ui/globals.css' | ||
|
||
export const metadata = getMetadata({ | ||
title: 'parallel-routes-navbar', | ||
description: 'parallel-routes-navbar', | ||
}) | ||
|
||
export default function RootLayout({ children }: { children: ReactNode }) { | ||
return ( | ||
<html lang="en"> | ||
<body> | ||
<Layout path="solutions/parallel-routes-navbar">{children}</Layout> | ||
</body> | ||
</html> | ||
) | ||
} |
127 changes: 127 additions & 0 deletions
127
solutions/parallel-routes-navbar/app/(example)/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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,127 @@ | ||
import { Page, Text, Code, Link, Snippet } from '@vercel/examples-ui' | ||
|
||
export default function Home() { | ||
return ( | ||
<Page className="flex flex-col gap-12"> | ||
<section className="flex flex-col gap-6"> | ||
<Text variant="h1"> | ||
Using parallel routes to display route related information in layouts | ||
</Text> | ||
<Text> | ||
Breadcrumbs can be found in e-commerce, blogs, documentation, and many | ||
other types of websites. They are a great way to help users understand | ||
where they are in the website hierarchy and navigate back to previous | ||
pages. If one should say where they should be placed at code level, | ||
it's probably in the layout shared between all the pages that has | ||
it. But sometimes it's not that simple. To show information about | ||
the current route, you might need information from the url, like the | ||
params, which is not available in layouts. | ||
</Text> | ||
<Text> | ||
A solution for this might be create the breadcrumbs component as a | ||
client component, use a hook to get the params and then display it. | ||
But, what happen if the parameter in the url is a slug, and we have to | ||
first get the display value for it from a service before showing it on | ||
the breadcrumb? Because it's a client component we have to waiy | ||
for the server to send the response, for the javascript to download | ||
and execute, for the call to the slug service to complete and then | ||
finally display that breadcrumb to the user. | ||
</Text> | ||
<Text> | ||
Wow, that sounds like a lot of work right? Well, or we can use a | ||
parallel route 👇 | ||
</Text> | ||
</section> | ||
|
||
<section className="flex flex-col gap-3"> | ||
<Text variant="h2">Pages as components</Text> | ||
<Text> | ||
You can image parallel routes as slots in your page. Your layout | ||
receives a prop <Code>children</Code> that has the content of the | ||
route to display. With parallel routes you can have more of this slots | ||
where you decide what to display in each one. And, like pages, they | ||
receive the same props, including the params. | ||
</Text> | ||
</section> | ||
|
||
<section className="flex flex-col gap-3"> | ||
<Text variant="h2">Let's do it</Text> | ||
<Text> | ||
You were given a task to update a bakery blog project. You have to | ||
show a breadcrumb with the category and its icon and the post title on | ||
it. | ||
</Text> | ||
<Text> | ||
You can't touch the <Code>page</Code> file because it's | ||
being handled by a CMS so you can only change the layout or add new | ||
files to add the breadcrumb. Let's start by creating a parallel | ||
route for our breadcrumb: | ||
</Text> | ||
<Snippet> | ||
{`|/app | ||
|__/[category] | ||
|____/[slug] | ||
|______/page.js | ||
|__/layout.js | ||
|__/@breadcrumb | ||
|____/[category] | ||
|______/[slug] | ||
|________/page.js`} | ||
</Snippet> | ||
<Text> | ||
Now we have a parallel route called <Code>breadcrumb</Code> that will | ||
match when someone requests the <Code>/category/slug</Code> page. | ||
Let's add the parallel route to our layout. | ||
</Text> | ||
<Snippet> | ||
{` | ||
export default function RootLayout({ children, breadcrumb }) { | ||
return ( | ||
<html lang="en"> | ||
<body> | ||
<header> | ||
<Navbar /> | ||
{breadcrumb} | ||
</header> | ||
<main> | ||
{children} | ||
</main> | ||
</body> | ||
</html> | ||
) | ||
} | ||
`} | ||
</Snippet> | ||
<Text> | ||
In our <Code>/@breadcrumb/[category]/[slug]/page.js</Code> let's | ||
get category and slug, fetch the display name and display them on | ||
screen. | ||
</Text> | ||
<Snippet> | ||
{`export default async function BreadcrumbPage({ params: { category, slug } }) { | ||
const displayName = await fetch(\`.../\${category}/\${slug}\`); | ||
|
||
return ( | ||
<nav className="flex gap-4 text-sm opacity-80"> | ||
<span className="capitalize">{\` > \${category}\`}</span> | ||
<span> / </span> | ||
<span>{displayName}</span> | ||
</nav> | ||
); | ||
} | ||
`} | ||
</Snippet> | ||
<Text> | ||
Now, Next.js will send the response as soon as it's ready on the | ||
server. Also, as this is a page, we can define a{' '} | ||
<Code>loading.js</Code> to display a skeleton while we fetch the | ||
display name. You can find a working example in the{' '} | ||
<Link href="/demo/pastries/key-lime-pie"> | ||
<Code>/demo</Code> | ||
</Link>{' '} | ||
route. | ||
</Text> | ||
</section> | ||
</Page> | ||
) | ||
} |
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,29 @@ | ||
{ | ||
"name": "parallel-routes-navbar", | ||
"repository": "https://github.com/vercel/examples.git", | ||
"license": "MIT", | ||
"private": true, | ||
"scripts": { | ||
"dev": "next dev", | ||
"build": "next build", | ||
"start": "next start", | ||
"lint": "next lint" | ||
}, | ||
"dependencies": { | ||
"@vercel/examples-ui": "^2.0.1", | ||
"next": "^13.4.10", | ||
"react": "^18.2.0", | ||
"react-dom": "^18.2.0" | ||
}, | ||
"devDependencies": { | ||
"@types/node": "^20.4.2", | ||
"@types/react": "^18.2.15", | ||
"autoprefixer": "^10.4.14", | ||
"eslint": "^8.45.0", | ||
"eslint-config-next": "^13.4.10", | ||
"postcss": "^8.4.26", | ||
"tailwindcss": "^3.3.3", | ||
"turbo": "^1.10.8", | ||
"typescript": "^5.1.6" | ||
} | ||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
is this explicit?