Another example for a next js site. I've adjusted it slightly to also handle sub-folders:
const fs = require('fs'); const path = require('path'); const removeFileExt = (dirname) => { const parsedPath = path.parse(dirname); if (parsedPath.dir) { return `${parsedPath.dir}/${parsedPath.name}`; } return parsedPath.name; }; module.exports = () => { const fileObj = {}; const walkSync = (dir) => { const files = fs.readdirSync(dir); files.forEach((file) => { // specific for Next, skip files starting with '-' or between brackets '[...]' const partialNextFile = /^_\w*/; const dynamicNextFile = /^\[(.*?)\]/; if (partialNextFile.test(file) || dynamicNextFile.test(file)) { return; } const fullPath = `${dir}${file}`; const fileStat = fs.statSync(fullPath); if (fileStat.isDirectory()) { walkSync(`${fullPath}/`); } else { const parsedPath = path.parse(fullPath); if (!parsedPath.ext) { // skip system files return; } const filePathAry = path.format(parsedPath).split(path.sep); filePathAry.splice(0, 3); const fileDir = filePathAry.join('/'); const cleanPath = removeFileExt(fileDir); fileObj[`/${cleanPath}`] = { page: `/${cleanPath}`, lastModified: fileStat.mtime, }; } }); }; walkSync('./src/pages/'); walkSync('./src/posts/'); return fileObj; };
Are you sure you want to hide this comment? It will become hidden in your post, but will still be visible via the comment's permalink.
Hide child comments as well
Confirm
For further actions, you may consider blocking this person and/or reporting abuse
We're a place where coders share, stay up-to-date and grow their careers.
Another example for a next js site. I've adjusted it slightly to also handle sub-folders: