Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Automatic robot.txt & sitemap.xml #382

Merged
merged 15 commits into from
Sep 23, 2024
5 changes: 0 additions & 5 deletions public/robots.txt

This file was deleted.

72 changes: 0 additions & 72 deletions public/sitemap.xml

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`,
};
}
}
89 changes: 89 additions & 0 deletions src/app/sitemap.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,89 @@
import { MetadataRoute } from 'next';
import fs from 'fs';
import path from 'path';

export default async function sitemap(): Promise<MetadataRoute.Sitemap> {
//const tournamentId = await tournamentsId();

//console.log(tournamentId);

const tournamentId = ['lol', 'cs2', 'osu', 'ssbu', 'tft', 'open'];

// Path to the directory containing your TSX files
const siteDirectory = path.join(process.cwd(), '/src/app');

// Retrieve all file paths recursively
const filePaths = getAllFilePaths(siteDirectory);

const absoluteAppPaths = path.resolve('./src');

const allowedFilePaths = filePaths.filter((filePath) => {
const relativeFilePaths = path.relative(absoluteAppPaths, filePath);
if (
relativeFilePaths.slice(3, 15) !== '/(dashboard)' &&
relativeFilePaths.slice(3, 9) !== '/oauth' &&
relativeFilePaths.slice(3, 9) !== '/reset' &&
relativeFilePaths.slice(3, 12) !== '/validate' &&
relativeFilePaths.slice(3, 12) !== '/page.tsx'
) {
return filePath;
}
});

// Select only .tsx files
const tsxFilePaths = allowedFilePaths.filter((filePath) => {
if (filePath.slice(-8) === 'page.tsx' && filePath.indexOf('[') === -1) {
return filePath;
}
});

// Generate URLs and add them to the sitemap
const sitemap = tsxFilePaths.map((filePath) => {
const category = path.basename(path.dirname(filePath));
const url = `${process.env.NEXT_PUBLIC_URL}/${category}`;
const changeFrequency = 'weekly' as const;
const lastModified = fs.statSync(filePath).mtime;
const priority = 0.5;
return {
url,
lastModified,
changeFrequency,
priority,
};
});

// Add other URLs to the sitemap
for (const id of tournamentId) {
sitemap.push({
url: `${process.env.NEXT_PUBLIC_URL}/tournament/${id}`,
lastModified: new Date(),
changeFrequency: 'weekly' as const,
priority: 0.8,
});
}

sitemap.push({
url: `${process.env.NEXT_PUBLIC_URL}`,
lastModified: new Date(),
changeFrequency: 'weekly' as const,
priority: 1.0,
});

return sitemap;
}

// Recursively retrieve all file paths
function getAllFilePaths(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 getAllFilePaths(filePath);
} else {
return filePath;
}
});

return Array.prototype.concat(...filePaths);
}
8 changes: 8 additions & 0 deletions src/modules/tournament.ts
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,14 @@ export const fetchTournaments = (): AppThunk => async (dispatch) => {
dispatch(setTournaments(res));
};

export const tournamentsId = async (): Promise<string[]> => {
const tournaments: Tournament[] = await API.get('tournaments');
const tournamentsId = tournaments.map((tournament) => {
return tournament.id;
});
return tournamentsId;
};

export const fetchSlots = (): AppThunk => async (dispatch) => {
const res = await API.get('tournaments?paidOnly=true');
const slots = res.reduce(
Expand Down
Loading