-
-
Notifications
You must be signed in to change notification settings - Fork 700
Added revalidation docs #1444
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Added revalidation docs #1444
Conversation
|
WalkthroughThe changes involve updates to the Changes
Possibly related PRs
Suggested reviewers
Thank you for using CodeRabbit. We offer it for free to the OSS community and would appreciate your support in helping us grow. If you find it useful, would you consider giving us a shout-out on your favorite social media? 🪧 TipsChatThere are 3 ways to chat with CodeRabbit:
Note: Be mindful of the bot's finite context window. It's strongly recommended to break down tasks such as reading entire modules into smaller chunks. For a focused discussion, use review comments to chat about specific files and their changes, instead of using the PR comments. CodeRabbit Commands (Invoked using PR comments)
Other keywords and placeholders
CodeRabbit Configuration File (
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Actionable comments posted: 3
🧹 Outside diff range and nitpick comments (2)
docs/guides/frameworks/nextjs.mdx (2)
257-264
: Consider adding security implications.While the introduction effectively explains revalidation, it would be beneficial to add a note about security considerations. Specifically, mention that the revalidation endpoint could be a potential attack vector if not properly secured, which is why the secret token validation is crucial.
391-399
: Enhance testing documentation.Consider adding:
- Validation rules for the path parameter (e.g., must start with '/', no query parameters)
- More example payloads for common scenarios
- Expected responses for both success and failure cases
Add examples like:
// Example 1: Revalidate blog post { "path": "blog/post-1" } // Example 2: Revalidate entire section { "path": "products" } // Example responses Success: { "revalidated": true } Error: { "message": "Invalid secret" }
📜 Review details
Configuration used: CodeRabbit UI
Review profile: CHILL
📒 Files selected for processing (1)
docs/guides/frameworks/nextjs.mdx
(2 hunks)
🔇 Additional comments (1)
docs/guides/frameworks/nextjs.mdx (1)
251-251
: LGTM!
The expanded section title better reflects the content that includes both troubleshooting information and additional resources.
docs/guides/frameworks/nextjs.mdx
Outdated
#### Revalidation helper: App Router | ||
|
||
If you are using the App router, create a new revalidation route at `app/api/revalidate/path/route.ts`: | ||
|
||
```ts app/api/revalidate/path/route.ts | ||
import { NextRequest, NextResponse } from "next/server"; | ||
import { revalidatePath } from "next/cache"; | ||
|
||
export async function POST(request: NextRequest) { | ||
try { | ||
const { path, type, secret } = await request.json(); | ||
// Create a REVALIDATION_SECRET and set it in your environment variables | ||
if (secret !== process.env.REVALIDATION_SECRET) { | ||
return NextResponse.json({ message: "Invalid secret" }, { status: 401 }); | ||
} | ||
|
||
if (!path) { | ||
return NextResponse.json({ message: "Path is required" }, { status: 400 }); | ||
} | ||
|
||
revalidatePath(path, type); | ||
|
||
return NextResponse.json({ revalidated: true }); | ||
} catch (err) { | ||
console.error("Error revalidating path:", err); | ||
return NextResponse.json({ message: "Error revalidating path" }, { status: 500 }); | ||
} | ||
} | ||
``` |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
🛠️ Refactor suggestion
Enhance error handling and documentation.
While the implementation is solid, consider these improvements:
- Document the optional
type
parameter in the request body (it can be 'page' or 'layout') - Use template literals for error messages
- Consider adding more specific error types
- console.error("Error revalidating path:", err);
+ console.error(`Error revalidating path: ${err instanceof Error ? err.message : String(err)}`);
Also, consider adding this documentation above the code:
type RevalidateRequestBody = {
path: string;
type?: "page" | "layout"; // Optional parameter to specify revalidation type
secret: string;
};
docs/guides/frameworks/nextjs.mdx
Outdated
#### Revalidation helper: Pages Router | ||
|
||
If you are using the Pages router, create a new revalidation route at `pages/api/revalidate/path.ts`: | ||
|
||
```ts pages/api/revalidate/path.ts | ||
import type { NextApiRequest, NextApiResponse } from "next"; | ||
|
||
export default async function handler(req: NextApiRequest, res: NextApiResponse) { | ||
try { | ||
if (req.method !== "POST") { | ||
return res.status(405).json({ message: "Method not allowed" }); | ||
} | ||
|
||
const { path, secret } = req.body; | ||
|
||
if (secret !== process.env.REVALIDATION_SECRET) { | ||
return res.status(401).json({ message: "Invalid secret" }); | ||
} | ||
|
||
if (!path) { | ||
return res.status(400).json({ message: "Path is required" }); | ||
} | ||
|
||
await res.revalidate(path); | ||
|
||
return res.json({ revalidated: true }); | ||
} catch (err) { | ||
console.error("Error revalidating path:", err); | ||
return res.status(500).json({ message: "Error revalidating path" }); | ||
} | ||
} | ||
``` |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
🛠️ Refactor suggestion
Enhance type safety and error handling.
Consider these improvements:
- Add type restriction for HTTP methods
- Use template literals for error messages
- Add more specific error types
+ type HttpMethod = 'GET' | 'POST' | 'PUT' | 'DELETE';
+ interface RevalidateRequest extends NextApiRequest {
+ method: HttpMethod;
+ body: {
+ path: string;
+ secret: string;
+ };
+ }
- export default async function handler(req: NextApiRequest, res: NextApiResponse) {
+ export default async function handler(req: RevalidateRequest, res: NextApiResponse) {
Committable suggestion was skipped due to low confidence.
#### Revalidation task | ||
|
||
This task takes a `path` as a payload and will revalidate the path you specify, using the handler you set up previously. | ||
|
||
<Note> | ||
|
||
To run this task locally you will need to set the `REVALIDATION_SECRET` environment variable in your `.env.local` file (or `.env` file if using Pages router). | ||
|
||
To run this task in production, you will need to set the `REVALIDATION_SECRET` environment variable in Vercel, in your project settings, and also in your environment variables in the Trigger.dev dashboard. | ||
|
||
</Note> | ||
|
||
```ts trigger/revalidate-path.ts | ||
import { logger, task } from "@trigger.dev/sdk/v3"; | ||
|
||
const NEXTJS_APP_URL = process.env.NEXTJS_APP_URL; // e.g. "http://localhost:3000" or "https://my-nextjs-app.vercel.app" | ||
const REVALIDATION_SECRET = process.env.REVALIDATION_SECRET; // Create a REVALIDATION_SECRET and set it in your environment variables | ||
|
||
export const revalidatePath = task({ | ||
id: "revalidate-path", | ||
run: async (payload: { path: string }) => { | ||
const { path } = payload; | ||
|
||
try { | ||
const response = await fetch(`${NEXTJS_APP_URL}/api/revalidate/path`, { | ||
method: "POST", | ||
headers: { | ||
"Content-Type": "application/json", | ||
}, | ||
body: JSON.stringify({ | ||
path: `${NEXTJS_APP_URL}/${path}`, | ||
secret: REVALIDATION_SECRET, | ||
}), | ||
}); | ||
|
||
if (response.ok) { | ||
logger.log("Path revalidation successful", { path }); | ||
return { success: true }; | ||
} else { | ||
logger.error("Path revalidation failed", { | ||
path, | ||
statusCode: response.status, | ||
statusText: response.statusText, | ||
}); | ||
return { | ||
success: false, | ||
error: `Revalidation failed with status ${response.status}: ${response.statusText}`, | ||
}; | ||
} | ||
} catch (error) { | ||
logger.error("Path revalidation encountered an error", { | ||
path, | ||
error: error instanceof Error ? error.message : String(error), | ||
}); | ||
return { | ||
success: false, | ||
error: `Failed to revalidate path due to an unexpected error`, | ||
}; | ||
} | ||
}, | ||
}); | ||
``` |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Improve URL handling and environment validation.
The implementation needs some improvements:
- URL Construction: The current implementation might create URLs with double slashes. Use
URL
class instead. - Environment Variables: Add validation at startup.
- Network Failures: Consider adding retry logic.
Apply these improvements:
+ // Validate environment variables at startup
+ if (!NEXTJS_APP_URL) {
+ throw new Error("NEXTJS_APP_URL environment variable is required");
+ }
+ if (!REVALIDATION_SECRET) {
+ throw new Error("REVALIDATION_SECRET environment variable is required");
+ }
run: async (payload: { path: string }) => {
const { path } = payload;
+ // Ensure clean URL construction
+ const baseUrl = new URL("/api/revalidate/path", NEXTJS_APP_URL);
+ const pathToRevalidate = new URL(path, NEXTJS_APP_URL).pathname;
try {
- const response = await fetch(`${NEXTJS_APP_URL}/api/revalidate/path`, {
+ const response = await fetch(baseUrl.toString(), {
method: "POST",
headers: {
"Content-Type": "application/json",
},
body: JSON.stringify({
- path: `${NEXTJS_APP_URL}/${path}`,
+ path: pathToRevalidate,
secret: REVALIDATION_SECRET,
}),
});
📝 Committable suggestion
‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.
#### Revalidation task | |
This task takes a `path` as a payload and will revalidate the path you specify, using the handler you set up previously. | |
<Note> | |
To run this task locally you will need to set the `REVALIDATION_SECRET` environment variable in your `.env.local` file (or `.env` file if using Pages router). | |
To run this task in production, you will need to set the `REVALIDATION_SECRET` environment variable in Vercel, in your project settings, and also in your environment variables in the Trigger.dev dashboard. | |
</Note> | |
```ts trigger/revalidate-path.ts | |
import { logger, task } from "@trigger.dev/sdk/v3"; | |
const NEXTJS_APP_URL = process.env.NEXTJS_APP_URL; // e.g. "http://localhost:3000" or "https://my-nextjs-app.vercel.app" | |
const REVALIDATION_SECRET = process.env.REVALIDATION_SECRET; // Create a REVALIDATION_SECRET and set it in your environment variables | |
export const revalidatePath = task({ | |
id: "revalidate-path", | |
run: async (payload: { path: string }) => { | |
const { path } = payload; | |
try { | |
const response = await fetch(`${NEXTJS_APP_URL}/api/revalidate/path`, { | |
method: "POST", | |
headers: { | |
"Content-Type": "application/json", | |
}, | |
body: JSON.stringify({ | |
path: `${NEXTJS_APP_URL}/${path}`, | |
secret: REVALIDATION_SECRET, | |
}), | |
}); | |
if (response.ok) { | |
logger.log("Path revalidation successful", { path }); | |
return { success: true }; | |
} else { | |
logger.error("Path revalidation failed", { | |
path, | |
statusCode: response.status, | |
statusText: response.statusText, | |
}); | |
return { | |
success: false, | |
error: `Revalidation failed with status ${response.status}: ${response.statusText}`, | |
}; | |
} | |
} catch (error) { | |
logger.error("Path revalidation encountered an error", { | |
path, | |
error: error instanceof Error ? error.message : String(error), | |
}); | |
return { | |
success: false, | |
error: `Failed to revalidate path due to an unexpected error`, | |
}; | |
} | |
}, | |
}); | |
``` | |
#### Revalidation task | |
This task takes a `path` as a payload and will revalidate the path you specify, using the handler you set up previously. | |
<Note> | |
To run this task locally you will need to set the `REVALIDATION_SECRET` environment variable in your `.env.local` file (or `.env` file if using Pages router). | |
To run this task in production, you will need to set the `REVALIDATION_SECRET` environment variable in Vercel, in your project settings, and also in your environment variables in the Trigger.dev dashboard. | |
</Note> | |
```ts trigger/revalidate-path.ts | |
import { logger, task } from "@trigger.dev/sdk/v3"; | |
const NEXTJS_APP_URL = process.env.NEXTJS_APP_URL; // e.g. "http://localhost:3000" or "https://my-nextjs-app.vercel.app" | |
const REVALIDATION_SECRET = process.env.REVALIDATION_SECRET; // Create a REVALIDATION_SECRET and set it in your environment variables | |
// Validate environment variables at startup | |
if (!NEXTJS_APP_URL) { | |
throw new Error("NEXTJS_APP_URL environment variable is required"); | |
} | |
if (!REVALIDATION_SECRET) { | |
throw new Error("REVALIDATION_SECRET environment variable is required"); | |
} | |
export const revalidatePath = task({ | |
id: "revalidate-path", | |
run: async (payload: { path: string }) => { | |
const { path } = payload; | |
// Ensure clean URL construction | |
const baseUrl = new URL("/api/revalidate/path", NEXTJS_APP_URL); | |
const pathToRevalidate = new URL(path, NEXTJS_APP_URL).pathname; | |
try { | |
const response = await fetch(baseUrl.toString(), { | |
method: "POST", | |
headers: { | |
"Content-Type": "application/json", | |
}, | |
body: JSON.stringify({ | |
path: pathToRevalidate, | |
secret: REVALIDATION_SECRET, | |
}), | |
}); | |
if (response.ok) { | |
logger.log("Path revalidation successful", { path }); | |
return { success: true }; | |
} else { | |
logger.error("Path revalidation failed", { | |
path, | |
statusCode: response.status, | |
statusText: response.statusText, | |
}); | |
return { | |
success: false, | |
error: `Revalidation failed with status ${response.status}: ${response.statusText}`, | |
}; | |
} | |
} catch (error) { | |
logger.error("Path revalidation encountered an error", { | |
path, | |
error: error instanceof Error ? error.message : String(error), | |
}); | |
return { | |
success: false, | |
error: `Failed to revalidate path due to an unexpected error`, | |
}; | |
} | |
}, | |
}); | |
``` |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Actionable comments posted: 1
🧹 Outside diff range and nitpick comments (3)
docs/guides/frameworks/nextjs.mdx (3)
267-293
: Consider adding JSDoc documentation for the route handler.While the implementation is solid, adding JSDoc documentation would improve maintainability and help other developers understand the API's purpose and requirements.
Add this documentation above the route handler:
/** * Route handler for revalidating Next.js paths * @route POST /api/revalidate/path * @param {Object} request - The request object containing path and secret * @returns {Object} JSON response indicating revalidation status */
297-326
: Consider adding JSDoc documentation for the API route handler.While the implementation is solid, adding JSDoc documentation would improve maintainability and help other developers understand the API's purpose and requirements.
Add this documentation above the route handler:
/** * API route handler for revalidating Next.js paths * @route POST /api/revalidate/path * @param {NextApiRequest} req - The request object containing path and secret * @param {NextApiResponse} res - The response object * @returns {Promise<void>} */
395-399
: Improve JSON payload example.The current JSON example contains a comment which is not valid JSON syntax. Consider providing multiple complete, valid examples.
Replace the current example with:
{ "path": "blog" }And add more examples:
{ "path": "posts/123" }{ "path": "categories/tech" }
📜 Review details
Configuration used: CodeRabbit UI
Review profile: CHILL
📒 Files selected for processing (1)
docs/guides/frameworks/nextjs.mdx
(1 hunks)
🧰 Additional context used
🪛 LanguageTool
docs/guides/frameworks/nextjs.mdx
[uncategorized] ~334-~334: Possible missing comma found.
Context: ...p previously. To run this task locally you will need to set the `REVALIDATION_...
(AI_HYDRA_LEO_MISSING_COMMA)
export const revalidatePath = task({ | ||
id: "revalidate-path", | ||
run: async (payload: { path: string }) => { | ||
const { path } = payload; | ||
|
||
try { | ||
const response = await fetch(`${NEXTJS_APP_URL}/api/revalidate/path`, { | ||
method: "POST", | ||
headers: { | ||
"Content-Type": "application/json", | ||
}, | ||
body: JSON.stringify({ | ||
path: `${NEXTJS_APP_URL}/${path}`, | ||
secret: REVALIDATION_SECRET, | ||
}), | ||
}); | ||
|
||
if (response.ok) { | ||
logger.log("Path revalidation successful", { path }); | ||
return { success: true }; | ||
} else { | ||
logger.error("Path revalidation failed", { | ||
path, | ||
statusCode: response.status, | ||
statusText: response.statusText, | ||
}); | ||
return { | ||
success: false, | ||
error: `Revalidation failed with status ${response.status}: ${response.statusText}`, | ||
}; | ||
} | ||
} catch (error) { | ||
logger.error("Path revalidation encountered an error", { | ||
path, | ||
error: error instanceof Error ? error.message : String(error), | ||
}); | ||
return { | ||
success: false, | ||
error: `Failed to revalidate path due to an unexpected error`, | ||
}; | ||
} | ||
}, | ||
}); | ||
``` |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
🛠️ Refactor suggestion
Enhance type safety and input validation.
Consider adding input validation and improving type safety:
- Define a proper type for the payload
- Add validation for the path format
Apply these improvements:
+type RevalidatePathPayload = {
+ path: string;
+};
+
+function isValidPath(path: string): boolean {
+ return path.length > 0 && !path.includes('..') && !path.startsWith('/');
+}
+
export const revalidatePath = task({
id: "revalidate-path",
- run: async (payload: { path: string }) => {
+ run: async (payload: RevalidatePathPayload) => {
const { path } = payload;
+
+ if (!isValidPath(path)) {
+ logger.error("Invalid path format", { path });
+ return {
+ success: false,
+ error: "Invalid path format. Path should not be empty, contain '..' or start with '/'",
+ };
+ }
📝 Committable suggestion
‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.
export const revalidatePath = task({ | |
id: "revalidate-path", | |
run: async (payload: { path: string }) => { | |
const { path } = payload; | |
try { | |
const response = await fetch(`${NEXTJS_APP_URL}/api/revalidate/path`, { | |
method: "POST", | |
headers: { | |
"Content-Type": "application/json", | |
}, | |
body: JSON.stringify({ | |
path: `${NEXTJS_APP_URL}/${path}`, | |
secret: REVALIDATION_SECRET, | |
}), | |
}); | |
if (response.ok) { | |
logger.log("Path revalidation successful", { path }); | |
return { success: true }; | |
} else { | |
logger.error("Path revalidation failed", { | |
path, | |
statusCode: response.status, | |
statusText: response.statusText, | |
}); | |
return { | |
success: false, | |
error: `Revalidation failed with status ${response.status}: ${response.statusText}`, | |
}; | |
} | |
} catch (error) { | |
logger.error("Path revalidation encountered an error", { | |
path, | |
error: error instanceof Error ? error.message : String(error), | |
}); | |
return { | |
success: false, | |
error: `Failed to revalidate path due to an unexpected error`, | |
}; | |
} | |
}, | |
}); | |
``` | |
type RevalidatePathPayload = { | |
path: string; | |
}; | |
function isValidPath(path: string): boolean { | |
return path.length > 0 && !path.includes('..') && !path.startsWith('/'); | |
} | |
export const revalidatePath = task({ | |
id: "revalidate-path", | |
run: async (payload: RevalidatePathPayload) => { | |
const { path } = payload; | |
if (!isValidPath(path)) { | |
logger.error("Invalid path format", { path }); | |
return { | |
success: false, | |
error: "Invalid path format. Path should not be empty, contain '..' or start with '/'", | |
}; | |
} | |
try { | |
const response = await fetch(`${NEXTJS_APP_URL}/api/revalidate/path`, { | |
method: "POST", | |
headers: { | |
"Content-Type": "application/json", | |
}, | |
body: JSON.stringify({ | |
path: `${NEXTJS_APP_URL}/${path}`, | |
secret: REVALIDATION_SECRET, | |
}), | |
}); | |
if (response.ok) { | |
logger.log("Path revalidation successful", { path }); | |
return { success: true }; | |
} else { | |
logger.error("Path revalidation failed", { | |
path, | |
statusCode: response.status, | |
statusText: response.statusText, | |
}); | |
return { | |
success: false, | |
error: `Revalidation failed with status ${response.status}: ${response.statusText}`, | |
}; | |
} | |
} catch (error) { | |
logger.error("Path revalidation encountered an error", { | |
path, | |
error: error instanceof Error ? error.message : String(error), | |
}); | |
return { | |
success: false, | |
error: `Failed to revalidate path due to an unexpected error`, | |
}; | |
} | |
}, | |
}); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Actionable comments posted: 2
🧹 Outside diff range and nitpick comments (1)
docs/guides/frameworks/nextjs.mdx (1)
395-399
: Enhance the JSON payload example with better formatting and documentation.The JSON example could be more helpful with proper formatting and additional examples.
Replace the current JSON with:
-{ - "path": "<path-to-revalidate>" // e.g. "blog" -} +{ + // The path to revalidate, relative to your site's root + "path": "blog/post-1", // Will revalidate /blog/post-1 + + // Optional: specify the type of revalidation (App Router only) + "type": "page" // Can be "page" or "layout" +}
```ts pages/api/revalidate/path.ts | ||
import type { NextApiRequest, NextApiResponse } from "next"; | ||
|
||
export default async function handler(req: NextApiRequest, res: NextApiResponse) { | ||
try { | ||
if (req.method !== "POST") { | ||
return res.status(405).json({ message: "Method not allowed" }); | ||
} | ||
|
||
const { path, secret } = req.body; | ||
|
||
if (secret !== process.env.REVALIDATION_SECRET) { | ||
return res.status(401).json({ message: "Invalid secret" }); | ||
} | ||
|
||
if (!path) { | ||
return res.status(400).json({ message: "Path is required" }); | ||
} | ||
|
||
await res.revalidate(path); | ||
|
||
return res.json({ revalidated: true }); | ||
} catch (err) { | ||
console.error("Error revalidating path:", err); | ||
return res.status(500).json({ message: "Error revalidating path" }); | ||
} | ||
} | ||
``` |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
🛠️ Refactor suggestion
Enhance type safety and error handling in the Pages Router handler.
Consider these improvements to align with best practices:
- Add type restriction for HTTP methods
- Add proper type definitions for the request body
- Enhance error handling with specific error types
Apply these improvements:
+type HttpMethod = 'GET' | 'POST' | 'PUT' | 'DELETE';
+interface RevalidateRequest extends NextApiRequest {
+ method: HttpMethod;
+ body: {
+ path: string;
+ secret: string;
+ };
+}
-export default async function handler(req: NextApiRequest, res: NextApiResponse) {
+export default async function handler(req: RevalidateRequest, res: NextApiResponse) {
try {
if (req.method !== "POST") {
return res.status(405).json({ message: "Method not allowed" });
}
const { path, secret } = req.body;
if (secret !== process.env.REVALIDATION_SECRET) {
return res.status(401).json({ message: "Invalid secret" });
}
if (!path) {
return res.status(400).json({ message: "Path is required" });
}
await res.revalidate(path);
return res.json({ revalidated: true });
} catch (err) {
- console.error("Error revalidating path:", err);
+ console.error(`Error revalidating path: ${err instanceof Error ? err.message : String(err)}`);
return res.status(500).json({ message: "Error revalidating path" });
}
}
📝 Committable suggestion
‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.
```ts pages/api/revalidate/path.ts | |
import type { NextApiRequest, NextApiResponse } from "next"; | |
export default async function handler(req: NextApiRequest, res: NextApiResponse) { | |
try { | |
if (req.method !== "POST") { | |
return res.status(405).json({ message: "Method not allowed" }); | |
} | |
const { path, secret } = req.body; | |
if (secret !== process.env.REVALIDATION_SECRET) { | |
return res.status(401).json({ message: "Invalid secret" }); | |
} | |
if (!path) { | |
return res.status(400).json({ message: "Path is required" }); | |
} | |
await res.revalidate(path); | |
return res.json({ revalidated: true }); | |
} catch (err) { | |
console.error("Error revalidating path:", err); | |
return res.status(500).json({ message: "Error revalidating path" }); | |
} | |
} | |
``` | |
type HttpMethod = 'GET' | 'POST' | 'PUT' | 'DELETE'; | |
interface RevalidateRequest extends NextApiRequest { | |
method: HttpMethod; | |
body: { | |
path: string; | |
secret: string; | |
}; | |
} | |
export default async function handler(req: RevalidateRequest, res: NextApiResponse) { | |
try { | |
if (req.method !== "POST") { | |
return res.status(405).json({ message: "Method not allowed" }); | |
} | |
const { path, secret } = req.body; | |
if (secret !== process.env.REVALIDATION_SECRET) { | |
return res.status(401).json({ message: "Invalid secret" }); | |
} | |
if (!path) { | |
return res.status(400).json({ message: "Path is required" }); | |
} | |
await res.revalidate(path); | |
return res.json({ revalidated: true }); | |
} catch (err) { | |
console.error(`Error revalidating path: ${err instanceof Error ? err.message : String(err)}`); | |
return res.status(500).json({ message: "Error revalidating path" }); | |
} | |
} |
```ts app/api/revalidate/path/route.ts | ||
import { NextRequest, NextResponse } from "next/server"; | ||
import { revalidatePath } from "next/cache"; | ||
|
||
export async function POST(request: NextRequest) { | ||
try { | ||
const { path, type, secret } = await request.json(); | ||
// Create a REVALIDATION_SECRET and set it in your environment variables | ||
if (secret !== process.env.REVALIDATION_SECRET) { | ||
return NextResponse.json({ message: "Invalid secret" }, { status: 401 }); | ||
} | ||
|
||
if (!path) { | ||
return NextResponse.json({ message: "Path is required" }, { status: 400 }); | ||
} | ||
|
||
revalidatePath(path, type); | ||
|
||
return NextResponse.json({ revalidated: true }); | ||
} catch (err) { | ||
console.error("Error revalidating path:", err); | ||
return NextResponse.json({ message: "Error revalidating path" }, { status: 500 }); | ||
} | ||
} | ||
``` |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
🛠️ Refactor suggestion
Enhance type safety and error handling in the App Router handler.
Consider these improvements to align with best practices:
- Add proper type definitions for the request body
- Enhance error handling with specific error types
- Document the optional
type
parameter
Apply these improvements:
+type RevalidateRequestBody = {
+ path: string;
+ type?: "page" | "layout"; // Optional parameter to specify revalidation type
+ secret: string;
+};
export async function POST(request: NextRequest) {
try {
- const { path, type, secret } = await request.json();
+ const body = await request.json() as RevalidateRequestBody;
+ const { path, type, secret } = body;
if (secret !== process.env.REVALIDATION_SECRET) {
return NextResponse.json({ message: "Invalid secret" }, { status: 401 });
}
if (!path) {
return NextResponse.json({ message: "Path is required" }, { status: 400 });
}
revalidatePath(path, type);
return NextResponse.json({ revalidated: true });
} catch (err) {
- console.error("Error revalidating path:", err);
+ console.error(`Error revalidating path: ${err instanceof Error ? err.message : String(err)}`);
return NextResponse.json({ message: "Error revalidating path" }, { status: 500 });
}
}
📝 Committable suggestion
‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.
```ts app/api/revalidate/path/route.ts | |
import { NextRequest, NextResponse } from "next/server"; | |
import { revalidatePath } from "next/cache"; | |
export async function POST(request: NextRequest) { | |
try { | |
const { path, type, secret } = await request.json(); | |
// Create a REVALIDATION_SECRET and set it in your environment variables | |
if (secret !== process.env.REVALIDATION_SECRET) { | |
return NextResponse.json({ message: "Invalid secret" }, { status: 401 }); | |
} | |
if (!path) { | |
return NextResponse.json({ message: "Path is required" }, { status: 400 }); | |
} | |
revalidatePath(path, type); | |
return NextResponse.json({ revalidated: true }); | |
} catch (err) { | |
console.error("Error revalidating path:", err); | |
return NextResponse.json({ message: "Error revalidating path" }, { status: 500 }); | |
} | |
} | |
``` | |
```ts app/api/revalidate/path/route.ts | |
import { NextRequest, NextResponse } from "next/server"; | |
import { revalidatePath } from "next/cache"; | |
type RevalidateRequestBody = { | |
path: string; | |
type?: "page" | "layout"; // Optional parameter to specify revalidation type | |
secret: string; | |
}; | |
export async function POST(request: NextRequest) { | |
try { | |
const body = await request.json() as RevalidateRequestBody; | |
const { path, type, secret } = body; | |
if (secret !== process.env.REVALIDATION_SECRET) { | |
return NextResponse.json({ message: "Invalid secret" }, { status: 401 }); | |
} | |
if (!path) { | |
return NextResponse.json({ message: "Path is required" }, { status: 400 }); | |
} | |
revalidatePath(path, type); | |
return NextResponse.json({ revalidated: true }); | |
} catch (err) { | |
console.error(`Error revalidating path: ${err instanceof Error ? err.message : String(err)}`); | |
return NextResponse.json({ message: "Error revalidating path" }, { status: 500 }); | |
} | |
} | |
``` |
Summary by CodeRabbit
New Features
Documentation