JavaScript rendering | 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  JavaScript rendering

 Version   v1      

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

  JavaScript rendering    
- [ 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)

 JavaScript rendering
====================

###  On this page 

1. [ Using Cloudflare Browser Rendering ](#content-using-cloudflare-browser-rendering)
2. [ Configuring a local browser ](#content-configuring-a-local-browser)
3. [ Custom renderers ](#content-custom-renderers)
4. [ Disabling JavaScript execution ](#content-disabling-javascript-execution)
5. [ Browser safety ](#content-browser-safety)

By default, the crawler will not execute JavaScript. Configure a `Renderer` to enable it. Renderers run concurrently with HTTP jobs, after a successful, nonempty, MIME-allowed response. They must be safe for concurrent use and honor context cancellation.

Using Cloudflare Browser Rendering
--------------------------------------------------------------------------------------------------------------------------------------------------------------

`CloudflareRenderer` sends a POST request with `{"url": "..."}` to a compatible gateway and expects JSON with a `content` field containing rendered HTML. This matches PHP's gateway contract, not every Cloudflare API endpoint:

```
c.Renderer = crawler.CloudflareRenderer{
    Endpoint: "https://your-worker.example.com/render",
    Headers: http.Header{"Authorization": {"Bearer " + token}},
    Client: crawler.NewHTTPClient(),
}
```

The default HTTP timeout is 10 seconds. `MaxResponseSize` limits the JSON envelope and defaults to 2 MiB. Non-2xx responses, invalid JSON and oversized envelopes return errors. The crawler reports rendering errors as per-URL failures while retaining the original response metadata.

Configuring a local browser
-----------------------------------------------------------------------------------------------------------------------------------------

Unlike PHP, Go does not install or automatically start Browsershot. Adapt a browser library using `RendererFunc`, or implement `Renderer` directly. Keep the browser dependency and its process lifecycle in your application. This keeps plain HTTP users free of browser dependencies and avoids requiring PHP or Node just to crawl links.

Custom renderers
--------------------------------------------------------------------------------------------------------

```
c.Renderer = crawler.RendererFunc(func(ctx context.Context, u string) ([]byte, error) {
    return renderHTML(ctx, u)
})
```

Here `renderHTML` is your application's browser or remote-service call. It must bound its allocations and honor the context. Configure its own request timeout or put a deadline on the crawl context. The crawler truncates returned HTML to `MaxResponseSize`, but cannot undo memory allocated by a renderer.

HTTP status, final URL, redirects and transfer statistics remain those of the initial HTTP fetch. Rendered content supplies DOM parsing, document robots directives and discovered links. If the browser navigates elsewhere, that new location is not automatically represented by `FinalURL`.

Disabling JavaScript execution
--------------------------------------------------------------------------------------------------------------------------------------------------

Leave `Config.Renderer` nil. Configuration is fixed before a crawl starts. To change settings, create a new crawler, optionally reusing its queue.

Browser safety
--------------------------------------------------------------------------------------------------

Only render trusted sites or use a network-isolated browser service. Scripts, frames, WebSockets and subresources can reach the browser host's network. A crawl profile is not a browser firewall. Keep Chromium's sandbox enabled, and do not forward crawl credentials to unrelated origins. The remote service owns browser authentication, sandboxing and network isolation.

 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)
