DEV Community

Discussion on: Auto-Generate sitemap.xml in Next.js

Collapse
 
studiospindle profile image
Remi Vledder

Very nice! I've made some changes in my project, perhaps this is of any use to someone.

In the walkSync method I used the 'dir' parameter to replace pages/. This makes the method reusable for any other paths such as posts.

// ... not shown for brevity
const cleanFileName = filePath.substr(0, filePath.lastIndexOf('.')).replace(dir, '');
// ... not shown for brevity

Also, by using an early return I filtered out any files that do not compile to an actual file:

// ... not shown for brevity
files.forEach((file) => {
      const partialNextFile = /^_\w*/; // any filenames starting with _*
      const dynamicNextFile = /^\[(.*?)\]/; // any filenames between brackets [*]
      if (partialNextFile.test(file) || dynamicNextFile.test(file)) {
        // skip
        return;
      }
// ... not shown for brevity