Skip to content

Commit 465e2b0

Browse files
committed
fix: add trailing slashes to sitemap URLs
Ensure sitemap loc entries use Cloudflare's canonical trailing-slash page URLs so crawlers do not pay for redirect hops on every route.
1 parent bf9ee3f commit 465e2b0

2 files changed

Lines changed: 11 additions & 3 deletions

File tree

__tests__/Sitemap_.test.res

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,10 +12,10 @@ test("renders sorted unique sitemap URLs with a normalized base URL", async () =
1212
<loc>https://preview.example.com/</loc>
1313
</url>
1414
<url>
15-
<loc>https://preview.example.com/blog</loc>
15+
<loc>https://preview.example.com/blog/</loc>
1616
</url>
1717
<url>
18-
<loc>https://preview.example.com/docs/manual/introduction</loc>
18+
<loc>https://preview.example.com/docs/manual/introduction/</loc>
1919
</url>
2020
</urlset>
2121
`)

src/common/Sitemap.res

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,8 +49,16 @@ let normalizePaths = paths =>
4949
}
5050
})
5151

52+
let withTrailingSlash = path => {
53+
if path === "/" || path->String.endsWith("/") {
54+
path
55+
} else {
56+
path ++ "/"
57+
}
58+
}
59+
5260
let renderUrl = (~baseUrl, path) => {
53-
let loc = baseUrl ++ path
61+
let loc = baseUrl ++ path->withTrailingSlash
5462

5563
` <url>
5664
<loc>${loc->escapeXml}</loc>

0 commit comments

Comments
 (0)