mirror of
https://github.com/rzmk/learnhouse.git
synced 2025-12-19 04:19:25 +00:00
feat: use Next.js on demand revalidation
This commit is contained in:
parent
86e4ec64b5
commit
cc6cc52ba5
7 changed files with 39 additions and 10 deletions
19
front/app/api/revalidate/route.ts
Normal file
19
front/app/api/revalidate/route.ts
Normal file
|
|
@ -0,0 +1,19 @@
|
||||||
|
import { NextRequest, NextResponse } from "next/server";
|
||||||
|
import { revalidateTag } from "next/cache";
|
||||||
|
|
||||||
|
export async function GET(request: NextRequest) {
|
||||||
|
const tag: any = request.nextUrl.searchParams.get("tag");
|
||||||
|
revalidateTag(tag);
|
||||||
|
|
||||||
|
return NextResponse.json(
|
||||||
|
{ revalidated: true, now: Date.now() },
|
||||||
|
{
|
||||||
|
status: 200,
|
||||||
|
headers: {
|
||||||
|
"Access-Control-Allow-Origin": "*",
|
||||||
|
"Access-Control-Allow-Methods": "GET, POST, PUT, DELETE, OPTIONS",
|
||||||
|
"Access-Control-Allow-Headers": "Content-Type, Authorization",
|
||||||
|
},
|
||||||
|
}
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
@ -10,6 +10,7 @@ import Link from 'next/link';
|
||||||
import React from 'react'
|
import React from 'react'
|
||||||
import Image from 'next/image';
|
import Image from 'next/image';
|
||||||
import { AuthContext } from '@components/Security/AuthProvider';
|
import { AuthContext } from '@components/Security/AuthProvider';
|
||||||
|
import { revalidateTags } from '@services/utils/ts/requests';
|
||||||
|
|
||||||
interface CourseProps {
|
interface CourseProps {
|
||||||
orgslug: string;
|
orgslug: string;
|
||||||
|
|
@ -28,13 +29,14 @@ function Courses(props: CourseProps) {
|
||||||
|
|
||||||
async function deleteCourses(course_id: any) {
|
async function deleteCourses(course_id: any) {
|
||||||
await deleteCourseFromBackend(course_id);
|
await deleteCourseFromBackend(course_id);
|
||||||
|
revalidateTags(['courses']);
|
||||||
}
|
}
|
||||||
|
|
||||||
async function closeNewCourseModal() {
|
async function closeNewCourseModal() {
|
||||||
setNewCourseModal(false);
|
setNewCourseModal(false);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
return (
|
return (
|
||||||
|
|
@ -103,7 +105,7 @@ const AdminEditsArea = (props: any) => {
|
||||||
|
|
||||||
// this is amazingly terrible code, but gotta release that MVP
|
// this is amazingly terrible code, but gotta release that MVP
|
||||||
// TODO: fix this
|
// TODO: fix this
|
||||||
|
|
||||||
function isAuthorized() {
|
function isAuthorized() {
|
||||||
const org_id = props.course.org_id;
|
const org_id = props.course.org_id;
|
||||||
const org_roles = auth.userInfo.user_object.orgs;
|
const org_roles = auth.userInfo.user_object.orgs;
|
||||||
|
|
|
||||||
|
|
@ -11,11 +11,11 @@ export const metadata: Metadata = {
|
||||||
|
|
||||||
const CoursesPage = async (params: any) => {
|
const CoursesPage = async (params: any) => {
|
||||||
const orgslug = params.params.orgslug;
|
const orgslug = params.params.orgslug;
|
||||||
const courses = await getOrgCourses(orgslug, { revalidate: 360 });
|
const courses = await getOrgCourses(orgslug, { revalidate: 360, tags: ['courses'] });
|
||||||
|
|
||||||
return (
|
return (
|
||||||
<div>
|
<div>
|
||||||
<Courses orgslug={orgslug} courses={courses}/>
|
<Courses orgslug={orgslug} courses={courses} />
|
||||||
</div>
|
</div>
|
||||||
);
|
);
|
||||||
};
|
};
|
||||||
|
|
|
||||||
|
|
@ -22,7 +22,7 @@ export async function generateMetadata(
|
||||||
): Promise<Metadata> {
|
): Promise<Metadata> {
|
||||||
|
|
||||||
// Get Org context information
|
// Get Org context information
|
||||||
const org = await getOrganizationContextInfo(params.orgslug, { revalidate: 1800 });
|
const org = await getOrganizationContextInfo(params.orgslug, { revalidate: 1800, tags: ['organizations'] });
|
||||||
return {
|
return {
|
||||||
title: org.name + " — Home",
|
title: org.name + " — Home",
|
||||||
description: org.description,
|
description: org.description,
|
||||||
|
|
@ -31,7 +31,7 @@ export async function generateMetadata(
|
||||||
|
|
||||||
const OrgHomePage = async (params: any) => {
|
const OrgHomePage = async (params: any) => {
|
||||||
const orgslug = params.params.orgslug;
|
const orgslug = params.params.orgslug;
|
||||||
const courses = await getOrgCourses(orgslug, { revalidate: 360 });
|
const courses = await getOrgCourses(orgslug, { revalidate: 360 , tags: ['courses'] });
|
||||||
const collections = await getOrgCollections();
|
const collections = await getOrgCollections();
|
||||||
|
|
||||||
// function to remove "course_" from the course_id
|
// function to remove "course_" from the course_id
|
||||||
|
|
|
||||||
|
|
@ -1,4 +1,4 @@
|
||||||
import FormLayout, { ButtonBlack, Flex, FormField, FormLabel, Input, Textarea } from '@components/UI/Form/Form'
|
import FormLayout, { ButtonBlack, Flex, FormField, FormLabel, Input, Textarea } from '@components/UI/Form/Form'
|
||||||
import * as Form from '@radix-ui/react-form'
|
import * as Form from '@radix-ui/react-form'
|
||||||
import { getAPIUrl, getUriWithOrg } from '@services/config/config';
|
import { getAPIUrl, getUriWithOrg } from '@services/config/config';
|
||||||
import { FormMessage } from "@radix-ui/react-form";
|
import { FormMessage } from "@radix-ui/react-form";
|
||||||
|
|
@ -7,6 +7,7 @@ import { getOrganizationContextInfo } from '@services/organizations/orgs';
|
||||||
import React, { useState } from 'react'
|
import React, { useState } from 'react'
|
||||||
import { BarLoader } from 'react-spinners'
|
import { BarLoader } from 'react-spinners'
|
||||||
import { mutate } from 'swr';
|
import { mutate } from 'swr';
|
||||||
|
import { revalidateTags } from '@services/utils/ts/requests';
|
||||||
|
|
||||||
function CreateCourseModal({ closeModal, orgslug }: any) {
|
function CreateCourseModal({ closeModal, orgslug }: any) {
|
||||||
const [isSubmitting, setIsSubmitting] = useState(false);
|
const [isSubmitting, setIsSubmitting] = useState(false);
|
||||||
|
|
@ -19,7 +20,7 @@ function CreateCourseModal({ closeModal, orgslug }: any) {
|
||||||
|
|
||||||
|
|
||||||
const getOrgMetadata = async () => {
|
const getOrgMetadata = async () => {
|
||||||
const org = await getOrganizationContextInfo(orgslug);
|
const org = await getOrganizationContextInfo(orgslug, { revalidate: 360, tags: ['organizations'] });
|
||||||
setOrgId(org.org_id);
|
setOrgId(org.org_id);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -40,7 +41,7 @@ function CreateCourseModal({ closeModal, orgslug }: any) {
|
||||||
e.preventDefault();
|
e.preventDefault();
|
||||||
setIsSubmitting(true);
|
setIsSubmitting(true);
|
||||||
let status = await createNewCourse(orgId, { name, description }, thumbnail);
|
let status = await createNewCourse(orgId, { name, description }, thumbnail);
|
||||||
mutate(`${getAPIUrl()}courses/org_slug/${orgslug}/page/1/limit/10`);
|
revalidateTags(['courses']);
|
||||||
setIsSubmitting(false);
|
setIsSubmitting(false);
|
||||||
|
|
||||||
if (status.org_id == orgId) {
|
if (status.org_id == orgId) {
|
||||||
|
|
|
||||||
|
|
@ -1,4 +1,4 @@
|
||||||
const LEARNHOUSE_HTTP_PROTOCOL = process.env.NEXT_PUBLIC_LEARNHOUSE_HTTPS === "true" ? "https://" : "http://";
|
export const LEARNHOUSE_HTTP_PROTOCOL = process.env.NEXT_PUBLIC_LEARNHOUSE_HTTPS === "true" ? "https://" : "http://";
|
||||||
const LEARNHOUSE_API_URL = `${process.env.NEXT_PUBLIC_LEARNHOUSE_API_URL}`;
|
const LEARNHOUSE_API_URL = `${process.env.NEXT_PUBLIC_LEARNHOUSE_API_URL}`;
|
||||||
const LEARNHOUSE_BACKEND_URL = `${process.env.NEXT_PUBLIC_LEARNHOUSE_BACKEND_URL}`;
|
const LEARNHOUSE_BACKEND_URL = `${process.env.NEXT_PUBLIC_LEARNHOUSE_BACKEND_URL}`;
|
||||||
export const LEARNHOUSE_DOMAIN = process.env.NEXT_PUBLIC_LEARNHOUSE_DOMAIN;
|
export const LEARNHOUSE_DOMAIN = process.env.NEXT_PUBLIC_LEARNHOUSE_DOMAIN;
|
||||||
|
|
|
||||||
|
|
@ -1,5 +1,6 @@
|
||||||
import { AppRouterInstance } from "next/dist/shared/lib/app-router-context";
|
import { AppRouterInstance } from "next/dist/shared/lib/app-router-context";
|
||||||
import { denyAccessToUser } from "../react/middlewares/views";
|
import { denyAccessToUser } from "../react/middlewares/views";
|
||||||
|
import { LEARNHOUSE_DOMAIN, LEARNHOUSE_HTTP_PROTOCOL } from "@services/config/config";
|
||||||
|
|
||||||
export const RequestBody = (method: string, data: any, next: any) => {
|
export const RequestBody = (method: string, data: any, next: any) => {
|
||||||
let HeadersConfig = new Headers({ "Content-Type": "application/json" });
|
let HeadersConfig = new Headers({ "Content-Type": "application/json" });
|
||||||
|
|
@ -69,3 +70,9 @@ export const errorHandling = (res: any) => {
|
||||||
}
|
}
|
||||||
return res.json();
|
return res.json();
|
||||||
};
|
};
|
||||||
|
|
||||||
|
export const revalidateTags = (tags: string[]) => {
|
||||||
|
tags.forEach((tag) => {
|
||||||
|
fetch(`${LEARNHOUSE_HTTP_PROTOCOL}${LEARNHOUSE_DOMAIN}/api/revalidate?tag=${tag}`);
|
||||||
|
});
|
||||||
|
};
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue