Skip to content

Commit

Permalink
SEO
Browse files Browse the repository at this point in the history
  • Loading branch information
gilhanan committed Nov 3, 2023
1 parent dc0c489 commit 49de144
Show file tree
Hide file tree
Showing 3 changed files with 94 additions and 0 deletions.
4 changes: 4 additions & 0 deletions public/robots.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
User-agent: *
Allow: /

Sitemap: https://gil-hanan.com/sitemap.xml
60 changes: 60 additions & 0 deletions src/app/sitemap.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
import { MetadataRoute } from "next";
import { projects } from "@data/projects";
import sitemap from "@app/sitemap";

jest.mock("./data/projects", () => ({
projects: [{ id: "project1" }, { id: "project2" }],
}));

describe("sitemap", () => {
const host = "https://gil-hanan.com";
let lastModified: Date;
let sitemapResult: MetadataRoute.Sitemap;

beforeEach(() => {
lastModified = new Date();
jest.useFakeTimers().setSystemTime(lastModified);
sitemapResult = sitemap();
});

afterEach(() => {
jest.useRealTimers();
});

it("should include the required static URLs", () => {
const staticUrls = [
host,
`${host}/projects`,
`${host}/about`,
`${host}/contact`,
];

staticUrls.forEach((url) => {
expect(sitemapResult).toEqual(
expect.arrayContaining([
expect.objectContaining({
url,
}),
]),
);
});
});

it("should include all projects with correct URLs", () => {
projects.forEach(({ id }) => {
expect(sitemapResult).toEqual(
expect.arrayContaining([
expect.objectContaining({
url: `${host}/projects/${id}`,
}),
]),
);
});
});

it("should have lastModified date set for all entries", () => {
sitemapResult.forEach((entry) => {
expect(entry.lastModified).toEqual(lastModified);
});
});
});
30 changes: 30 additions & 0 deletions src/app/sitemap.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
import { MetadataRoute } from "next";
import { projects } from "@data/projects";

export default function sitemap(): MetadataRoute.Sitemap {
const host = "https://gil-hanan.com";
const lastModified = new Date();

return [
{
url: host,
lastModified,
},
{
url: `${host}/projects`,
lastModified,
},
{
url: `${host}/about`,
lastModified,
},
{
url: `${host}/contact`,
lastModified,
},
...projects.map(({ id }) => ({
url: `${host}/projects/${id}`,
lastModified,
})),
];
}

0 comments on commit 49de144

Please sign in to comment.