-
Notifications
You must be signed in to change notification settings - Fork 92
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
sitemap index for sitemap size > 10MB #17
Comments
I ran into the same problem and solved it by writing the sitemap contents to strings and check the length of these strings. If any of the generated lists exceeds 10Mb then I decreased the number of max URLs and started all over again. This might take up some time but if you have a chance to test locally with your production data you can find a better starting value for private final Map<String, Pair<Date, String>> sitemaps = new HashMap<>();
private final AtomicInteger maxUrls = new AtomicInteger(50000);
private void generateGoogleImageSitemapContents(String fileNamePrefix, Function<Integer, GoogleImageSitemapGenerator> sitemapCreator) {
Predicate<GoogleImageSitemapGenerator> exceeds10Mb = googleImageSitemapGenerator -> googleImageSitemapGenerator.writeAsStrings()
.stream()
.anyMatch(list -> list.getBytes(US_ASCII).length > 1e+7);
GoogleImageSitemapGenerator generator = sitemapCreator.apply(maxUrls.get());
if (generator == null) {
return;
}
while (exceeds10Mb.test(generator) && maxUrls.get() > 0) {
generator = sitemapCreator.apply(maxUrls.addAndGet(-maxUrls.get() / 2));
}
// In theory now all lists are smaller than 10Mb lets store them for later
AtomicInteger counter = new AtomicInteger(0);
generator.writeAsStrings()
.forEach(sitemapContent -> sitemaps.put(fileNamePrefix + counter.getAndIncrement() + ".xml", Pair.of(new Date(), sitemapContent)));
} And later you can use this method like this generateGoogleImageSitemapContents("changes_daily_with_images_", maxUrls -> {
try {
GoogleImageSitemapGenerator imageGenerator = GoogleImageSitemapGenerator.builder(baseUrl, null)
.dateFormat(new W3CDateFormat(MINUTE))
.maxUrls(maxUrls)
.build();
addProducts(imageGenerator);
addCategories(imageGenerator);
return imageGenerator;
} catch (MalformedURLException e) {
throw new RuntimeException(e);
}
}); In my case I store the sitemaps in-memory and deliver them on request. The Pair of date and sitemap content is used to add a |
is possible to force the creation for a sitemap if the current sitemap file size is greater then 10MB?
The text was updated successfully, but these errors were encountered: