Problem/Motivation

The sitemap isn't cached. Make it so.

Proposed resolution

Remaining tasks

User interface changes

API changes

Data model changes

Release notes snippet

Command icon Show commands

Start within a Git clone of the project using the version control instructions.

Or, if you do not have SSH keys set up on git.drupalcode.org:

Comments

heddn created an issue. See original summary.

heddn’s picture

StatusFileSize
new3.6 KB
heddn’s picture

Status: Active » Needs review
gbyte’s picture

Status: Needs review » Active

Why would you want to cache it though if its content is fetched entirely from the DB anyways with very little PHP overhead? Have you done performance testing comparing the cached result with the non-cahced result?

See #3001470: Remove caching as now all sitemap content is stored in database.

heddn’s picture

A couple thoughts on caching. If it is cacheable, then the additional cache layers like redis, memcache, etc can help. Right now I have to rely on the (moderately) slower DB query. Not to mention I can't use HTTP layer caching like 304 Not Modified from Varnish, Fastly, Akamai, CloudFlare, etc.

heddn’s picture

Status: Active » Needs review
StatusFileSize
new3.28 KB
new4.6 KB
gbyte’s picture

Status: Needs review » Needs work

Thanks for the patch. Let's see how much popularity this issue will get.

If what you are doing in SitemapGeneratorBase::purgeSitemapVariants() is really necessary, it should go into SitmemapGeneratorBase::invalidateCache() instead and should use the API for getting variants instead of the raw DB query. Maybe something like SimplesitemapManage::getSitemapVariants()?

In SimplesitemapController::getSitemap, please note SimplesitemapManager::DEFAULT_SITEMAP_GENERATOR is the default sitemap generator and not sitemap variant. There is no default sitemap variant constant, as all variants (sitemap instances) can be deleted. Variant 'default' gets created on install. But generally I don't like that code there and I am sure there is a better way of getting the default sitemap variant somewhere else in your code. You should use Simplesitemap::getVairants(FALSE) to get the currently set variant.

heddn’s picture

Status: Needs work » Needs review
StatusFileSize
new5.08 KB
new3.19 KB

I think this addresses your concerns.

  1. SitemapGeneratorBase: We're calling these things statically, so we have to call \Drupal::service() directly. And if if someone doesn't pass in the variant name, it sounds like we want to flush all the variations.
  2. SimplesitemapController: This supports the default route and all others are named. Not having that config item set is a bit of an odd/borken situation, no?
dww’s picture

StatusFileSize
new5.14 KB

Thanks for this issue and patch! I need the same on a site where I'm using simple_sitemap.

A) I marked #3174147: Allow responses to be browser-cached duplicate with this. Please credit @rmontero here, since they worked on a fairly similar fix.

B) The patch in #8 no longer applies cleanly to the 8.x-3.x branch. Simple re-roll here. No interdiff, since the only change is patch context and offsets.

C) I haven't closely reviewed or tested this, but that's coming soon. ;) Stay tuned...

Thanks again,
-Derek

recrit’s picture

Thanks @heddn and @dww! The patch worked for me.
The sitemap.xml is static until it is regenerated so it's a perfect candidate to cache in order to take that load off of the web server.

gbyte’s picture

Status: Needs review » Needs work

The sitemap.xml is static until it is regenerated so it's a perfect candidate to cache in order to take that load off of the web server.

Partly yes, but don't forget it just gets fetched with a simple SQL select query, so the load shouldn't be substantial.

heddn’s picture

This was one of the things that came up in new relic monitoring. So saying this is a simple SQL query... something caused it to bubble to the top of performance optimizations. And there are now 3 different folks who have stumbled on the desire for this feature. Even if you don't see the need for it on your sites, perhaps you could consider that its a fairly simple change (with test coverage) that would benefit others.

gbyte’s picture

@heddn I was just trying to lower @recrit's expectations regarding the performance gains. I do intend to merge this since the points you made in #5 were convincing. Needs rerolling though, there have been substantial changes in regards to generation performance.

BTW I don't appreciate the notion of me implementing features according to what 'my sites' need. I've spent a considerable amount of time making this module work for the Drupal community which also sometimes means denying feature requests. But you will certainly know that.

bernardm28’s picture

Caching this module would be great! Tks for the patch. We hit the threshold two months or so ago, where one can notice a slowdown for it.

fago’s picture

I've just noted that sitemaps are not cached. I'd agree that this is a desired feature.

>BTW I don't appreciate the notion of me implementing features according to what 'my sites' need. I've spent a considerable amount of time making this module work for the Drupal community which also sometimes means denying feature requests. But you will certainly know that.

Highly appreciated, thank you for all your hard work!

heddn’s picture

Status: Needs work » Needs review

Yes, thank you for your fine work on this module!

gbyte’s picture

Version: 8.x-3.x-dev » 4.x-dev
Status: Needs review » Needs work

3.x is at feature freeze and 4.x has been deemed good enough for non-api-dependant use cases. Let's get this into 4.x before the stable release!

vasike made their first commit to this issue’s fork.

vasike’s picture

Re-rolled the patch for 3.x ... as it seems not working anymore with the latest dev ...

And create new MR https://git.drupalcode.org/issue/simple_sitemap-3090254/-/merge_requests/1

So it can be used for 3.x version ... till upgrade

todo ... 4.x MR ... to be continued

fago’s picture

the main hunk

-    return new Response($output, Response::HTTP_OK, [
+    $response = new CacheableResponse($output, Response::HTTP_OK, [
       'Content-type' => 'application/xml; charset=utf-8',
       'X-Robots-Tag' => 'noindex, follow',
     ]);
+    $response->getCacheableMetadata()
+      ->addCacheTags(Cache::buildTags('simple_sitemap', $variant))
+      ->addCacheContexts(['url.query_args']);

seems to be missing from 4.x ?

ldavidsp’s picture

StatusFileSize
new4.18 KB
new5.27 KB

Support for version 4

gbyte’s picture

Status: Needs work » Needs review

Status: Needs review » Needs work

The last submitted patch, 22: 3090254-22.patch, failed testing. View results
- codesniffer_fixes.patch Interdiff of automated coding standards fixes only.

gngn’s picture

My sitemap.xml contains 10,000+ nodes - so I am really interested in caching it.

gbyte’s picture

My sitemap.xml contains 10,000+ nodes - so I am really interested in caching it.

The sitemap is already cached - every sitemap is one record in the database. Thus caching the sitemap via regular caching methods would have little impact. The amount of links has little to do with the retrieval time and more to do with downloading the webpage when you visit /sitemap.xml.

There would be some impact if we implemented this in conjunction with other caching layers, see this comment. I am not opposed to merge this, just waiting for the community to step up as I am working on features that are more important at the moment (in my opinion).

gngn’s picture

@gbyte - ah, thanks.

yonailo’s picture

I don't know if this patch would fix our issue, but currently we are forced to exclude "/sitemap.xml" from our varnish server in order to get fresh results, would this patch help to clear varnish automatically when the sitemap changes ?

heddn’s picture

Status: Needs work » Needs review
StatusFileSize
new3.88 KB

Rebase for 4.x again.

Status: Needs review » Needs work

The last submitted patch, 29: 3090254-29.patch, failed testing. View results

gbyte’s picture

Happy to merge as soon as all tests pass.

heddn’s picture

Status: Needs work » Needs review
StatusFileSize
new2.09 KB
new4.11 KB

Let's see if this fixes tests.

Status: Needs review » Needs work

The last submitted patch, 32: 3090254-32.patch, failed testing. View results

heddn’s picture

Status: Needs work » Needs review
StatusFileSize
new4.65 KB
new3.72 KB
gbyte’s picture

Assigned: Unassigned » walkingdexter

@heddn Thanks.

@WalkingDexter Can you take a close look at the patch in #34?

  • WalkingDexter committed f415a10 on 4.x
    Issue #3090254 by vasike, heddn, ldavidsp, dww, gbyte, fago, rmontero,...

walkingdexter’s picture

Assigned: walkingdexter » Unassigned
Status: Needs review » Fixed

Made a few changes and committed

Status: Fixed » Closed (fixed)

Automatically closed - issue fixed for 2 weeks with no activity.