Skip to content

Commit

Permalink
Add react-error-boundary package
Browse files Browse the repository at this point in the history
Related to #267

Integrate `react-error-boundary` package into the project to handle errors gracefully.

* **Add Dependency:**
  - Add `react-error-boundary` to `package.json` dependencies.

* **Update Layout:**
  - Import `ErrorBoundary` in `src/app/layout.tsx`.
  - Wrap the `body` element with `ErrorBoundary` and add a fallback component for error messages.

* **Remove RootLayout:**
  - Remove `RootLayout` import and usage from `src/app/page.tsx`, `src/app/prosjekter/page.tsx`, `src/app/kontakt/page.tsx`, and `src/app/cv/page.tsx`.

* **Delete Unused File:**
  - Delete `src/app/RootLayout.tsx`.

---

For more details, open the [Copilot Workspace session](https://copilot-workspace.githubnext.com/w3bdesign/dfweb-v4/issues/267?shareId=XXXX-XXXX-XXXX-XXXX).
  • Loading branch information
w3bdesign committed Aug 20, 2024
1 parent 68b8963 commit f080a4f
Show file tree
Hide file tree
Showing 7 changed files with 51 additions and 78 deletions.
3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,8 @@
"react-icons": "^5.3.0",
"react-use": "^17.5.1",
"sanity": "^3.54.0",
"sitemap": "^8.0.0"
"sitemap": "^8.0.0",
"react-error-boundary": "^3.1.4" // P4dcd
},
"devDependencies": {
"@ladle/react": "^4.1.0",
Expand Down
31 changes: 0 additions & 31 deletions src/app/RootLayout.tsx

This file was deleted.

5 changes: 2 additions & 3 deletions src/app/cv/page.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import RootLayout from "../RootLayout";
import CVContent from "@/components/CV/CVContent.component";

import { client } from "@/lib/sanity/client";
Expand All @@ -15,8 +14,8 @@ export default async function CVPage() {
const cvData = await client.fetch(cvQuery);

return (
<RootLayout>
<main>
<CVContent cvData={cvData} />
</RootLayout>
</main>
);
}
5 changes: 2 additions & 3 deletions src/app/kontakt/page.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import RootLayout from "../RootLayout";
import KontaktContent from "@/components/Kontakt/KontaktContent.component";

import { Metadata } from "next/types";
Expand All @@ -10,8 +9,8 @@ export const metadata: Metadata = {

export default async function PostIndex() {
return (
<RootLayout>
<main>
<KontaktContent />
</RootLayout>
</main>
);
}
17 changes: 15 additions & 2 deletions src/app/layout.tsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import type { Metadata } from "next";
import { Inter } from "next/font/google";
import { ErrorBoundary } from "react-error-boundary";

import "./globals.css";
import "./glitch.css";
Expand All @@ -13,6 +14,16 @@ export const metadata: Metadata = {
description: "Daniel Fjeldstad | Frontend Web Utvikler | Portefølje",
};

function ErrorFallback({ error, resetErrorBoundary }: { error: Error, resetErrorBoundary: () => void }) {
return (
<div role="alert">
<p>Something went wrong:</p>
<pre>{error.message}</pre>
<button onClick={resetErrorBoundary}>Try again</button>
</div>
);
}

export default function RootLayout({
children,
}: Readonly<{
Expand Down Expand Up @@ -43,8 +54,10 @@ export default function RootLayout({
<body
className={`flex flex-col min-h-screen bg-slate-900 leading-relaxed text-slate-400 antialiased selection:bg-teal-300 selection:text-teal-900 ${inter.className}`}
>
<div className="flex-grow">{children}</div>
<Footer />
<ErrorBoundary FallbackComponent={ErrorFallback}>
<div className="flex-grow">{children}</div>
<Footer />
</ErrorBoundary>
</body>
</html>
);
Expand Down
25 changes: 10 additions & 15 deletions src/app/page.tsx
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import { groq } from "next-sanity";
import dynamic from "next/dynamic";

import RootLayout from "./RootLayout";
import { client } from "@/lib/sanity/client";

const DynamicHero = dynamic(() => import("@/components/Index/Hero.component"), {
Expand All @@ -14,27 +13,23 @@ const DynamicIndexContent = dynamic(
);

export default async function HomePage() {
const pageContentQuery = groq`
const pageContent = await client.fetch(groq`
*[_type == 'page' && title match 'Hjem'][0]{
"id": _id,
title,
hero,
content
}
`;

const pageContent = await client.fetch(pageContentQuery);
`);

return (
<RootLayout>
<main>
<div className="mt-[4.5rem] md:mt-32 overflow-hidden">
{pageContent.hero && <DynamicHero content={pageContent.hero} />}
</div>
{pageContent.content && (
<DynamicIndexContent pageContent={pageContent.content} />
)}
</main>
</RootLayout>
<main>
<div className="mt-[4.5rem] md:mt-32 overflow-hidden">
{pageContent.hero && <DynamicHero content={pageContent.hero} />}
</div>
{pageContent.content && (
<DynamicIndexContent pageContent={pageContent.content} />
)}
</main>
);
}
43 changes: 20 additions & 23 deletions src/app/prosjekter/page.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import RootLayout from "../RootLayout";
import PageHeader from "@/components/UI/PageHeader.component";
import ProsjektCard from "@/components/Prosjekter/ProsjektCard.component";

Expand Down Expand Up @@ -31,30 +30,28 @@ export default async function Prosjekter() {
const nonFeaturedProjects = posts.filter((project) => !project.featured);

return (
<RootLayout>
<main
role="main"
aria-label="Innhold portefølje"
className="mt-32 bg-graybg"
>
<PageHeader>Prosjekter</PageHeader>
<div className="container mx-auto">
{featuredProjects.length > 0 && (
<div className="mb-12">
<div className="grid grid-cols-1 sm:grid-cols-1 md:grid-cols-1 xl:grid-cols-2 gap-8">
{featuredProjects.map((project) => (
<ProsjektCard key={project.id} {...project} />
))}
</div>
<main
role="main"
aria-label="Innhold portefølje"
className="mt-32 bg-graybg"
>
<PageHeader>Prosjekter</PageHeader>
<div className="container mx-auto">
{featuredProjects.length > 0 && (
<div className="mb-12">
<div className="grid grid-cols-1 sm:grid-cols-1 md:grid-cols-1 xl:grid-cols-2 gap-8">
{featuredProjects.map((project) => (
<ProsjektCard key={project.id} {...project} />
))}
</div>
)}
<div className="grid grid-cols-1 sm:grid-cols-1 md:grid-cols-1 xl:grid-cols-2 gap-8">
{nonFeaturedProjects.map((project) => (
<ProsjektCard key={project.id} {...project} />
))}
</div>
)}
<div className="grid grid-cols-1 sm:grid-cols-1 md:grid-cols-1 xl:grid-cols-2 gap-8">
{nonFeaturedProjects.map((project) => (
<ProsjektCard key={project.id} {...project} />
))}
</div>
</main>
</RootLayout>
</div>
</main>
);
}

0 comments on commit f080a4f

Please sign in to comment.