@@ -538,6 +538,57 @@ The ``file()`` helper provides some arguments to configure its behavior::
538538 return $this->file('invoice_3241.pdf', 'my_invoice.pdf', ResponseHeaderBag::DISPOSITION_INLINE);
539539 }
540540
541+ Sending Early Hints
542+ ~~~~~~~~~~~~~~~~~~~
543+
544+ .. versionadded :: 6.3
545+
546+ The Early Hints helper of the ``AbstractController `` was introduced
547+ in Symfony 6.3.
548+
549+ Early hints allow to tell user's browser to start downloading some assets
550+ even before sending the response content. Thanks to this, the browser is able
551+ to prefetch resources that will be needed once the full response is finally sent.
552+ These resources are commonly Javascript or CSS files, but it can be any type of
553+ resource.
554+
555+ .. note ::
556+
557+ In order to work, the SAPI you're using must support this feature, like
558+ `FrankenPHP `_.
559+
560+ You can send early hints from your controller action thanks to the
561+ :method: `Symfony\\ Bundle\\ FrameworkBundle\\ Controller\\ AbstractController::sendEarlyHints `
562+ method::
563+
564+ namespace App\Controller;
565+
566+ use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
567+ use Symfony\Component\HttpFoundation\Response;
568+ use Symfony\Component\Routing\Annotation\Route;
569+ use Symfony\Component\WebLink\Link;
570+
571+ class HomepageController extends AbstractController
572+ {
573+ #[Route("/", name: "homepage")]
574+ public function index(): Response
575+ {
576+ $response = $this->sendEarlyHints([
577+ (new Link(href: '/style.css'))->withAttribute('as', 'stylesheet'),
578+ (new Link(href: '/script.js'))->withAttribute('as', 'script'),
579+ ]);
580+
581+ // Do something slow...
582+
583+ return $this->render('homepage/index.html.twig', response: $response);
584+ }
585+ }
586+
587+ The ``sendEarlyHints `` method will send a first informational response to the
588+ web browser with a 103 status code. If it supports it, the browser will start
589+ to download ``style.css `` and ``script.js `` while you're generating the response
590+ full content.
591+
541592Final Thoughts
542593--------------
543594
@@ -577,3 +628,4 @@ Learn more about Controllers
577628
578629.. _`Symfony Maker` : https://symfony.com/doc/current/bundles/SymfonyMakerBundle/index.html
579630.. _`unvalidated redirects security vulnerability` : https://cheatsheetseries.owasp.org/cheatsheets/Unvalidated_Redirects_and_Forwards_Cheat_Sheet.html
631+ .. _`FrankenPHP` : https://frankenphp.dev
0 commit comments