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

[Feature]: Fix/Add Correct "Sitemap.xml" Route routes/[sitemap.xml].tsx #25

Open
jimbrig opened this issue Oct 2, 2024 · 0 comments
Open
Assignees
Labels
enhancement New feature or request feature New feature requests help wanted Extra attention is needed refactor Code refactoring and cleanup

Comments

@jimbrig
Copy link
Member

jimbrig commented Oct 2, 2024

Currently there is app/app/routes/site-map.tsx.

I think that we should have a proper sitemap.xml route exposed that uses the proper Remix routing file path (routes/[sitemap.xml].tsx) and provides the necessary loader() function to generate the sitemap given the request and context.

Something along the lines of:

// app/routes/[sitemap.xml].tsx

import type { LoaderFunctionArgs } from "@remix-run/server-runtime";

interface SitemapEntry {
  loc: string;
  lastmod?: string;
  changefreq?:
    | "never"
    | "yearly"
    | "monthly"
    | "weekly"
    | "daily"
    | "hourly"
    | "always";
  priority?: 1.0 | 0.9 | 0.8 | 0.7 | 0.6 | 0.5 | 0.4 | 0.3 | 0.2 | 0.1 | 0.0;
}

export async function loader({ request, context }: LoaderFunctionArgs) {
  const url = new URL(request.url);
  const guide = await context.resourceStore.getData();
  const entries: SitemapEntry[] = [
    { loc: url.origin, changefreq: "hourly", priority: 1 },
  ];

  for (const list of guide.metadata.lists ?? []) {
    entries.push({
      loc: `$url.origin}/${list.slug}`,
      changefreq: "hourly",
      priority: 0.8,
    });
  }

  for (const [resourceId, resource] of Object.entries(guide.value)) {
    const unknownResource = resource as unknown;
    entries.push({
      loc: `${url.origin}/resources/${resourceId}`,
      lastmod: (unknownResource as { updatedAt?: string }).updatedAt,
      changefreq: "weekly",
      priority: 0.5,
    });
  }

  const sitemap = `
        <?xml version="1.0" encoding="UTF-8"?>
        <urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9">
            ${entries
              .map((entry) =>
                `
				<url>
					<loc>${entry.loc}</loc>
					${entry.lastmod ? `<lastmod>${entry.lastmod}</lastmod>` : ""}
					${entry.changefreq ? `<changefreq>${entry.changefreq}</changefreq>` : ""}
					${entry.priority ? `<priority>${entry.priority}</priority>` : ""}
				</url>
			`.trim(),
              )
              .join("\n")}
        </urlset>
    `.trim();

  return new Response(sitemap, {
    headers: {
      "Content-Type": "application/xml",
      "Content-Length": String(new TextEncoder().encode(sitemap).length),
    },
  });
}
@jimbrig jimbrig added enhancement New feature or request feature New feature requests help wanted Extra attention is needed refactor Code refactoring and cleanup labels Oct 2, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request feature New feature requests help wanted Extra attention is needed refactor Code refactoring and cleanup
Projects
Status: Backlog
Development

No branches or pull requests

2 participants