@@ -230,7 +230,7 @@ const buildSitemapXmlStream = async <T extends ObjectLiteral>(
230230} ;
231231
232232const getSitemapPageCount = ( totalPosts : number ) : number =>
233- Math . max ( 1 , Math . ceil ( totalPosts / getPaginatedSitemapLimit ( ) ) ) ;
233+ Math . ceil ( totalPosts / getPaginatedSitemapLimit ( ) ) ;
234234
235235const getReplicaQueryCount = async (
236236 con : DataSource ,
@@ -245,6 +245,20 @@ const getReplicaQueryCount = async (
245245 }
246246} ;
247247
248+ const hasPaginatedSitemapPage = async (
249+ con : DataSource ,
250+ buildQuery : ( source : EntityManager , page : number ) => SelectQueryBuilder < Post > ,
251+ page : number ,
252+ ) : Promise < boolean > => {
253+ const queryRunner = con . createQueryRunner ( 'slave' ) ;
254+
255+ try {
256+ return ! ! ( await buildQuery ( queryRunner . manager , page ) . limit ( 1 ) . getRawOne ( ) ) ;
257+ } finally {
258+ await queryRunner . release ( ) ;
259+ }
260+ } ;
261+
248262const buildSitemapIndexEntries = (
249263 prefix : string ,
250264 sitemapCount : number ,
@@ -823,6 +837,10 @@ export default async function (fastify: FastifyInstance): Promise<void> {
823837
824838 const con = await createOrGetConnection ( ) ;
825839
840+ if ( ! ( await hasPaginatedSitemapPage ( con , buildPostsSitemapQuery , page ) ) ) {
841+ return res . code ( 404 ) . send ( ) ;
842+ }
843+
826844 return res
827845 . type ( 'application/xml' )
828846 . header ( 'cache-control' , SITEMAP_CACHE_CONTROL )
@@ -849,6 +867,12 @@ export default async function (fastify: FastifyInstance): Promise<void> {
849867
850868 const con = await createOrGetConnection ( ) ;
851869
870+ if (
871+ ! ( await hasPaginatedSitemapPage ( con , buildEvergreenSitemapQuery , page ) )
872+ ) {
873+ return res . code ( 404 ) . send ( ) ;
874+ }
875+
852876 return res
853877 . type ( 'application/xml' )
854878 . header ( 'cache-control' , SITEMAP_CACHE_CONTROL )
0 commit comments