Customize request detection | laravel-markdown-response | Spatie

 SPATIE

  Laravel Markdown Response
============================

spatie.be/open-source

  [Docs](https://spatie.be/docs)  [Laravel-markdown-response](https://spatie.be/docs/laravel-markdown-response/v1)  Advanced-usage  Customize request detection

 Version   v1

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

- [ Introduction ](https://spatie.be/docs/laravel-markdown-response/v1/introduction)
- [ Requirements ](https://spatie.be/docs/laravel-markdown-response/v1/requirements)
- [ Install the package ](https://spatie.be/docs/laravel-markdown-response/v1/installation-setup)
- [ Questions &amp; issues ](https://spatie.be/docs/laravel-markdown-response/v1/questions-issues)
- [ Support us ](https://spatie.be/docs/laravel-markdown-response/v1/support-us)
- [ About us ](https://spatie.be/docs/laravel-markdown-response/v1/about-us)
- [ Changelog ](https://spatie.be/docs/laravel-markdown-response/v1/changelog)

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

- [ Serve markdown to AI agents ](https://spatie.be/docs/laravel-markdown-response/v1/basic-usage/serve-markdown-to-ai-agents)
- [ Cache converted responses ](https://spatie.be/docs/laravel-markdown-response/v1/basic-usage/cache-converted-responses)
- [ Choose a conversion driver ](https://spatie.be/docs/laravel-markdown-response/v1/basic-usage/choose-a-conversion-driver)
- [ Test your setup ](https://spatie.be/docs/laravel-markdown-response/v1/basic-usage/test-your-setup)

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

- [ Clean up HTML with preprocessors ](https://spatie.be/docs/laravel-markdown-response/v1/advanced-usage/clean-up-html-with-preprocessors)
- [ Clean up markdown with postprocessors ](https://spatie.be/docs/laravel-markdown-response/v1/advanced-usage/clean-up-markdown-with-postprocessors)
- [ Customize request detection ](https://spatie.be/docs/laravel-markdown-response/v1/advanced-usage/customize-request-detection)
- [ Listen to conversion events ](https://spatie.be/docs/laravel-markdown-response/v1/advanced-usage/listen-to-conversion-events)
- [ Convert HTML directly ](https://spatie.be/docs/laravel-markdown-response/v1/advanced-usage/convert-html-directly)
- [ CDN and cache layers ](https://spatie.be/docs/laravel-markdown-response/v1/advanced-usage/cdn-and-cache-layers)
- [ Response headers ](https://spatie.be/docs/laravel-markdown-response/v1/advanced-usage/response-headers)

 Customize request detection
===========================

###  On this page

1. [ Toggle detection methods ](#content-toggle-detection-methods)
2. [ Extend the detector ](#content-extend-the-detector)

The built-in detector handles most cases, but you can fine-tune which detection methods are active or replace the detector entirely.

Toggle detection methods
--------------------------------------------------------------------------------------------------------------------------------

You can enable or disable individual detection methods in the config:

```
// config/markdown-response.php

'detection' => [
    'detect_via_accept_header' => true,
    'detect_via_md_suffix' => true,
    'detect_via_user_agents' => [
        'GPTBot',
        'ClaudeBot',
        // ...
    ],
],
```

Set `detect_via_user_agents` to an empty array to disable user agent detection.

Extend the detector
-----------------------------------------------------------------------------------------------------------------

If you need more control, you can extend the `DetectsMarkdownRequest` class and point to it in the config:

```
// config/markdown-response.php

'detection' => [
    'detector' => App\Actions\CustomDetectsMarkdownRequest::class,
],
```

```
namespace App\Actions;

use Illuminate\Http\Request;
use Spatie\MarkdownResponse\Actions\DetectsMarkdownRequest;

class CustomDetectsMarkdownRequest extends DetectsMarkdownRequest
{
    public function __invoke(Request $request): false|string
    {
        // Only respond with markdown for specific paths
        if (! str_starts_with($request->getPathInfo(), '/docs')) {
            return false;
        }

        return parent::__invoke($request);
    }
}
```

The returned string tells the middleware how to handle the request:

- `'suffix'`: the middleware strips `.md` from the URL before routing
- `'accept'`: the middleware temporarily swaps the Accept header to `text/html`
- `'user-agent'`: the middleware lets the request pass through normally
