Skip to content

Commit

Permalink
CMS content is now cached based on a tag rather than time.
Browse files Browse the repository at this point in the history
  • Loading branch information
moonstar-x committed Jan 13, 2025
1 parent 49b228d commit c41574a
Show file tree
Hide file tree
Showing 7 changed files with 48 additions and 2 deletions.
12 changes: 12 additions & 0 deletions src/app/api/cache/revalidate/cms/route.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
import { NextResponse } from 'next/server';
import { revalidateTag } from 'next/cache';
import { NEXT_CACHE_TAG_STRAPI } from '@lib/constants/app';

export const POST = () => {
revalidateTag(NEXT_CACHE_TAG_STRAPI);

return NextResponse.json({
revalidated: true,
date: new Date().toISOString()
}, { status: 200 });
};
1 change: 1 addition & 0 deletions src/global.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ declare global {
LOCAL_DOWNLOADS_URL?: string

NEXT_REVALIDATE_TIME?: string
NEXT_INTERNAL_AUTH_SECRET?: string

STRAPI_API_TOKEN?: string
NEXT_PUBLIC_STRAPI_URL?: string
Expand Down
1 change: 1 addition & 0 deletions src/lib/config/app.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ export const NEXT_PUBLIC_DOWNLOADS_URL = process.env.NEXT_PUBLIC_DOWNLOADS_URL!;
export const LOCAL_DOWNLOADS_URL = process.env.LOCAL_DOWNLOADS_URL!;

export const NEXT_REVALIDATE_TIME = process.env.NEXT_REVALIDATE_TIME ? parseInt(process.env.NEXT_REVALIDATE_TIME, 10) : 3600;
export const NEXT_INTERNAL_AUTH_SECRET = process.env.NEXT_INTERNAL_AUTH_SECRET!;

export const STRAPI_API_TOKEN = process.env.STRAPI_API_TOKEN!;
export const NEXT_PUBLIC_STRAPI_URL = process.env.NEXT_PUBLIC_STRAPI_URL!;
2 changes: 2 additions & 0 deletions src/lib/constants/app.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,3 +9,5 @@ export const PEPTIDE_ATTRIBUTE_MAX_DECIMALS = 4;
export const PEPTIDE_SCORE_DECIMALS = 2;

export const DEFAULT_AUTO_RELOAD_INTERVAL_SECONDS = 5;

export const NEXT_CACHE_TAG_STRAPI = 'strapi-cms';
7 changes: 7 additions & 0 deletions src/lib/next/auth.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
import { NextRequest } from 'next/server';
import { NEXT_INTERNAL_AUTH_SECRET } from '@lib/config/app';

export const isInternalApiRequestAuthenticated = (req: NextRequest) => {
const token = req.headers.get('X-Auth-Secret');
return token === NEXT_INTERNAL_AUTH_SECRET;
};
5 changes: 3 additions & 2 deletions src/lib/services/strapi/graphql/client.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
import 'server-only';
import { ApolloClient, ApolloLink, HttpLink, InMemoryCache, NormalizedCacheObject, OperationVariables, QueryOptions } from '@apollo/client';
import { DeepPartial } from 'utility-types';
import { NEXT_REVALIDATE_TIME, NEXT_PUBLIC_STRAPI_URL, STRAPI_API_TOKEN } from '@lib/config/app';
import { NEXT_PUBLIC_STRAPI_URL, STRAPI_API_TOKEN } from '@lib/config/app';
import { NEXT_CACHE_TAG_STRAPI } from '@lib/constants/app';

const httpLink = new HttpLink({
uri: `${NEXT_PUBLIC_STRAPI_URL}/graphql`,
Expand All @@ -13,7 +14,7 @@ const httpLink = new HttpLink({
Authorization: `Bearer ${STRAPI_API_TOKEN}`
},
next: {
revalidate: NEXT_REVALIDATE_TIME
tags: [NEXT_CACHE_TAG_STRAPI]
}
});
}
Expand Down
22 changes: 22 additions & 0 deletions src/middleware.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
import { NextRequest, NextResponse } from 'next/server';
import { isInternalApiRequestAuthenticated } from '@lib/next/auth';

export const config = {
matcher: ['/api/:path*']
};

const middleware = (req: NextRequest) => {
if (!req.nextUrl.pathname.startsWith('/api')) {
return NextResponse.next();
}

if (!isInternalApiRequestAuthenticated(req)) {
return NextResponse.json({
message: 'You are not authenticated.'
}, { status: 401 });
}

return NextResponse.next();
};

export default middleware;

0 comments on commit c41574a

Please sign in to comment.