remove duplicate code in next.js and previous.js