Add production environment guards to Keystatic routes
Implemented production environment guards in Keystatic routes to prevent access to certain functions in production mode. Updated 'layout.tsx.hbs' to redirect to home if the environment is production, and modified 'keystatic-route-handler.ts' to return 404 status code if attempting to utilize route handlers while in production.
This commit is contained in:
@@ -2,6 +2,28 @@ import { makeRouteHandler } from '@keystatic/next/route-handler';
|
|||||||
|
|
||||||
import config from './keystatic.config';
|
import config from './keystatic.config';
|
||||||
|
|
||||||
export const keystaticRouteHandlers = makeRouteHandler({
|
const handlers = makeRouteHandler({
|
||||||
config,
|
config,
|
||||||
});
|
});
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @name productionGuard
|
||||||
|
* @description Guard for production environment. Returns 404 if in production.
|
||||||
|
* @param routeHandler
|
||||||
|
*/
|
||||||
|
function productionGuard(routeHandler: (req: Request) => Promise<Response>) {
|
||||||
|
if (process.env.NODE_ENV === 'production') {
|
||||||
|
return new Response('Not found', { status: 404 });
|
||||||
|
}
|
||||||
|
|
||||||
|
return (req: Request) => routeHandler(req);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @name keystaticRouteHandlers
|
||||||
|
* @description Route handlers for keystatic
|
||||||
|
*/
|
||||||
|
export const keystaticRouteHandlers = {
|
||||||
|
POST: productionGuard(handlers.POST),
|
||||||
|
GET: productionGuard(handlers.GET),
|
||||||
|
};
|
||||||
|
|||||||
@@ -1,7 +1,10 @@
|
|||||||
|
import { redirect } from 'next/navigation';
|
||||||
import KeystaticAdmin from '@kit/keystatic/admin';
|
import KeystaticAdmin from '@kit/keystatic/admin';
|
||||||
|
|
||||||
export default function Layout() {
|
export default function Layout() {
|
||||||
return (
|
if (process.env.NODE_ENV === 'production') {
|
||||||
<KeystaticAdmin />
|
redirect('/');
|
||||||
);
|
}
|
||||||
|
|
||||||
|
return <KeystaticAdmin />;
|
||||||
}
|
}
|
||||||
Reference in New Issue
Block a user