Skip to content

Commit 4444565

Browse files
committed
fix: remove changefreq/priority and fix lastmod per Google/Bing guidelines
Google and Bing both ignore changefreq and priority fields in sitemaps. Remove these fields from SitemapEntry, SitemapGeneratorOptions, and SitemapBuilder to avoid generating misleading metadata. Also remove the Instant.now() fallback for lastmod in transform(): only set lastmod when UrlEntryMeta actually carries a real modification time, preventing inaccurate timestamps that could mislead crawlers. Made-with: Cursor
1 parent 00fe12c commit 4444565

6 files changed

Lines changed: 11 additions & 132 deletions

File tree

src/main/java/run/halo/sitemap/ChangeFreqEnum.java

Lines changed: 0 additions & 35 deletions
This file was deleted.

src/main/java/run/halo/sitemap/SitemapBuilder.java

Lines changed: 1 addition & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -34,19 +34,11 @@ public String withXMLTemplate(String content) {
3434
* @see <a href="https://www.w3schools.com/xml/el_sequence.asp">el_sequence</a>
3535
*/
3636
public LinkedHashMap<String, String> normalizeSitemapEntry(SitemapEntry sitemapEntry) {
37-
LinkedHashMap<String, String> sitemapEntryMap = new LinkedHashMap<>(4, 1);
38-
// Return keys in following order
37+
LinkedHashMap<String, String> sitemapEntryMap = new LinkedHashMap<>(2, 1);
3938
sitemapEntryMap.put("loc", sitemapEntry.getLoc());
4039
if (StringUtils.isNotBlank(sitemapEntry.getLastmod())) {
4140
sitemapEntryMap.put("lastmod", sitemapEntry.getLastmod());
4241
}
43-
ChangeFreqEnum changefreq = sitemapEntry.getChangefreq();
44-
if (changefreq != null) {
45-
sitemapEntryMap.put("changefreq", changefreq.name().toLowerCase());
46-
}
47-
if (sitemapEntry.getPriority() != null) {
48-
sitemapEntryMap.put("priority", sitemapEntry.getPriority().toString());
49-
}
5042
return sitemapEntryMap;
5143
}
5244

src/main/java/run/halo/sitemap/SitemapEntry.java

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -15,11 +15,8 @@ public class SitemapEntry {
1515
* <p>Parent tag for each URL entry. The remaining tags are children of this tag.</p>
1616
* required.
1717
*/
18+
@NonNull
1819
private String loc;
1920

2021
private String lastmod;
21-
22-
private ChangeFreqEnum changefreq;
23-
24-
private Double priority;
2522
}

src/main/java/run/halo/sitemap/SitemapGeneratorOptions.java

Lines changed: 1 addition & 65 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@
33
import java.net.URI;
44
import java.net.URISyntaxException;
55
import java.net.URL;
6-
import java.time.Instant;
76
import java.time.format.DateTimeFormatter;
87
import java.util.Set;
98
import lombok.Builder;
@@ -26,92 +25,29 @@ public class SitemapGeneratorOptions {
2625
@NonNull
2726
private URL siteUrl;
2827

29-
@Builder.Default
30-
private String fileNamePrefix = "sitemap";
31-
32-
@Builder.Default
33-
private boolean allowEmptySitemap = false;
34-
35-
@Builder.Default
36-
private boolean allowMultipleSitemaps = true;
37-
3828
@Builder.Default
3929
private DateTimeFormatter dateTimeFormatter = W3cDatetimeFormat.SECOND_FORMATTER;
4030

41-
/**
42-
* Split large sitemap into multiple files by specifying sitemap size. Default 5000.
43-
*/
44-
@Builder.Default
45-
private int sitemapSize = 5000;
46-
47-
@Builder.Default
48-
private boolean autoValidate = false;
49-
50-
@Builder.Default
51-
private boolean gzip = false;
52-
53-
@Builder.Default
54-
private ChangeFreqEnum changefreq = ChangeFreqEnum.DAILY;
55-
56-
/**
57-
* How to assign sitemap priorities:
58-
* <pre>
59-
* 1.0-0.8
60-
* Homepage, product information, landing pages.
61-
*
62-
* 0.7-0.4
63-
* News articles, some weather services, blog posts, category pages, pages that no site would be complete without.
64-
*
65-
* 0.3-0.0
66-
* FAQs, outdated info, old press releases, completely static pages that are still relevant enough to keep from deleting entirely.
67-
* </pre>
68-
*
69-
* @see
70-
* <a href="https://www.contentpowered.com/blog/xml-sitemap-priority-changefreq/">xml-sitemap-priority-changefreq</a>
71-
* @see
72-
* <a href="https://slickplan.com/blog/xml-sitemap-priority-changefreq">xml-sitemap-priority-changefreq</a>
73-
*/
74-
@Builder.Default
75-
private double priority = 0.7;
76-
7731
/**
7832
* <p>Array of relative paths (wildcard pattern supported) to exclude from listing on sitemap
7933
* .xml or sitemap-*.xml.</p>
8034
*
8135
* <p>e.g.: ['/page-0', '/page-*', '/private/*'].</p>
82-
* Apart from this option next-sitemap also offers a custom transform option which could be
83-
* used to exclude urls that match specific patterns
8436
*/
8537
private Set<String> exclude;
8638

87-
/**
88-
* Generate index sitemaps. Default true.
89-
*/
90-
@Builder.Default
91-
private boolean generateIndexSitemap = true;
92-
9339
public SitemapEntry transform(UrlEntryMeta context) {
9440
String escapedUrl = UrlUtils.escapeSitemapUrl(context.getUrl());
9541
String loc = UrlUtils.toURI(escapedUrl).normalize().toASCIIString();
9642
if (!PathUtils.isAbsoluteUri(loc)) {
9743
loc = getSiteUri().resolve(escapedUrl).normalize().toASCIIString();
9844
}
9945

100-
var builder = SitemapEntry.builder()
101-
.loc(loc)
102-
.changefreq(changefreq);
103-
104-
if (context.getPriority() != null) {
105-
builder.priority(context.getPriority());
106-
} else {
107-
builder.priority(priority);
108-
}
46+
var builder = SitemapEntry.builder().loc(loc);
10947

11048
if (context.getLastModifiedTime() != null) {
11149
builder.lastmod(
11250
W3cDatetimeFormat.format(context.getLastModifiedTime(), dateTimeFormatter));
113-
} else {
114-
builder.lastmod(W3cDatetimeFormat.format(Instant.now(), dateTimeFormatter));
11551
}
11652

11753
return builder.build();

src/test/java/run/halo/sitemap/SitemapBuilderTest.java

Lines changed: 1 addition & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -20,14 +20,10 @@ void buildSitemapXml() {
2020
entries.add(SitemapEntry.builder()
2121
.loc("https://halo.run/about")
2222
.lastmod("2022-11-12T13:57:43.898+0800")
23-
.changefreq(ChangeFreqEnum.DAILY)
24-
.priority(0.7)
2523
.build());
2624
entries.add(SitemapEntry.builder()
2725
.loc("https://halo.run/categories")
2826
.lastmod("2022-11-12T13:57:43.898+0800")
29-
.changefreq(ChangeFreqEnum.DAILY)
30-
.priority(0.7)
3127
.build());
3228
String s = new SitemapBuilder().buildSitemapXml(entries);
3329
assertEquals("""
@@ -41,16 +37,12 @@ void buildSitemapXml() {
4137
xmlns:video="http://www.google.com/schemas/sitemap-video/1.1">
4238
<url><loc>https://halo.run/about</loc>
4339
<lastmod>2022-11-12T13:57:43.898+0800</lastmod>
44-
<changefreq>daily</changefreq>
45-
<priority>0.7</priority>
4640
</url>
4741
<url><loc>https://halo.run/categories</loc>
4842
<lastmod>2022-11-12T13:57:43.898+0800</lastmod>
49-
<changefreq>daily</changefreq>
50-
<priority>0.7</priority>
5143
</url>
5244
5345
</urlset>
5446
""", s);
5547
}
56-
}
48+
}

src/test/java/run/halo/sitemap/SitemapGeneratorOptionsTest.java

Lines changed: 7 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package run.halo.sitemap;
22

33
import static org.junit.jupiter.api.Assertions.assertEquals;
4+
import static org.junit.jupiter.api.Assertions.assertNull;
45

56
import java.net.MalformedURLException;
67
import java.net.URL;
@@ -19,27 +20,23 @@ void transform() throws MalformedURLException {
1920
SitemapGeneratorOptions options = SitemapGeneratorOptions.builder()
2021
.siteUrl(new URL("https://halo.run"))
2122
.build();
23+
2224
SitemapEntry entry = options.transform(new UrlEntryMeta("/about"));
2325
assertEquals("https://halo.run/about", entry.getLoc());
24-
assertEquals(ChangeFreqEnum.DAILY, entry.getChangefreq());
25-
assertEquals(0.7, entry.getPriority());
26+
assertNull(entry.getLastmod());
2627

2728
entry = options.transform(new UrlEntryMeta("/archives"));
2829
assertEquals("https://halo.run/archives", entry.getLoc());
29-
assertEquals(ChangeFreqEnum.DAILY, entry.getChangefreq());
30-
assertEquals(0.7, entry.getPriority());
30+
assertNull(entry.getLastmod());
3131

3232
entry = options.transform(new UrlEntryMeta("/categories/ümlat/>&>中"));
3333
assertEquals("https://halo.run/categories/%C3%BCmlat/&gt;&amp;&gt;%E4%B8%AD",
3434
entry.getLoc());
35-
assertEquals(ChangeFreqEnum.DAILY, entry.getChangefreq());
36-
assertEquals(0.7, entry.getPriority());
35+
assertNull(entry.getLastmod());
3736

3837
entry = options.transform(new UrlEntryMeta("https://guqing.xyz/hello-中国<>&"));
3938
assertEquals("https://guqing.xyz/hello-%E4%B8%AD%E5%9B%BD&lt;&gt;&amp;",
4039
entry.getLoc());
41-
42-
assertEquals(ChangeFreqEnum.DAILY, entry.getChangefreq());
43-
assertEquals(0.7, entry.getPriority());
40+
assertNull(entry.getLastmod());
4441
}
45-
}
42+
}

0 commit comments

Comments
 (0)