Skip to content

Commit 3371335

Browse files
Cache Components, REad Env Values, Images, Font, Sitemap Tutorial added
1 parent b6ee1d7 commit 3371335

15 files changed

Lines changed: 102 additions & 14 deletions

File tree

NextJS/next-tutorial/.env

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
DATABASE_URL="file:./dev.db"
2+
ANALYTICS_URL="prathamesh.dhande"
3+
NEXT_PUBLIC_ANALYTICS_URL="prathamesh.ananlytics"

NextJS/next-tutorial/.gitignore

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -30,8 +30,6 @@ yarn-debug.log*
3030
yarn-error.log*
3131
.pnpm-debug.log*
3232

33-
# env files (can opt-in for committing if needed)
34-
.env*
3533

3634
# vercel
3735
.vercel

NextJS/next-tutorial/next.config.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,8 @@ import type { NextConfig } from "next";
22

33
const nextConfig: NextConfig = {
44
/* config options here */
5-
reactCompiler: true
5+
reactCompiler: true,
6+
cacheComponents: true,
67
};
78

89
export default nextConfig;

NextJS/next-tutorial/src/app/article/[articleId]/page.tsx

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,14 @@
1-
import ArticleCard from "@/components/ArticleCard";
1+
import dynamic from "next/dynamic";
22

33
// creating the type safety for the the Article Search Params
44
interface ArticleDetailsPageProps {
55
params: Promise<{ articleId: string }>;
66
searchParams: Promise<{ lang: "en" | "es" | "fr" }>;
77
}
88

9+
// Dynamic import component or lazy load the component
10+
const ArticleCard = dynamic(() => import("@/components/ArticleCard"));
11+
912
const ArticleDetailsPage = async ({
1013
params,
1114
searchParams,
Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
1-
import { Metadata } from "next";
1+
import { Metadata, Viewport } from "next";
2+
import Image from "next/image";
3+
import BlogImage from "@/assets/images.jpg";
24

35
// defining the title to override the parent title template
46
export const metadata: Metadata = {
@@ -7,11 +9,19 @@ export const metadata: Metadata = {
79
},
810
};
911

12+
// defining the viewport
13+
export const viewport: Viewport = {
14+
colorScheme: "dark",
15+
};
16+
1017
export default function Blog() {
1118
return (
1219
<div>
1320
<h1>Blog</h1>
1421
<p>Welcome to the blog page!</p>
22+
23+
{/* Using the nextjs Image Component */}
24+
<Image src={BlogImage} alt="Blog image"></Image>
1525
</div>
1626
);
1727
}
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
export default async function Page() {
2+
"use cache";
3+
4+
// Execute once, then cached for all requests
5+
const random = Math.random();
6+
const random2 = Math.random();
7+
const now = Date.now();
8+
const date = new Date();
9+
const uuid = crypto.randomUUID();
10+
const bytes = crypto.getRandomValues(new Uint8Array(16));
11+
12+
return (
13+
<div>
14+
<p>
15+
{random} and {random2}
16+
</p>
17+
<p>{now}</p>
18+
<p>{date.getTime()}</p>
19+
<p>{uuid}</p>
20+
<p>{bytes}</p>
21+
</div>
22+
);
23+
}

NextJS/next-tutorial/src/app/comments/route.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,8 @@ export async function GET(request: NextRequest) {
1212
headers: {
1313
"x-query": String(query),
1414
"X-Powered-By": "Prathamesh Dhande",
15+
// Using the Env values in the NextJS
16+
Analyticcs: String(process.env.ANALYTICS_URL),
1517
},
1618
});
1719
}

NextJS/next-tutorial/src/app/dbfetch/page.tsx

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,17 @@
11
import { deleteProduct } from "@/actions/product";
22
import { prisma } from "@/prisma.client";
3+
import { cacheLife } from "next/cache";
34
import Form from "next/form";
45
import Link from "next/link";
56

67
const DBFetch = async () => {
8+
// caching these content
9+
"use cache";
10+
cacheLife({
11+
stale: 60,
12+
revalidate: 60,
13+
expire: 60,
14+
});
715
// Accessing the DB prisma client on the server side page only
816
const products = await prisma.product.findMany({
917
orderBy: {

NextJS/next-tutorial/src/app/fetcher/page.tsx

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,8 @@ const FetcherPage = () => {
1717
});
1818
return (
1919
<div>
20+
{/* Using the Browser based ENV value */}
21+
{process.env.NEXT_PUBLIC_ANALYTICS_URL}
2022
<div>
2123
{isLoading ? (
2224
<div>Loading...</div>

NextJS/next-tutorial/src/app/layout.tsx

Lines changed: 18 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,9 @@ import "./globals.css";
33
import Footer from "@/components/Footer";
44
import Header from "@/components/Header";
55
import Provider from "@/components/Provider";
6+
import { Suspense } from "react";
7+
// Importing the font with its font name
8+
import { Edu_NSW_ACT_Cursive } from "next/font/google";
69

710
// Title metadata configuring title to be used by the child routes also
811
export const metadata: Metadata = {
@@ -15,19 +18,29 @@ export const metadata: Metadata = {
1518
"A simple NextJS tutorial to get started with NextJS 13 and the new app directory structure.",
1619
};
1720

21+
// Declaring the Font
22+
const cursivefont = Edu_NSW_ACT_Cursive({
23+
subsets: ["latin"],
24+
});
25+
1826
export default function RootLayout({
1927
children,
2028
}: Readonly<{
2129
children: React.ReactNode;
2230
}>) {
2331
return (
24-
<html lang="en">
32+
// add to include as the classname
33+
<html lang="en" className={cursivefont.className}>
2534
<body>
26-
<Header></Header>
35+
<Suspense fallback={<div>Loading...</div>}>
36+
<Header></Header>
37+
</Suspense>
2738
{/* Pass the Context Provider or any client side provider in these way */}
28-
<Provider>
29-
<main>{children}</main>
30-
</Provider>
39+
<Suspense fallback={<div>Loading....</div>}>
40+
<Provider>
41+
<main>{children}</main>
42+
</Provider>
43+
</Suspense>
3144
<Footer />
3245
</body>
3346
</html>

0 commit comments

Comments
 (0)