mirror of
				https://github.com/strapi/strapi.git
				synced 2025-11-04 11:54:10 +00:00 
			
		
		
		
	fix: gracefully fail 404 results from API
This commit is contained in:
		
							parent
							
								
									55c20e9773
								
							
						
					
					
						commit
						c1f8d20245
					
				@ -8,8 +8,9 @@ export const normalizeSearchResults = (relations, { mainFieldName }) => {
 | 
			
		||||
    ...relations,
 | 
			
		||||
    data: pages
 | 
			
		||||
      .map((page) =>
 | 
			
		||||
        (page ?? []).results.map((relation) => normalizeRelation(relation, { mainFieldName }))
 | 
			
		||||
        page?.results.map((relation) => normalizeRelation(relation, { mainFieldName }))
 | 
			
		||||
      )
 | 
			
		||||
      .filter(Boolean)
 | 
			
		||||
      .flat(),
 | 
			
		||||
  };
 | 
			
		||||
};
 | 
			
		||||
 | 
			
		||||
@ -49,13 +49,26 @@ export const useRelation = (cacheKey, { name, relation, search }) => {
 | 
			
		||||
  const relationsRes = useInfiniteQuery(['relation', cacheKey], fetchRelations, {
 | 
			
		||||
    cacheTime: 0,
 | 
			
		||||
    enabled: relation.enabled,
 | 
			
		||||
    /**
 | 
			
		||||
     * @type {(lastPage:
 | 
			
		||||
     * | { data: null }
 | 
			
		||||
     * | { results: any[],
 | 
			
		||||
     *     pagination: {
 | 
			
		||||
     *      page: number,
 | 
			
		||||
     *      pageCount: number,
 | 
			
		||||
     *      pageSize: number,
 | 
			
		||||
     *      total: number
 | 
			
		||||
     *     }
 | 
			
		||||
     *   }
 | 
			
		||||
     * ) => number}
 | 
			
		||||
     */
 | 
			
		||||
    getNextPageParam(lastPage) {
 | 
			
		||||
      const isXToOneRelation = !lastPage?.pagination;
 | 
			
		||||
 | 
			
		||||
      if (
 | 
			
		||||
        !lastPage || // the API may send an empty 204 response
 | 
			
		||||
        isXToOneRelation || // xToOne relations do not have a pagination
 | 
			
		||||
        lastPage.pagination.page >= lastPage.pagination.pageCount
 | 
			
		||||
        lastPage?.pagination.page >= lastPage?.pagination.pageCount
 | 
			
		||||
      ) {
 | 
			
		||||
        return undefined;
 | 
			
		||||
      }
 | 
			
		||||
@ -126,8 +139,21 @@ export const useRelation = (cacheKey, { name, relation, search }) => {
 | 
			
		||||
    fetchSearch,
 | 
			
		||||
    {
 | 
			
		||||
      enabled: Object.keys(searchParams).length > 0,
 | 
			
		||||
      /**
 | 
			
		||||
       * @type {(lastPage:
 | 
			
		||||
       * | { data: null }
 | 
			
		||||
       * | { results: any[],
 | 
			
		||||
       *     pagination: {
 | 
			
		||||
       *      page: number,
 | 
			
		||||
       *      pageCount: number,
 | 
			
		||||
       *      pageSize: number,
 | 
			
		||||
       *      total: number
 | 
			
		||||
       *     }
 | 
			
		||||
       *   }
 | 
			
		||||
       * ) => number}
 | 
			
		||||
       */
 | 
			
		||||
      getNextPageParam(lastPage) {
 | 
			
		||||
        if (lastPage.pagination.page >= lastPage.pagination.pageCount) {
 | 
			
		||||
        if (!lastPage?.pagination || lastPage.pagination.page >= lastPage.pagination.pageCount) {
 | 
			
		||||
          return undefined;
 | 
			
		||||
        }
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
		Loading…
	
	
			
			x
			
			
		
	
		Reference in New Issue
	
	Block a user