Skip to content

Commit b5312ef

Browse files
authored
Add post on xml sitemaps (StubbornJava#42)
1 parent df97757 commit b5312ef

2 files changed

Lines changed: 41 additions & 24 deletions

File tree

‎stubbornjava-webapp/src/main/java/com/stubbornjava/webapp/post/PostData.java‎

Lines changed: 24 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -553,30 +553,30 @@ public class PostData {
553553
))
554554
.build()
555555
);
556-
// posts.add(PostRaw.builder()
557-
// .postId(880770018810625706L)
558-
// .title("Creating XML sitemaps in Java")
559-
// .metaDesc("Example XML sitemap in Java using sitemapgen4j to increase page exposure and SEO.")
560-
// .dateCreated(LocalDateTime.parse("2017-07-15T10:15:30"))
561-
// .dateUpdated(LocalDateTime.parse("2017-07-15T10:15:30"))
562-
// .javaLibs(Lists.newArrayList(JavaLib.Undertow, JavaLib.SitemapGen4j))
563-
// .tags(Lists.newArrayList(Tags.SEO))
564-
// .gitFileReferences(Lists.newArrayList(
565-
// FileReference.stubbornJava(
566-
// "model",
567-
// "stubbornjava-webapp/src/main/java/com/stubbornjava/webapp/themes/HtmlCssTheme.java")
568-
// , FileReference.stubbornJava(
569-
// "scraper",
570-
// "stubbornjava-webapp/src/main/java/com/stubbornjava/webapp/themes/WrapBootstrapScraper.java")
571-
// , FileReference.stubbornJava(
572-
// "service",
573-
// "stubbornjava-webapp/src/main/java/com/stubbornjava/webapp/themes/Themes.java")
574-
// , FileReference.stubbornJava(
575-
// "routes",
576-
// "stubbornjava-webapp/src/main/java/com/stubbornjava/webapp/themes/ThemeRoutes.java")
577-
// ))
578-
// .build()
579-
// );
556+
posts.add(PostRaw.builder()
557+
.postId(880770018810625706L)
558+
.title("Creating XML sitemaps in Java")
559+
.metaDesc("Example XML sitemap in Java using sitemapgen4j to increase page exposure and SEO.")
560+
.dateCreated(LocalDateTime.parse("2017-07-15T10:15:30"))
561+
.dateUpdated(LocalDateTime.parse("2017-07-15T10:15:30"))
562+
.javaLibs(Lists.newArrayList(JavaLib.Undertow, JavaLib.SitemapGen4j))
563+
.tags(Lists.newArrayList(Tags.SEO))
564+
.gitFileReferences(Lists.newArrayList(
565+
FileReference.stubbornJava(
566+
"inMemorySitemap",
567+
"stubbornjava-common/src/main/java/com/stubbornjava/common/seo/InMemorySitemap.java")
568+
, FileReference.stubbornJava(
569+
"routes",
570+
"stubbornjava-common/src/main/java/com/stubbornjava/common/seo/SitemapRoutes.java")
571+
, FileReference.stubbornJava(
572+
"sitemapGen",
573+
"stubbornjava-webapp/src/main/java/com/stubbornjava/webapp/StubbornJavaSitemapGenerator.java")
574+
, FileReference.stubbornJava(
575+
"robots",
576+
"stubbornjava-webapp/src/main/java/com/stubbornjava/webapp/PageRoutes.java")
577+
))
578+
.build()
579+
);
580580
}
581581

582582
public static List<PostRaw> getPosts() {
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
<p>XML sitemaps are a great way to expose your sites content to search engines especially when you do not have an internal or external linking structure built out yet. XML sitemaps in its simplest form is a directory of every unique url your website contains. This gives Google and other search engines a one stop shop for all pages they should index. XML sitemaps are restricted to 10MB or 50k links per sitemap but this limitation can be circumvented with sitemap indexes which link to multiple sitemaps. Sitemaps can also include additional metadata such as how frequently pages get updated or when was the last time a page was updated. After you design a site with <a href="/best-selling-html-css-themes-and-website-templates">HTML / CSS templates</a> make sure you include sitemaps to index the pages quicker.</p>
2+
3+
<h2 class="anchored">XML Sitemap with Java</h2>
4+
<p>The <a href="https://github.com/dfabulich/sitemapgen4j">SitemapGen4J</a> library gives a nice object model for generating all urls required to build out a sitemap. Most likely you will need to write code that can generate all possible urls for your website. Another alternative is to build a generic crawler that can build a sitemap for any website. It's not too difficult to build all of the custom urls so we create a method for each page type. We section them all out because we plan on making a sitemap index later.</p>
5+
{{> templates/src/widgets/code/code-snippet file=sitemapGen section=sitemapGen.sections.sitemapgen}}
6+
7+
<h2 class="anchored">XML Sitemap Index</h2>
8+
<p>SitemapGen4J was built to write the sitemaps to files on disk, however just want to keep ours in memory since it is fairly small. Unfortunately it looks like exposing the internal object model or additional rendering features was an after thought. There is an overriding for the individual sitemaps but not for the index. We should probably contribute an implementation or create a fully custom sitemap generator. Instead we need to build our own internal mapping. Sitemaps have a limit of 10MB or 50k urls per sitemap. This is why an index is needed.</p>
9+
{{> templates/src/widgets/code/code-snippet file=inMemorySitemap section=inMemorySitemap.sections.inMemorySitemap}}
10+
11+
<h2 class="anchored">XML Sitemap Routes</h2>
12+
<p>With an internal representation of the sitemap we now need to expose it in our <a href="/java-libraries/Undertow">Undertow web server</a>. A cool feature of the <a href="/posts/url-routing-with-undertow-embedded-http-server">RoutingHandler</a> is that it allows you to combine two <code>RoutingHandlers</code> with the <code>addAll</code> method.</p>
13+
{{> templates/src/widgets/code/code-snippet file=routes section=routes.sections.SitemapRoutes}}
14+
15+
<h2 class="anchored">Exposing the Sitemap</h2>
16+
<p>Ideally you can just expose a single sitemap index file that references all of the others. Since we had to hack around this a bit anohter option is to include all of the sitemap files in our robots.txt.</p>
17+
{{> templates/src/widgets/code/code-snippet file=robots section=robots.sections.robots}}

0 commit comments

Comments
 (0)