Sitemap Trailing Slashes
When using netlify and cloudflare pages, trailing slashes are automatically added to the end of page urls that are named "index.html" and are in a sub directory.
i.e.
output/blog/index.html -> <loc>https://baseurl.com/blog</loc>
This is bad for SEO as visiting site.com/blog will result in a redirect to site.com/blog/
To fix this, I made my own sitemap generator that runs after all of my pages are generated into a local output folder.
Would it be possible to have the sitemap generation add trailing slashed to urls where applicable?
i.e.
output/blog/index.html -> <loc>https://baseurl.com/blog/</loc>
Hey, sorry for the delay. Let's get this sorted.
So, you've built your own sitemap generator that ensures trailing slashes, and you'd like to integrate it into BlazorStatic?
I assume the relevant code is here:
https://github.com/BlazorStatic/BlazorStatic/blob/9a70e33f0d8b269ffcb34c68e8197baf1149c90e/src/Services/BlazorStaticService.cs#L126
I don't have much SEO experience—do you think always enforcing trailing slashes would be generally beneficial, or is this just a specific use case? I'm wondering whether we should make it the default behavior or add a setting so users can configure it themselves.
So I should clarify here, the code I wrote was not within Blazor Static, but I would be glad to hop in and change the sitemap generation there to accommodate.
I also don't know that the solution to add the trailing slashes would be beneficial in every use case to be completely honest. I don't know if systems outside of Cloudflare Pages and Netlify handle these things differently.
As for Cloudflare and Netlify, adding a trailing slash to any index.html file that is within a subdirectory would be the end goal here. So the home page at output/index.html would have no trailing slash. A main blog page at output/blog/index.html would need a trailing slash.
We also wouldn't want a trailing slash for pages in subdirectories that are named anything other than index i.e. output/blog/blogpost.html.
So for recap: output/index.html (no trailing slash) output/blog/post.html (no trailing slash) output/blog/index.html (needs trailing slash)
Maybe someone else could chime in if they know of another case where you wouldn't want the sitemap generated this way or if I'm just generally missing any scenarios here?
It may also be prudent, if no one else chimes in, that some kind of flag is added to either generate the sitemap with these changes or without them per end user input.
So what about exposing a Func<string> where the string would be the page.Url and you would have the option to implement your own
https://github.com/BlazorStatic/BlazorStatic/blob/9a70e33f0d8b269ffcb34c68e8197baf1149c90e/src/Services/BlazorStaticService.cs#L150
?
That is general enough and doesn't really hurt anyone?
I guess with such approach you can even reuse your own sitemap generator.
Really, for my use case, just having access during the initial build/startup to the list of pages to be generated would be enough I could implement my own and that wouldn't have any impact on anyone else.
This way I can go just make my own foreach loop to go through each page / route to be generated and build out the sitemap before things start getting added to the output folder. #
it seems the AddBeforeFilesGenerationAction is the right place.
builder.Services.AddBlazorStaticService(opt => {
opt.AddBeforeFilesGenerationAction(() => {
foreach(var page in opt.PagesToGenerate)
{
Console.WriteLine($"Page to be generated. outpufile:{page.OutputFile}, url:{page.Url}");
}
return Task.CompletedTask;
});
is that so?
To be honest, I wasn't even aware of it's existence, I will need to do some testing with it, but that sounds like what I would need.
I have added my own custom code using AddBeforeFilesGenerationAction, very happy to have learned of this! Everything is working as expected! Thank you for the help @tesar-tech , if anyone would like to see the code I have for this, I'm happy to share it.
very happy to have learned of this!
We really need to step up with the docs or examples. It would be cool do generate documentation for BlazorStatic with BlazorStatic.
I'm happy to share it.
Please do, resources are scarce.