-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Dynamical robots.txt but bug with sitemap.tsx
- Loading branch information
Showing
3 changed files
with
71 additions
and
5 deletions.
There are no files selected for viewing
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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`, | ||
}; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} |