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 8
8
9
9
class AddLinkHeadersForPreloadedAssets
10
10
{
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
+
11
22
/**
12
23
* Handle the incoming request.
13
24
*
14
25
* @param \Illuminate\Http\Request $request
15
26
* @param \Closure $next
27
+ * @param int $limit
16
28
* @return \Illuminate\Http\Response
17
29
*/
18
- public function handle ($ request , $ next )
30
+ public function handle ($ request , $ next, $ limit = null )
19
31
{
20
- return tap ($ next ($ request ), function ($ response ) {
32
+ return tap ($ next ($ request ), function ($ response ) use ( $ limit ) {
21
33
if ($ response instanceof Response && Vite::preloadedAssets () !== []) {
22
34
$ response ->header ('Link ' , (new Collection (Vite::preloadedAssets ()))
35
+ ->when ($ limit , fn ($ assets , $ limit ) => $ assets ->take ($ limit ))
23
36
->map (fn ($ attributes , $ url ) => "< {$ url }>; " .implode ('; ' , $ attributes ))
24
37
->join (', ' ), false );
25
38
}
Original file line number Diff line number Diff line change @@ -106,4 +106,47 @@ public function testItDoesNotOverwriteOtherLinkHeaders()
106
106
$ response ->headers ->all ('Link ' ),
107
107
);
108
108
}
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
+ }
109
152
}
You can’t perform that action at this time.
0 commit comments