Skip to content

Commit

Permalink
Make default eagerness configurable
Browse files Browse the repository at this point in the history
  • Loading branch information
authanram committed Aug 9, 2024
1 parent 357c5e8 commit c9c2ae9
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 4 deletions.
8 changes: 4 additions & 4 deletions src/LaravelSpeculationRulesApiServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,18 +18,18 @@ public function configurePackage(Package $package): void

public function bootingPackage(): void
{
Route::macro('prerender', function (string $eagerness = 'moderate') {
Route::macro('prerender', function (?string $eagerness = null) {
LaravelSpeculationRulesApi::$routeSpeculationRules['prerender'][] = [
'eagerness' => $eagerness,
'eagerness' => $eagerness ?? config('speculation-rules-api.default_eagerness'),
'uri' => $this->uri,
];

return $this;
});

Route::macro('prefetch', function (string $eagerness = 'moderate') {
Route::macro('prefetch', function (?string $eagerness = null) {
LaravelSpeculationRulesApi::$routeSpeculationRules['prefetch'][] = [
'eagerness' => $eagerness,
'eagerness' => $eagerness ?? config('speculation-rules-api.default_eagerness'),
'uri' => $this->uri,
];

Expand Down
19 changes: 19 additions & 0 deletions tests/SpeculationRulesApiTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,25 @@
expect(LaravelSpeculationRulesApi::$routeSpeculationRules)->toMatchSnapshot();
});

test('default eagerness', function () {
LaravelSpeculationRulesApi::$routeSpeculationRules = [
'prerender' => [],
'prefetch' => [],
];

Route::get('page-1', fn () => null)->prerender();

expect(data_get(LaravelSpeculationRulesApi::$routeSpeculationRules, 'prerender.0.eagerness'))
->toBe('moderate');

config()->set('speculation-rules-api.default_eagerness', 'eager');

Route::get('page-1', fn () => null)->prerender();

expect(data_get(LaravelSpeculationRulesApi::$routeSpeculationRules, 'prerender.1.eagerness'))
->toBe('eager');
});

test('speculation rules are merged properly', function () {
LaravelSpeculationRulesApi::$routeSpeculationRules = [
'prerender' => [],
Expand Down

0 comments on commit c9c2ae9

Please sign in to comment.