-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
3ffe7e4
commit 053f9ec
Showing
10 changed files
with
142 additions
and
10 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
export const numLatest = 5; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
import { MongoClient } from "mongodb"; | ||
import "dotenv/config"; | ||
import { revalidateTag, unstable_cache } from "next/cache"; | ||
import { NextRequest, NextResponse } from "next/server"; | ||
import { numLatest } from "./const"; | ||
|
||
async function getLatestImpl() { | ||
console.log("getLatestImpl"); | ||
const client = new MongoClient(process.env.MONGODB_URI!); | ||
try { | ||
await client.connect(); | ||
const db = client.db("nikochan"); | ||
return await db | ||
.collection("chart") | ||
.find({ published: true }) | ||
.sort({ updatedAt: -1 }) | ||
.limit(numLatest) | ||
.project({ _id: 0, cid: 1 }) | ||
.toArray(); | ||
} finally { | ||
await client.close(); | ||
} | ||
} | ||
export function revalidateLatest() { | ||
console.warn(`revalidate latest`); | ||
revalidateTag(`latest`); | ||
} | ||
|
||
export async function getLatest() { | ||
const getLatestCache = unstable_cache(getLatestImpl, [], { | ||
tags: ["latest"], | ||
revalidate: false, | ||
}); | ||
return await getLatestCache(); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
import { NextRequest, NextResponse } from "next/server"; | ||
import { getLatest } from "./latest"; | ||
|
||
export async function GET(request: NextRequest) { | ||
const latest = await getLatest(); | ||
console.log(latest) | ||
return NextResponse.json(latest); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters