Skip to content

Commit

Permalink
Align dev server with CF Pages behavior
Browse files Browse the repository at this point in the history
  • Loading branch information
soof-golan committed Oct 23, 2024
1 parent 6ae6c40 commit 728fd7b
Show file tree
Hide file tree
Showing 4 changed files with 28 additions and 41 deletions.
12 changes: 5 additions & 7 deletions website/components/DashboardRoomList.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -38,13 +38,11 @@ export default function DashboardRoomsList() {
<div className="flex flex-col">
<CreateRoomCard />
{rooms ? (
rooms
.sort((a, b) => moment(b.updatedAt).diff(moment(a.updatedAt)))
.map((room) => (
<div key={room.id} className="flex flex-row">
<WaitingRoomDashboardCard room={room} />
</div>
))
rooms.map((room) => (
<div key={room.id} className="flex flex-row">
<WaitingRoomDashboardCard room={room} />
</div>
))
) : (
<>
<Spinner />
Expand Down
2 changes: 1 addition & 1 deletion website/components/WaitingRoomDashboardCard.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ export default function WaitingRoomDashboardCard({
initialData: { urlReady: false },
queryFn: async () => {
const roomUrl = `/room/${room.id}`;
const response = await fetch(roomUrl);
const response = await fetch(roomUrl, { method: "HEAD" });
if (!response.ok) {
throw new Error("Url not live yet");
}
Expand Down
3 changes: 3 additions & 0 deletions website/pages/room/@id/index.page.server.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,9 @@ async function prerender() {
async function onBeforeRender(pageContext: PageContextServer) {
const id = pageContext.routeParams.id;
const room = await findRoomById(id);
if (!room) {
throw render(404, "Room not found");
}
if (!room.published) {
throw render(403, "Room not published");
}
Expand Down
52 changes: 19 additions & 33 deletions website/utils/queries.ts
Original file line number Diff line number Diff line change
@@ -1,49 +1,35 @@
import moment from "moment/moment";
import { prisma } from "../server/db";

const queryCache = new Map();
const roomFilter = {
published: true,
AND: {
closesAt: {
gt: moment().subtract(1, "day").toDate(),
},
},
};

export async function findPublishedRooms() {
const rooms = await prisma.waitingRoom.findMany({
where: {
published: true,
AND: {
closesAt: {
gt: moment().subtract(1, "day").toDate(),
},
},
},
return prisma.waitingRoom.findMany({
where: roomFilter,
include: {
owner: true,
},
orderBy: {
closesAt: "asc",
},
});

rooms.forEach((room) => {
queryCache.set(room.id, room);
});

return rooms;
}

export async function findRoomById(id: string) {
async function getRoomById() {
return prisma.waitingRoom.findUniqueOrThrow({
where: {
id,
},
include: {
owner: true,
},
});
}

if (queryCache.has(id)) {
return queryCache.get(id) as Awaited<ReturnType<typeof getRoomById>>;
}
const room = await getRoomById();
queryCache.set(room.id, room);
return room;
return prisma.waitingRoom.findFirst({
where: {
id,
...roomFilter,
},
include: {
owner: true,
},
});
}

0 comments on commit 728fd7b

Please sign in to comment.