Skip to content

feat: Add use to Router for middleware support with pre-handler execution#1857

Merged
vdusek merged 4 commits intoapify:masterfrom
Mantisus:add-router-use
Apr 28, 2026
Merged

feat: Add use to Router for middleware support with pre-handler execution#1857
vdusek merged 4 commits intoapify:masterfrom
Mantisus:add-router-use

Conversation

@Mantisus
Copy link
Copy Markdown
Collaborator

Description

  • Add Router.use() for registering middleware functions that execute sequentially before the matched request handler, regardless of the request label.

Issues

Testing

  • Add new tests for Router.

Copy link
Copy Markdown
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

Adds router-level middleware support to the Python Router, aligning it with Crawlee JS semantics by executing registered middleware sequentially before the matched route handler.

Changes:

  • Add Router.use() to register pre-handler middleware functions.
  • Execute registered middleware in order on each request before dispatching to the matched/default handler.
  • Add unit tests and documentation/code example demonstrating middleware usage.

Reviewed changes

Copilot reviewed 4 out of 4 changed files in this pull request and generated 2 comments.

File Description
src/crawlee/router.py Introduces middleware registration/storage and executes middleware before handler dispatch.
tests/unit/test_router.py Adds unit tests validating middleware execution order, label behavior, and exception interruption.
docs/guides/request_router.mdx Documents router middleware behavior and links to a runnable example.
docs/guides/code_examples/request_router/router_middleware.py Adds an example showing practical middleware usage (logging + timestamp).

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Comment thread src/crawlee/router.py Outdated
Comment thread src/crawlee/router.py
Copy link
Copy Markdown
Collaborator

@vdusek vdusek left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Two details, otherwise LGTM

Comment thread docs/guides/request_router.mdx Outdated
Comment thread src/crawlee/router.py Outdated
Comment thread docs/guides/request_router.mdx Outdated
Co-authored-by: Vlada Dusek <v.dusek96@gmail.com>
Comment thread docs/guides/request_router.mdx Outdated

## Middleware

Middlewares are functions registered with `router.use()` that execute before the matched request handler on every request, regardless of the request label. Multiple middlewares can be registered and are executed sequentially in the order they were registered. If a middleware raises an exception, the execution chain is interrupted and the handler is not called.
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

router.use is still not linked via ApiLink; we can link it like this class/Router#use

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Updated

Comment thread docs/guides/request_router.mdx Outdated
## Conclusion

This guide introduced you to the <ApiLink to="class/Router">`Router`</ApiLink> class and how to organize your crawling logic. You learned how to use built-in and custom routers, implement request handlers with label-based routing, handle errors with error and failed request handlers, and configure pre-navigation hooks for different crawler types.
This guide introduced you to the <ApiLink to="class/Router">`Router`</ApiLink> class and how to organize your crawling logic. You learned how to use built-in and custom routers, implement request handlers with label-based routing, add middleware with `router.use()`, handle errors with error and failed request handlers, and configure pre-navigation hooks for different crawler types.
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

router.use is still not linked via ApiLink; we can link it like this class/Router#use

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Updated

@vdusek vdusek merged commit 23d7d6c into apify:master Apr 28, 2026
30 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Add Router.use() middleware support

4 participants