0

Below my code as per documentation

    use Spatie\Sitemap\Sitemap;
    use Spatie\Sitemap\Tags\Url;

    $sitemap = Sitemap::create()
        ->add(Url::create(config('app.url')))
    Posts::where(['published'=>true])->each(function (Posts $post) use ($sitemap) {
        $sitemap->add(Url::create(config('app.url')."/post/{$post->category}/{$post->title_slug}")->addImage($post->thumbnail,$post->post_title));
    });

    $sitemap->writeToFile(public_path('sitemap.xml'));

Sitemap generated is

<?xml version="1.0" encoding="UTF-8"?>
<urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9" xmlns:xhtml="http://www.w3.org/1999/xhtml">
  <url>
    <loc>http://127.0.0.1:8000</loc>
    <lastmod>2022-10-20T16:13:52+00:00</lastmod>
    <changefreq>daily</changefreq>
    <priority>0.8</priority>
  </url>
  <url>
    <loc>http://127.0.0.1:8000/posts/hotel/tester-poster</loc>
    <lastmod>2022-10-20T16:13:52+00:00</lastmod>
    <changefreq>daily</changefreq>
    <priority>0.8</priority>
  </url>
</urlset>

And is there A way can generate in chunks I am picturing the feature and if post or pages are more than 50k generating such sitemap would take a lot of memory.

  • I might be wrong but i think you can just do " ->chunk(1000) ->each(function (Collection $chunk) {" – user1170117 Oct 23 '22 at 16:39
  • Okay I will try this bro and let you know if it works okay – Nevil Saul Blemuss Oct 23 '22 at 16:43
  • Just a thought for a lot of items I think it is better to first chunk lets say 2000 items create a file and than create an index of all the files. So the Sitemap::create will be within the each function not outside. You will also have to create add a delete before in order to clear all the old files first. You will have to regenerate a new sitemap each time you update or add a new item but your server will not be processing one every time google decides to check it. I might be wrong about all this but this makes more sense. – user1170117 Oct 23 '22 at 16:44
  • I was looking to answer the same question and I found this package. https://packagist.org/packages/watson/sitemap – user1170117 Oct 23 '22 at 16:47

0 Answers0