From 44445653412659075b9687f9986d280e86d7f55b Mon Sep 17 00:00:00 2001 From: Ryan Wang Date: Mon, 27 Apr 2026 16:52:45 +0800 Subject: [PATCH] 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 --- .../java/run/halo/sitemap/ChangeFreqEnum.java | 35 ---------- .../java/run/halo/sitemap/SitemapBuilder.java | 10 +-- .../java/run/halo/sitemap/SitemapEntry.java | 5 +- .../halo/sitemap/SitemapGeneratorOptions.java | 66 +------------------ .../run/halo/sitemap/SitemapBuilderTest.java | 10 +-- .../sitemap/SitemapGeneratorOptionsTest.java | 17 ++--- 6 files changed, 11 insertions(+), 132 deletions(-) delete mode 100644 src/main/java/run/halo/sitemap/ChangeFreqEnum.java diff --git a/src/main/java/run/halo/sitemap/ChangeFreqEnum.java b/src/main/java/run/halo/sitemap/ChangeFreqEnum.java deleted file mode 100644 index f76a79d..0000000 --- a/src/main/java/run/halo/sitemap/ChangeFreqEnum.java +++ /dev/null @@ -1,35 +0,0 @@ -package run.halo.sitemap; - -/** - * How frequently the page is likely to change. - * This value provides general information to search engines and may not correlate exactly to - * how often they crawl the page. Valid values are: - * - * The value "always" should be used to describe documents that change each time they are accessed. - * The value "never" should be used to describe archived URLs. - * Please note that the value of this tag is considered a hint and not a command. - * Even though search engine crawlers may consider this information when making decisions, they may - * crawl pages marked "hourly" less frequently than that, and they may crawl pages marked "yearly" - * more frequently than that. Crawlers may periodically crawl pages marked "never" so that they - * can handle unexpected changes to those pages. - * - * @author guqing - * @since 1.0.0 - */ -public enum ChangeFreqEnum { - ALWAYS, - HOURLY, - DAILY, - WEEKLY, - MONTHLY, - YEARLY, - NEVER -} diff --git a/src/main/java/run/halo/sitemap/SitemapBuilder.java b/src/main/java/run/halo/sitemap/SitemapBuilder.java index 4771e94..12b7e38 100644 --- a/src/main/java/run/halo/sitemap/SitemapBuilder.java +++ b/src/main/java/run/halo/sitemap/SitemapBuilder.java @@ -34,19 +34,11 @@ public String withXMLTemplate(String content) { * @see el_sequence */ public LinkedHashMap normalizeSitemapEntry(SitemapEntry sitemapEntry) { - LinkedHashMap sitemapEntryMap = new LinkedHashMap<>(4, 1); - // Return keys in following order + LinkedHashMap sitemapEntryMap = new LinkedHashMap<>(2, 1); sitemapEntryMap.put("loc", sitemapEntry.getLoc()); if (StringUtils.isNotBlank(sitemapEntry.getLastmod())) { sitemapEntryMap.put("lastmod", sitemapEntry.getLastmod()); } - ChangeFreqEnum changefreq = sitemapEntry.getChangefreq(); - if (changefreq != null) { - sitemapEntryMap.put("changefreq", changefreq.name().toLowerCase()); - } - if (sitemapEntry.getPriority() != null) { - sitemapEntryMap.put("priority", sitemapEntry.getPriority().toString()); - } return sitemapEntryMap; } diff --git a/src/main/java/run/halo/sitemap/SitemapEntry.java b/src/main/java/run/halo/sitemap/SitemapEntry.java index 960cd51..130a843 100644 --- a/src/main/java/run/halo/sitemap/SitemapEntry.java +++ b/src/main/java/run/halo/sitemap/SitemapEntry.java @@ -15,11 +15,8 @@ public class SitemapEntry { *

Parent tag for each URL entry. The remaining tags are children of this tag.

* required. */ + @NonNull private String loc; private String lastmod; - - private ChangeFreqEnum changefreq; - - private Double priority; } diff --git a/src/main/java/run/halo/sitemap/SitemapGeneratorOptions.java b/src/main/java/run/halo/sitemap/SitemapGeneratorOptions.java index 6c87f34..c529578 100644 --- a/src/main/java/run/halo/sitemap/SitemapGeneratorOptions.java +++ b/src/main/java/run/halo/sitemap/SitemapGeneratorOptions.java @@ -3,7 +3,6 @@ import java.net.URI; import java.net.URISyntaxException; import java.net.URL; -import java.time.Instant; import java.time.format.DateTimeFormatter; import java.util.Set; import lombok.Builder; @@ -26,70 +25,17 @@ public class SitemapGeneratorOptions { @NonNull private URL siteUrl; - @Builder.Default - private String fileNamePrefix = "sitemap"; - - @Builder.Default - private boolean allowEmptySitemap = false; - - @Builder.Default - private boolean allowMultipleSitemaps = true; - @Builder.Default private DateTimeFormatter dateTimeFormatter = W3cDatetimeFormat.SECOND_FORMATTER; - /** - * Split large sitemap into multiple files by specifying sitemap size. Default 5000. - */ - @Builder.Default - private int sitemapSize = 5000; - - @Builder.Default - private boolean autoValidate = false; - - @Builder.Default - private boolean gzip = false; - - @Builder.Default - private ChangeFreqEnum changefreq = ChangeFreqEnum.DAILY; - - /** - * How to assign sitemap priorities: - *
-     * 1.0-0.8
-     * Homepage, product information, landing pages.
-     *
-     * 0.7-0.4
-     * News articles, some weather services, blog posts, category pages, pages that no site would be complete without.
-     *
-     * 0.3-0.0
-     * FAQs, outdated info, old press releases, completely static pages that are still relevant enough to keep from deleting entirely.
-     * 
- * - * @see - * xml-sitemap-priority-changefreq - * @see - * xml-sitemap-priority-changefreq - */ - @Builder.Default - private double priority = 0.7; - /** *

Array of relative paths (wildcard pattern supported) to exclude from listing on sitemap * .xml or sitemap-*.xml.

* *

e.g.: ['/page-0', '/page-*', '/private/*'].

- * Apart from this option next-sitemap also offers a custom transform option which could be - * used to exclude urls that match specific patterns */ private Set exclude; - /** - * Generate index sitemaps. Default true. - */ - @Builder.Default - private boolean generateIndexSitemap = true; - public SitemapEntry transform(UrlEntryMeta context) { String escapedUrl = UrlUtils.escapeSitemapUrl(context.getUrl()); String loc = UrlUtils.toURI(escapedUrl).normalize().toASCIIString(); @@ -97,21 +43,11 @@ public SitemapEntry transform(UrlEntryMeta context) { loc = getSiteUri().resolve(escapedUrl).normalize().toASCIIString(); } - var builder = SitemapEntry.builder() - .loc(loc) - .changefreq(changefreq); - - if (context.getPriority() != null) { - builder.priority(context.getPriority()); - } else { - builder.priority(priority); - } + var builder = SitemapEntry.builder().loc(loc); if (context.getLastModifiedTime() != null) { builder.lastmod( W3cDatetimeFormat.format(context.getLastModifiedTime(), dateTimeFormatter)); - } else { - builder.lastmod(W3cDatetimeFormat.format(Instant.now(), dateTimeFormatter)); } return builder.build(); diff --git a/src/test/java/run/halo/sitemap/SitemapBuilderTest.java b/src/test/java/run/halo/sitemap/SitemapBuilderTest.java index dc05fcf..7756629 100644 --- a/src/test/java/run/halo/sitemap/SitemapBuilderTest.java +++ b/src/test/java/run/halo/sitemap/SitemapBuilderTest.java @@ -20,14 +20,10 @@ void buildSitemapXml() { entries.add(SitemapEntry.builder() .loc("https://halo.run/about") .lastmod("2022-11-12T13:57:43.898+0800") - .changefreq(ChangeFreqEnum.DAILY) - .priority(0.7) .build()); entries.add(SitemapEntry.builder() .loc("https://halo.run/categories") .lastmod("2022-11-12T13:57:43.898+0800") - .changefreq(ChangeFreqEnum.DAILY) - .priority(0.7) .build()); String s = new SitemapBuilder().buildSitemapXml(entries); assertEquals(""" @@ -41,16 +37,12 @@ void buildSitemapXml() { xmlns:video="http://www.google.com/schemas/sitemap-video/1.1"> https://halo.run/about 2022-11-12T13:57:43.898+0800 - daily - 0.7 https://halo.run/categories 2022-11-12T13:57:43.898+0800 - daily - 0.7 """, s); } -} \ No newline at end of file +} diff --git a/src/test/java/run/halo/sitemap/SitemapGeneratorOptionsTest.java b/src/test/java/run/halo/sitemap/SitemapGeneratorOptionsTest.java index 22ec749..54e9da2 100644 --- a/src/test/java/run/halo/sitemap/SitemapGeneratorOptionsTest.java +++ b/src/test/java/run/halo/sitemap/SitemapGeneratorOptionsTest.java @@ -1,6 +1,7 @@ package run.halo.sitemap; import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertNull; import java.net.MalformedURLException; import java.net.URL; @@ -19,27 +20,23 @@ void transform() throws MalformedURLException { SitemapGeneratorOptions options = SitemapGeneratorOptions.builder() .siteUrl(new URL("https://halo.run")) .build(); + SitemapEntry entry = options.transform(new UrlEntryMeta("/about")); assertEquals("https://halo.run/about", entry.getLoc()); - assertEquals(ChangeFreqEnum.DAILY, entry.getChangefreq()); - assertEquals(0.7, entry.getPriority()); + assertNull(entry.getLastmod()); entry = options.transform(new UrlEntryMeta("/archives")); assertEquals("https://halo.run/archives", entry.getLoc()); - assertEquals(ChangeFreqEnum.DAILY, entry.getChangefreq()); - assertEquals(0.7, entry.getPriority()); + assertNull(entry.getLastmod()); entry = options.transform(new UrlEntryMeta("/categories/ümlat/>&>中")); assertEquals("https://halo.run/categories/%C3%BCmlat/>&>%E4%B8%AD", entry.getLoc()); - assertEquals(ChangeFreqEnum.DAILY, entry.getChangefreq()); - assertEquals(0.7, entry.getPriority()); + assertNull(entry.getLastmod()); entry = options.transform(new UrlEntryMeta("https://guqing.xyz/hello-中国<>&")); assertEquals("https://guqing.xyz/hello-%E4%B8%AD%E5%9B%BD<>&", entry.getLoc()); - - assertEquals(ChangeFreqEnum.DAILY, entry.getChangefreq()); - assertEquals(0.7, entry.getPriority()); + assertNull(entry.getLastmod()); } -} \ No newline at end of file +}