Scheduled functions in Next.js with Edge runtime
Create a scheduled Edge function in Next.js that performs a task, such as clearing cache or sending emails, at specified intervals.
import { schedule } from 'next-edge-functions';
// Define your task function to perform the desired operation
async function clearCacheTask() {
// Your cache clearing logic here
console.log('Cache cleared successfully!');
// You can also use fetch or other Node.js methods if needed
// e.g., to send a request to an API to clear the cache
}
// Schedule the function to run at a specified interval
// '*/10 * * * * *' is the cron format which means every 10 seconds
// You can change it to suit your needs, e.g., '0 0 * * * *' for every hour
schedule('*/10 * * * * *', clearCacheTask);
This code snippet schedules an Edge function named 'clearCacheTask' to be executed every 10 seconds. The function can contain logic to clear a cache or perform other tasks, and is scheduled using a cron string format. The 'schedule' function from 'next-edge-functions' is used to set the interval.