@@ -44,35 +44,43 @@ public function __invoke(): Response
4444 });
4545
4646 return response (content: $ content )
47- ->header ('Content-Type ' , 'application/xml ' );
47+ ->header ('Content-Type ' , 'application/xml ' )
48+ ->header ('Cache-Control ' , 'public, max-age=3600 ' ); // Cache for 1 hour
4849 }
4950
5051 private function builder (): void
5152 {
5253 $ this ->addDefaultRoutesToSitemap ();
5354
55+ // Use chunked queries to prevent memory issues
5456 News::whereNotNull ('published_at ' )
5557 ->with ('references ' )
56- ->each (function (News $ news ): void {
57- $ this ->addLocalizedPageSet (
58- page: (new PageAction (locale: null , routeName: null ))->news (news: $ news , withReferences: true ),
59- );
58+ ->chunk (100 , function ($ news ) {
59+ foreach ($ news as $ item ) {
60+ $ this ->addLocalizedPageSet (
61+ page: (new PageAction (locale: null , routeName: null ))->news (news: $ item , withReferences: true ),
62+ );
63+ }
6064 });
6165
6266 Service::where ('published ' , true )
6367 ->with ('references ' )
64- ->each (function (Service $ service ): void {
65- $ this ->addLocalizedPageSet (
66- page: (new PageAction (locale: null , routeName: null ))->service (service: $ service , withReferences: true ),
67- );
68+ ->chunk (100 , function ($ services ) {
69+ foreach ($ services as $ item ) {
70+ $ this ->addLocalizedPageSet (
71+ page: (new PageAction (locale: null , routeName: null ))->service (service: $ item , withReferences: true ),
72+ );
73+ }
6874 });
6975
7076 Product::where ('published ' , true )
7177 ->with ('references ' )
72- ->each (function (Product $ product ): void {
73- $ this ->addLocalizedPageSet (
74- page: (new PageAction (locale: null , routeName: null ))->product (product: $ product , withReferences: true ),
75- );
78+ ->chunk (100 , function ($ products ) {
79+ foreach ($ products as $ item ) {
80+ $ this ->addLocalizedPageSet (
81+ page: (new PageAction (locale: null , routeName: null ))->product (product: $ item , withReferences: true ),
82+ );
83+ }
7684 });
7785 }
7886
0 commit comments