File tree Expand file tree Collapse file tree 2 files changed +58
-2
lines changed
src/Illuminate/Http/Middleware Expand file tree Collapse file tree 2 files changed +58
-2
lines changed Original file line number Diff line number Diff line change 88
99class AddLinkHeadersForPreloadedAssets
1010{
11+ /**
12+ * Configure the middleware.
13+ *
14+ * @param int $limit
15+ * @return string
16+ */
17+ public static function using ($ limit )
18+ {
19+ return static ::class.': ' .$ limit ;
20+ }
21+
1122 /**
1223 * Handle the incoming request.
1324 *
1425 * @param \Illuminate\Http\Request $request
1526 * @param \Closure $next
27+ * @param int $limit
1628 * @return \Illuminate\Http\Response
1729 */
18- public function handle ($ request , $ next )
30+ public function handle ($ request , $ next, $ limit = null )
1931 {
20- return tap ($ next ($ request ), function ($ response ) {
32+ return tap ($ next ($ request ), function ($ response ) use ( $ limit ) {
2133 if ($ response instanceof Response && Vite::preloadedAssets () !== []) {
2234 $ response ->header ('Link ' , (new Collection (Vite::preloadedAssets ()))
35+ ->when ($ limit , fn ($ assets , $ limit ) => $ assets ->take ($ limit ))
2336 ->map (fn ($ attributes , $ url ) => "< {$ url }>; " .implode ('; ' , $ attributes ))
2437 ->join (', ' ), false );
2538 }
Original file line number Diff line number Diff line change @@ -106,4 +106,47 @@ public function testItDoesNotOverwriteOtherLinkHeaders()
106106 $ response ->headers ->all ('Link ' ),
107107 );
108108 }
109+
110+ public function testItCanLimitNumberOfAssetsPreloaded ()
111+ {
112+ $ app = new Container ;
113+ $ app ->instance (Vite::class, new class extends Vite
114+ {
115+ protected $ preloadedAssets = [
116+ 'https://laravel.com/first.js ' => [
117+ 'rel="modulepreload" ' ,
118+ 'foo="bar" ' ,
119+ ],
120+ 'https://laravel.com/second.js ' => [
121+ 'rel="modulepreload" ' ,
122+ 'foo="bar" ' ,
123+ ],
124+ 'https://laravel.com/third.js ' => [
125+ 'rel="modulepreload" ' ,
126+ 'foo="bar" ' ,
127+ ],
128+ 'https://laravel.com/fourth.js ' => [
129+ 'rel="modulepreload" ' ,
130+ 'foo="bar" ' ,
131+ ],
132+ ];
133+ });
134+ Facade::setFacadeApplication ($ app );
135+
136+ $ response = (new AddLinkHeadersForPreloadedAssets )->handle (new Request , fn () => new Response ('ok ' ), 2 );
137+
138+ $ this ->assertSame (
139+ [
140+ '<https://laravel.com/first.js>; rel="modulepreload"; foo="bar", <https://laravel.com/second.js>; rel="modulepreload"; foo="bar" ' ,
141+ ],
142+ $ response ->headers ->all ('Link ' ),
143+ );
144+ }
145+
146+ public function test_it_can_configure_the_middleware ()
147+ {
148+ $ definition = AddLinkHeadersForPreloadedAssets::using (limit: 5 );
149+
150+ $ this ->assertSame ('Illuminate\Http\Middleware\AddLinkHeadersForPreloadedAssets:5 ' , $ definition );
151+ }
109152}
You can’t perform that action at this time.
0 commit comments