Skip to content

Commit

Permalink
Fix sitemap
Browse files Browse the repository at this point in the history
  • Loading branch information
gnovl committed Oct 26, 2024
1 parent f5b9064 commit 9ff527d
Show file tree
Hide file tree
Showing 5 changed files with 49 additions and 4 deletions.
7 changes: 7 additions & 0 deletions next-sitemap.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,15 @@ module.exports = {
sitemapSize: 7000,
changefreq: "daily",
priority: 0.7,
// Exclude the server-side sitemap from static generation
exclude: ["/server-sitemap.xml"],
robotsTxtOptions: {
policies: [
{
userAgent: "*",
allow: "/",
},
],
additionalSitemaps: ["https://averiashogar.es/server-sitemap.xml"],
},
};
3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,8 @@
"dev": "next dev",
"build": "next build",
"start": "next start",
"lint": "next lint"
"lint": "next lint",
"postbuild": "next-sitemap"
},
"dependencies": {
"@mapbox/rehype-prism": "^0.9.0",
Expand Down
2 changes: 1 addition & 1 deletion src/app/page.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ export default function Home() {
description:
"Fontanero en Madrid especializado en averías, reparaciones y mantenimiento del hogar. Servicios de fontanería, calefacción y pintura interior.",
url: "https://averiashogar.es",
telephone: "123123123",
telephone: "689680473",
address: {
"@type": "PostalAddress",
addressLocality: "Madrid",
Expand Down
37 changes: 37 additions & 0 deletions src/app/server-sitemap.xml/route.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
import { getServerSideSitemap } from "next-sitemap";
import type { ISitemapField } from "next-sitemap";
import fs from "fs";
import path from "path";

export async function GET(request: Request) {
try {
// Get the absolute path to your MDX files
const postsDirectory = path.join(process.cwd(), "src", "app", "posts"); // Adjust this path to where your MDX files are stored

// Read all MDX files from the directory
const files = fs.readdirSync(postsDirectory);

// Create sitemap entries for each blog post
const fields: ISitemapField[] = files
.filter((filename) => filename.endsWith(".mdx"))
.map((filename) => ({
loc: `${process.env.SITE_URL}/blog/${filename.replace(".mdx", "")}`,
lastmod: new Date().toISOString(),
changefreq: "daily" as const, // This fixes the TypeScript error
priority: 0.7,
}));

// Add the main blog page
fields.push({
loc: `${process.env.SITE_URL}/blog`,
lastmod: new Date().toISOString(),
changefreq: "daily" as const, // This fixes the TypeScript error
priority: 0.8,
});

return getServerSideSitemap(fields);
} catch (e) {
console.log(e);
return new Response("Something went wrong", { status: 500 });
}
}
4 changes: 2 additions & 2 deletions src/components/navigation.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -186,7 +186,7 @@ const Navbar: React.FC = () => {
</li>
</div>
<div className="mt-4 lg:mt-0">
{/* <li>
<li>
<Link
href="/blog"
className={`${inactiveStyle} mr-4 cursor-pointer`}
Expand All @@ -196,7 +196,7 @@ const Navbar: React.FC = () => {
Blog
</span>
</Link>
</li> */}
</li>
</div>
</ul>
</div>
Expand Down

0 comments on commit 9ff527d

Please sign in to comment.