Crawling across requests | crawler-go | Spatie       

 SPATIE  

crawler-go
==========

spatie.be/open-source

  [Docs](https://spatie.be/docs)  [Crawler-go](https://spatie.be/docs/crawler-go/v1)  Advanced-usage  Crawling across requests

 Version   v1      

 Other versions for crawler [v1](https://spatie.be/docs/crawler-go/v1) 

  Crawling across requests    
- [ Introduction ](https://spatie.be/docs/crawler-go/v1/introduction)
- [ Installation &amp; setup ](https://spatie.be/docs/crawler-go/v1/installation-setup)
- [ Questions and issues ](https://spatie.be/docs/crawler-go/v1/questions-issues)
- [ About us ](https://spatie.be/docs/crawler-go/v1/about-us)
- [ Releasing ](https://spatie.be/docs/crawler-go/v1/releasing)
- [ Changelog ](https://spatie.be/docs/crawler-go/v1/changelog)
- [ Support us ](https://spatie.be/docs/crawler-go/v1/support-us)

Basic usage
-----------

- [ Your first crawl ](https://spatie.be/docs/crawler-go/v1/basic-usage/starting-your-first-crawl)
- [ Crawl responses ](https://spatie.be/docs/crawler-go/v1/basic-usage/handling-crawl-responses)
- [ Using observers ](https://spatie.be/docs/crawler-go/v1/basic-usage/using-observers)
- [ Collecting URLs ](https://spatie.be/docs/crawler-go/v1/basic-usage/collecting-urls)
- [ Filtering URLs ](https://spatie.be/docs/crawler-go/v1/basic-usage/filtering-urls)
- [ Testing ](https://spatie.be/docs/crawler-go/v1/basic-usage/testing)
- [ Tracking progress ](https://spatie.be/docs/crawler-go/v1/basic-usage/tracking-progress)

Configuring the crawler
-----------------------

- [ Concurrency &amp; throttling ](https://spatie.be/docs/crawler-go/v1/configuring-the-crawler/crawl-behavior)
- [ Limits ](https://spatie.be/docs/crawler-go/v1/configuring-the-crawler/setting-crawl-limits)
- [ Extracting resources ](https://spatie.be/docs/crawler-go/v1/configuring-the-crawler/extracting-resources)
- [ Configuring requests ](https://spatie.be/docs/crawler-go/v1/configuring-the-crawler/configuring-requests)
- [ Response filtering ](https://spatie.be/docs/crawler-go/v1/configuring-the-crawler/handling-responses)
- [ Respecting robots.txt ](https://spatie.be/docs/crawler-go/v1/configuring-the-crawler/respecting-robots-txt)

Advanced usage
--------------

- [ JavaScript rendering ](https://spatie.be/docs/crawler-go/v1/advanced-usage/rendering-javascript)
- [ Custom link extraction ](https://spatie.be/docs/crawler-go/v1/advanced-usage/extracting-custom-links)
- [ Custom request handlers ](https://spatie.be/docs/crawler-go/v1/advanced-usage/custom-request-handlers)
- [ Crawling across requests ](https://spatie.be/docs/crawler-go/v1/advanced-usage/crawling-across-requests)
- [ Custom crawl queue ](https://spatie.be/docs/crawler-go/v1/advanced-usage/using-a-custom-crawl-queue)
- [ Graceful shutdown ](https://spatie.be/docs/crawler-go/v1/advanced-usage/graceful-shutdown)

 Crawling across requests
========================

###  On this page 

1. [ Initial request ](#content-initial-request)
2. [ Subsequent requests ](#content-subsequent-requests)

Use `LimitPerExecution` to break a long crawl across multiple HTTP requests. This is useful in serverless environments or when you want to avoid timeouts. See [limits](/docs/crawler-go/v1/configuring-the-crawler/setting-crawl-limits) for all options.

Initial request
-----------------------------------------------------------------------------------------------------

Create a queue and pass it to the crawler. New discoveries fill it while responses are processed. When `Start` returns, serialize and store the queue:

```
queue := crawler.NewMemoryQueue()
c, err := crawler.New("https://example.com", func(c *crawler.Config) {
    c.Queue = queue
    c.LimitPerExecution = 10
})
if err != nil {
    return err
}
_, err = c.Start(ctx)
if err != nil {
    return err
}
checkpoint, err := json.Marshal(queue)
```

Subsequent requests
-----------------------------------------------------------------------------------------------------------------

```
queue := crawler.NewMemoryQueue()
if err := json.Unmarshal(checkpoint, queue); err != nil {
    return err
}
c, err := crawler.New("https://example.com", func(c *crawler.Config) {
    c.Queue = queue
    c.LimitPerExecution = 10
})
if err != nil {
    return err
}
_, err = c.Start(ctx)
```

Store `json.Marshal(queue)` again after the execution. The queue preserves original discoveries, processed identities and elapsed time. It does not store headers, credentials, filters or results. Reapply desired settings and keep a queue associated with its original seed in your application.

Do not serialize or share a queue during an active crawl. Save it only after `Start` has returned and workers have stopped. Interrupted claims are pending again. The JSON decoder also recovers saved active entries as pending. Only one execution can own a queue at once.

In-memory JSON checkpoints are snapshots, not crash-safe transactions. A process crash before saving loses progress since the previous snapshot and may repeat requests and output. Queue completion and application output are not one transaction. Use a custom persistent queue if you need different durability, and make output idempotent.

 A good
match?
-------------

### What we do best

- All things Laravel
- Custom frontend components
- Building APIs
- AI-powered features
- Simplifying things
- Clean solutions
- Integrating services

### Not our cup of tea

- WordPress themes
- Cutting corners
- Free mockups to win a job
- "Just execute the briefing"

 In short: we'd like to be a **substantial part** of your project.

 [ Get in touch via email ](mailto:info@spatie.be?subject=A%20good%20match%21&body=Tell%20us%20as%20much%20as%20you%20can%20about%0A-%20your%20online%20project%0A-%20your%20planning%0A-%20your%20budget%0A-%20%E2%80%A6%0A%0AAnything%20that%20helps%20us%20to%20start%20straightforward%21)
