Next.js Discord

Discord Forum

How can I get the current page URL in the Server Component?

Unanswered
Sloth bear posted this in #help-forum
Open in Discord
Sloth bearOP
Here's my Nextjs code, and wanted to get the current page url. How can I get?
import React from "react";
import TabSwitch from "@/components/dashboard/tab-switch";
import { getCreditsByTeamId } from "@/data/team";
import { Badge } from "@/components/ui/badge";

const DashboardLayout = async ({
  children,
  params,
}: {
  children: React.ReactNode;
  params: { id: string };
}) => {
  const { id: teamId } = params;
  const remainingCredits = await getCreditsByTeamId(teamId);
  return (
    <div>
      <div className="flex flex-row-reverse place-items-center justify-between">
        {remainingCredits && (
          <Badge
            variant={remainingCredits.credits >= 20 ? "warning" : "destructive"}
          >
            {remainingCredits.credits >= 20
              ? remainingCredits.credits + " credits remaining"
              : "Insuffecient Credits"}
          </Badge>
        )}
        <TabSwitch params={params} />
      </div>
      <main className="m-2 mx-auto max-w-screen-lg p-4">{children}</main>
    </div>
  );
};

export default DashboardLayout;

2 Replies

Transvaal lion
if you're using server components and the app dir, I believe the docs explicitly call out that by design it has to be in a client component: https://nextjs.org/docs/app/building-your-application/upgrading/app-router-migration#step-5-migrating-routing-hooks
Sloth bearOP
👍