Customize the sitemap
Since version 3.4
Introduction
This guide provides step-by-step instructions on how to customize the sitemap for your Front-Commerce application. Customizing your sitemap can help improve SEO by ensuring search engines can easily crawl and index your site's content.
Static Pages Customization
getSitemapEntries
Function
To customize the sitemap for static pages, use the getSitemapEntries
function
in the SEO handle. This function allows you to add custom URLs, set the last
modification date, change frequency, and priority for each URL.
Example:
import { createSeoHandle } from "@front-commerce/remix/seo";
export const handle = createSeoHandle({
getSitemapEntries: ({ request, app }) => [
{
url: "/blog",
lastmod: new Date(),
changefreq: "daily",
priority: 0.5,
images: ["/blog-image.jpg"],
},
],
});
Ensure server-only logic in getSitemapEntries
is implemented using
vite-env-only
to prevent leaking into client-side code.
Dynamic Pages Customization
Dynamic pages can also be customized using getSitemapEntries
or a custom
sitemapFetcher
.
getSitemapEntries
Function
import { createSeoHandle } from "@front-commerce/remix/seo";
import { serverOnly$ } from "vite-env-only";
export const handle = createSeoHandle({
getSitemapEntries: serverOnly(async ({ request, app }) => {
const posts = await fetch(
"https://jsonplaceholder.typicode.com/posts"
).then((res) => res.json());
return posts.map((post) => ({
url: `/todo/${post.id}`,
}));
}),
});
sitemapFetcher
Option
Refer to Registering Sitemap Fetcher for custom fetcher registration.
import { createSeoHandle } from "@front-commerce/remix/seo";
export const handle = createSeoHandle({
sitemapFetcher: "todoSitemapFetcher",
});
Creating and Registering a Custom Fetcher
Create a custom sitemap fetcher and register it in your extension definition.
Creating a Fetcher
import { createSitemapFetcher } from "@front-commerce/core";
export default createSitemapFetcher(async () => {
const posts = await fetch("https://jsonplaceholder.typicode.com/posts").then(
(res) => res.json()
);
return posts.map((post) => ({
url: `/todo/${post.id}`,
}));
});
Registering a Fetcher
import { defineExtension } from "@front-commerce/core";
export default defineExtension({
name: "acme-extension",
meta: import.meta,
routes: import.meta.url,
unstable_lifecycleHooks: {
onServerServicesInit: async (services, request, config) => {
services.DI.register("acme", createAcmeDI(config.acme));
services.Sitemap.registerFetcher(
"todoSitemapFetcher",
() => import("./sitemap/todo.ts")
);
},
},
});
Override existing fetchers using services.Sitemap.overrideFetcher
. We are
still working on an API to extend existing fetchers.
Opting Out of Sitemap Generation
Static pages are automatically included in the sitemap. To exclude a page from
the sitemap, return null
or an empty array from getSitemapEntries
.
import { createSeoHandle } from "@front-commerce/remix/seo";
export const handle = createSeoHandle({
getSitemapEntries: () => null,
});