Skip to content

Commit

Permalink
Merge pull request #590 from dappforce/deploy/revalidate
Browse files Browse the repository at this point in the history
Fix revalidation try catch
  • Loading branch information
olehmell authored Mar 12, 2024
2 parents 0a11262 + 9a69d6a commit 00c6f46
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 8 deletions.
7 changes: 6 additions & 1 deletion src/hooks/useWaitHasEnergy.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { ESTIMATED_ENERGY_FOR_ONE_TX } from '@/constants/subsocial'
import { useMyAccount } from '@/stores/my-account'
import { currentNetwork } from '@/utils/network'
import { useCallback, useEffect, useRef } from 'react'
Expand Down Expand Up @@ -32,7 +33,11 @@ export default function useWaitHasEnergy(
}, [timeout, resubscribeEnergy])

useEffect(() => {
if (!energy || energy <= 0 || hasEnergyResolvers.current.length === 0)
if (
!energy ||
energy < ESTIMATED_ENERGY_FOR_ONE_TX ||
hasEnergyResolvers.current.length === 0
)
return
function resolveAllPending() {
hasEnergyResolvers.current.forEach((resolve) => resolve())
Expand Down
19 changes: 12 additions & 7 deletions src/pages/api/posts.ts
Original file line number Diff line number Diff line change
Expand Up @@ -101,17 +101,22 @@ export async function getPostsServer(postIds: string[]): Promise<PostData[]> {
})

const metadataMap: Record<string, LinkMetadata> = {}
const metadataPromises = Array.from(linksToFetch).map(async (link) => {
const metadataPromises = Array.from(linksToFetch).map((link) => {
// link fetching itself has timeout of 20_000, but for data fetching, it faster timeout
// if it needs more than 8s, the post fetching is not delayed, but it will still be fetched and put to redis
// so that in the next fetch, it will have the correct data from the redis
try {
setTimeout(() => {
throw new Error('Link metadata fetching timeout')
}, 8_000)
async function getMetadata() {
const metadata = await getLinkMetadata(link)
if (metadata) metadataMap[link] = metadata
} catch {}
}
return new Promise((resolve, reject) => {
setTimeout(() => {
reject('Link metadata fetching timeout')
}, 8_000)
getMetadata().then(() => {
resolve(null)
}, reject)
})
})
await Promise.allSettled(metadataPromises)

Expand Down Expand Up @@ -164,7 +169,7 @@ export async function getLinkMetadata(
)
return parsedMetadata
} catch (err) {
console.error('Error fetching page metadata for link: ', link)
console.error('Error fetching link metadata for link: ', link)
redisCallWrapper((redis) =>
redis?.set(
getMetadataRedisKey(link),
Expand Down

0 comments on commit 00c6f46

Please sign in to comment.