From cc3cef06d5414fc3f06b0354b25320b99f405c6a Mon Sep 17 00:00:00 2001 From: Ross Date: Thu, 25 May 2023 14:40:38 +0300 Subject: [PATCH 1/3] Add imports for User model to routes where needed --- app/api/prompt/[id]/route.js | 2 ++ app/api/prompt/route.js | 2 ++ app/api/users/[id]/posts/route.js | 2 ++ 3 files changed, 6 insertions(+) diff --git a/app/api/prompt/[id]/route.js b/app/api/prompt/[id]/route.js index a8837a4b..5cc545af 100644 --- a/app/api/prompt/[id]/route.js +++ b/app/api/prompt/[id]/route.js @@ -1,4 +1,6 @@ import Prompt from "@models/prompt"; +import User from "@models/user"; + import { connectToDB } from "@utils/database"; export const GET = async (request, { params }) => { diff --git a/app/api/prompt/route.js b/app/api/prompt/route.js index 751cd1f0..f8892548 100644 --- a/app/api/prompt/route.js +++ b/app/api/prompt/route.js @@ -1,4 +1,6 @@ import Prompt from "@models/prompt"; +import User from "@models/user"; + import { connectToDB } from "@utils/database"; export const GET = async (request) => { diff --git a/app/api/users/[id]/posts/route.js b/app/api/users/[id]/posts/route.js index 0c275977..124d3d7d 100644 --- a/app/api/users/[id]/posts/route.js +++ b/app/api/users/[id]/posts/route.js @@ -1,4 +1,6 @@ import Prompt from "@models/prompt"; +import User from "@models/user"; + import { connectToDB } from "@utils/database"; export const GET = async (request, { params }) => { From 81fd06338fa8dce9939754f018babc45b7a6cc43 Mon Sep 17 00:00:00 2001 From: Ross Date: Wed, 31 May 2023 22:44:46 +0300 Subject: [PATCH 2/3] Add a unique identifier to the URL to force a cache-busting reload --- app/api/prompt/route.js | 23 ++++++++++++++++++----- 1 file changed, 18 insertions(+), 5 deletions(-) diff --git a/app/api/prompt/route.js b/app/api/prompt/route.js index f8892548..febf3f5d 100644 --- a/app/api/prompt/route.js +++ b/app/api/prompt/route.js @@ -5,11 +5,24 @@ import { connectToDB } from "@utils/database"; export const GET = async (request) => { try { - await connectToDB() - - const prompts = await Prompt.find({}).populate('creator') - - return new Response(JSON.stringify(prompts), { status: 200 }) + await connectToDB(); + const prompts = await Prompt.find().populate({ + path: "creator" + }); + + const response = new Response(JSON.stringify(prompts), { + status: 200, + }); + + // Add a unique identifier to the URL to force a cache-busting reload + const url = new URL(req.url); + url.searchParams.set("t", Date.now()); + response.headers.set("Cache-Control", "no-cache, no-store, must-revalidate"); + response.headers.set("Pragma", "no-cache"); + response.headers.set("Expires", "0"); + response.headers.set("Location", url.toString()); + + return response; } catch (error) { return new Response("Failed to fetch all prompts", { status: 500 }) } From 040d160c69c28c214af9fd724664b16fd99f7b39 Mon Sep 17 00:00:00 2001 From: Ross <46504720+uns4inted@users.noreply.github.com> Date: Wed, 31 May 2023 23:04:38 +0300 Subject: [PATCH 3/3] Fix typo Minor fix: typo in API route file --- app/api/prompt/route.js | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/app/api/prompt/route.js b/app/api/prompt/route.js index febf3f5d..87a42da5 100644 --- a/app/api/prompt/route.js +++ b/app/api/prompt/route.js @@ -15,7 +15,7 @@ export const GET = async (request) => { }); // Add a unique identifier to the URL to force a cache-busting reload - const url = new URL(req.url); + const url = new URL(request.url); url.searchParams.set("t", Date.now()); response.headers.set("Cache-Control", "no-cache, no-store, must-revalidate"); response.headers.set("Pragma", "no-cache"); @@ -26,4 +26,4 @@ export const GET = async (request) => { } catch (error) { return new Response("Failed to fetch all prompts", { status: 500 }) } -} \ No newline at end of file +}