Skip to content

Commit

Permalink
Dynamical robots.txt but bug with sitemap.tsx
Browse files Browse the repository at this point in the history
  • Loading branch information
Suboyyy committed Sep 14, 2024
1 parent 0b16639 commit ca639cd
Show file tree
Hide file tree
Showing 3 changed files with 71 additions and 5 deletions.
5 changes: 0 additions & 5 deletions public/robots.txt

This file was deleted.

21 changes: 21 additions & 0 deletions src/app/robots.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
import { MetadataRoute } from 'next';

export default function robots(): MetadataRoute.Robots {
if (process.env.NODE_ENV === 'production') {
return {
rules: {
userAgent: '*',
disallow: ['/dashboard/', '/admin/', '/uploads/'],
},
sitemap: `${process.env.NEXT_PUBLIC_URL}/sitemap.xml`,
};
} else {
return {
rules: {
userAgent: '*',
disallow: '/',
},
sitemap: `${process.env.NEXT_PUBLIC_URL}/sitemap.xml`,
};
}
}
50 changes: 50 additions & 0 deletions src/app/sitemap.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
import { MetadataRoute } from 'next';
import fs from 'fs';
import path from 'path';

export default function sitemap(): MetadataRoute.Sitemap {
// Path to the directory containing your MDX files
const siteDirectory = path.join(process.cwd(), 'tournaments'); // your blog directory maybe different

// Retrieve all MDX file paths recursively
const tsxFilePaths = getAllTsxFilePaths(siteDirectory);

// Generate URLs and add them to the sitemap
const sitemap = tsxFilePaths.map((filePath) => {
const slug = path.basename(filePath, '.tsx'); // remove the .mdx extension from the file name to get the slug
const category = path.basename(path.dirname(filePath));
const url = `${process.env.NEXT_PUBLIC_URL}/${category}/${slug}`;
const lastModified = fs.statSync(filePath).mtime;
return {
url,
lastModified,
};
});

// Add other URLs to the sitemap
sitemap.push(
{
url: `${process.env.NEXT_PUBLIC_URL}`,
lastModified: new Date(),
},
// Add other URLs here
);

return sitemap;
}

// Recursively retrieve all MDX file paths
function getAllTsxFilePaths(directory: string): string[] {
const fileNames = fs.readdirSync(directory);
const filePaths = fileNames.map((fileName) => {
const filePath = path.join(directory, fileName);
const stat = fs.statSync(filePath);
if (stat.isDirectory()) {
return getAllTsxFilePaths(filePath);
} else {
return filePath;
}
});

return Array.prototype.concat(...filePaths);
}

0 comments on commit ca639cd

Please sign in to comment.