Route Handlers
The route handlers included in @nextwp/core
add endpoints in your Next.js project's app router to handle things like preview and revalidation.
These route handlers are included by default in the starter project generated with create-nextwp-app.
revalidate
The revalidate route handler in Next.js allows for instant content updates without triggering a full site build. When a post is updated in WordPress, the specific page(s) in Next.js are regenerated, ensuring that the latest content is displayed.
To use the revalidate route handler, you need to install the On-Demand Revalidation WordPress plugin.
src/app/api/revalidate/route.ts
export { revalidate as PUT } from '@nextwp/core'
preview
The preview
route handler is used to preview posts in WordPress.
You can use preview with default settings by simply re-exporting the handler.
src/app/api/draft/[...preview]/route.ts
export { preview as GET } from '@nextwp/core'
Or you can customize the preview handler by passing options to the preview
function.
src/app/api/draft/[...preview]/route.ts
import { preview } from '@nextwp/core'
const previewOptions = {
toolbar: true, // Shows the preview toolbar on the left side of the screen
}
const previewHandler = preview(previewOptions)
export { previewHandler as GET }