How it works
The BigPipe integration works by utilizing the dobron\BigPipe\AsyncResponse class to process requests and generate the defined instructions. These instructions are then handled by the ServerJS class on the frontend. The required Primer module registers essential event listeners, such as click and submit, for elements with the rel attribute.
To make XHR (XMLHttpRequest) requests, you can use the AsyncRequest JavaScript class. It allows you to send requests to the server and receive responses, similar to using the native fetch method. These responses are automatically processed and executed through BigPipe. However, it's important to note that the response on the backend must always be sent using the AsyncResponse class.
Usage example
Here's a step-by-step example of how to use BigPipe to display an alert with the name of the user who just logged in:
Create the file
UserLoggedInAlert.js:export default function UserLoggedInAlert(username) {alert(`Welcome, ${username}!`);}Require the module on backend:
In your PHP backend code, use the
dobron\BigPipe\AsyncResponseclass to require the module and pass arguments:<?php$asyncResponse = new \dobron\BigPipe\AsyncResponse();$asyncResponse->bigPipe()->require("require('UserLoggedInAlert')", ['Marvin', // username argument]);// If this request was made via AsyncRequest, call the send() method$asyncResponse->send();
Request API
In your frontend JavaScript, you can use the AsyncRequest class to send XHR requests.
What all can be Ajaxifed?
You can apply the BigPipe functionality to various elements in your application to enhance its interactivity and performance. Here are some examples of elements that can be ajaxified:
Links
You can ajaxify links using the ajaxify and rel attributes:
Forms
Forms can be ajaxified by adding the rel="async" attribute and the appropriate action:
Dialogs
Dialogs can also be ajaxified by using the ajaxify and rel attributes:
By ajaxifying these elements, you enable dynamic content loading and interaction without the need for full page reloads. This enhances the user experience by providing seamless and efficient updates to the page content.