About Dynamic Page Hooks

Dynamic page hooks, also known as data binding router hooks, run custom code at specific points during the page routing and loading process. Unlike regular data hooks that run during data operations, dynamic page hooks let you intercept the routing process when a visitor requests a dynamic page. You can use these hooks to validate requests, modify data queries, or customize the page response.

Hook types

Dynamic page hooks run in a specific sequence during the routing process. You can implement any or all of these hooks depending on your needs.

The hooks run in the following order:

  • beforeRouter: Runs before the router processes the request. Use this to validate requests, check permissions, or redirect visitors based on authentication or other criteria.
  • customizeQuery: Runs before the page's data query executes. Use this to modify the query that determines what data binds to the page's dataset, such as filtering results or adding additional query conditions.
  • afterRouter: Runs after retrieving data but before the page displays. Use this to modify the router's response based on the retrieved data, such as choosing different page layouts or redirecting based on data properties.

If you also have regular data hooks defined, they run between customizeQuery and afterRouter in the sequence.

Hook conventions

Dynamic page hooks follow specific naming and file conventions. Define hooks in the routers.js file in the site's backend folder and use the router prefix in their function names.

For detailed information about hook conventions, parameters, and implementation, see the Router hooks reference.

See also

Did this help?