OpenLink

Retry

Built-in retry logic with exponential backoff.

OpenLink includes retry functionality for handling transient failures.

Basic Usage

import { preview } from "openlink"

const data = await preview("https://example.com", {
  retry: 3,
  retryDelay: 1000
})

Options

OptionTypeDefaultDescription
retrynumber0Number of retry attempts
retryDelaynumber1000Initial delay between retries in ms

Exponential Backoff

Retry delay doubles after each attempt:

Attempt 1: immediate
Attempt 2: 1000ms delay
Attempt 3: 2000ms delay
Attempt 4: 4000ms delay

Retryable Errors

Not all errors trigger retries. Only transient failures are retried:

Error CodeRetriedDescription
TIMEOUTYesRequest timed out
FETCH_ERRORYesNetwork error
HTTP_ERROR 429YesRate limited
HTTP_ERROR 5xxYesServer error
HTTP_ERROR 4xxNoClient error
INVALID_URLNoBad URL format

Advanced Usage

Use withRetry directly for custom retry logic:

import { withRetry, isRetryable } from "openlink"

const result = await withRetry(
  async (attempt) => {
    console.log(`Attempt ${attempt + 1}`)
    return await riskyOperation()
  },
  {
    retries: 5,
    delay: 500,
    backoff: 2,
    shouldRetry: (error, attempt) => {
      return attempt < 3 && isRetryable(error)
    }
  }
)

With Proxies

Combine retry with proxy failover:

import { preview, corsProxy, createProxyFetch } from "openlink"

const proxies = [
  "https://proxy1.example.com/?url={url}",
  "https://proxy2.example.com/?url={url}"
]

let lastError
for (const proxyUrl of proxies) {
  try {
    return await preview(url, {
      fetch: createProxyFetch(proxyUrl),
      retry: 2
    })
  } catch (err) {
    lastError = err
  }
}
throw lastError

On this page