Guest User

Untitled

a guest
May 17th, 2025
7
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.14 KB | None | 0 0
  1.  
  2. ```ts
  3. import React from 'react';
  4. import { notFound } from 'next/navigation';
  5.  
  6. // 1️⃣ Import your actual page components
  7. import ContactPage from '@/app/contact/page';
  8. import AboutPage from '@/app/a-propos/page';
  9. import TeamPage from '@/app/a-propos/equipe/page';
  10.  
  11. // 2️⃣ Define the routing tree
  12. type RouteNode = {
  13. component?: React.FC;
  14. children?: Record<string, RouteNode>;
  15. };
  16.  
  17. const routeTree: Record<string, RouteNode> = {
  18. 'contact': { component: ContactPage },
  19. 'a-propos': {
  20. component: AboutPage,
  21. children: {
  22. 'equipe': { component: TeamPage },
  23. },
  24. },
  25. };
  26.  
  27. export default function CatchAllPage({
  28. params,
  29. }: {
  30. params: Promise<{ segments?: string[] }>;
  31. }) {
  32. const resolvedParams = await params;
  33. const segments = resolvedParams.segments ?? [];
  34. let node: RouteNode | undefined;
  35. let Comp: React.FC | undefined;
  36.  
  37. for (let i = 0; i < segments.length; i++) {
  38. const rawSeg = segments[i];
  39. const seg = decodeURIComponent(rawSeg);
  40. node = i === 0 ? routeTree[seg] : node?.children?.[seg];
  41. if (!node) {
  42. return notFound();
  43. }
  44. if (i === segments.length - 1) {
  45. Comp = node.component;
  46. }
  47. }
  48.  
  49. if (!Comp) {
  50. return notFound();
  51. }
  52. return <Comp />;
  53. }
  54.  
  55. export async function generateStaticParams() {
  56. return [
  57. { segments: ['contact'] },
  58. { segments: ['a-propos', 'equipe'] },
  59. ];
  60. }
  61.  
  62.  
  63. export async function generateMetadata({
  64. params,
  65. }: {
  66. params: Promise<{ segments?: string[] }>;
  67. }) {
  68. const resolvedParams = await params;
  69. const path = (resolvedParams.segments ?? []).join('/');
  70.  
  71. switch (path) {
  72. case 'contact':
  73. return {
  74. title: 'Contact – Mon Site',
  75. description: 'Contactez-nous pour toute question ou demande d’information.',
  76. alternates: { canonical: '/contact' },
  77. };
  78. case 'a-propos/equipe':
  79. return {
  80. title: 'Notre Équipe – À Propos',
  81. description: 'Découvrez les membres de notre équipe et leurs expertises.',
  82. alternates: { canonical: '/a-propos/equipe' },
  83. };
  84. default:
  85. return {};
  86. }
  87. }
  88. ```
Advertisement
Add Comment
Please, Sign In to add comment