Skip to content

Commit

Permalink
Update GET route to return JSON response with headers & fixed linksLi…
Browse files Browse the repository at this point in the history
…mit on dashboard
  • Loading branch information
pheralb committed Mar 30, 2024
1 parent 0932ce3 commit 4922551
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 3 deletions.
9 changes: 7 additions & 2 deletions src/app/api/url/route.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,10 @@ export const GET = async (req: NextRequest) => {

// If no link found (404):
if (!getLinkFromServer) {
return NextResponse.redirect(new URL(`/dashboard?search=${params}`, req.url));
return NextResponse.json(
{ error: "Error: Slug not found or invalid." },
{ status: 404 },
);
}

// Increment the clicks in the database:
Expand All @@ -43,5 +46,7 @@ export const GET = async (req: NextRequest) => {
newHeaders.set("cache-control", "public, max-age=31536000, immutable");

// Redirect to the URL:
return NextResponse.redirect(new URL(getLinkFromServer.url).toString());
return NextResponse.json(getLinkFromServer, {
headers: newHeaders,
});
};
2 changes: 1 addition & 1 deletion src/app/dashboard/page.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ const DashboardPage = async ({
<main className="w-full duration-500 animate-in fade-in-5 slide-in-from-bottom-2">
<div className="mb-2 flex w-full flex-col items-end justify-between space-y-4 md:flex-row md:items-center md:space-y-0">
<SearchLinks className="w-full md:w-72 md:max-w-72" />
<LinksLimit userLinks={filteredLinks.length} maxLinks={data.limit} />
<LinksLimit userLinks={data.links.length} maxLinks={data.limit} />
</div>
<div className="grid grid-cols-1 gap-2 md:grid-cols-1 lg:grid-cols-2">
{filteredLinks
Expand Down

0 comments on commit 4922551

Please sign in to comment.