Skip to content

Commit

Permalink
Adding a prefetch() method to the Request class
Browse files Browse the repository at this point in the history
  • Loading branch information
fkeloks committed Dec 31, 2018
1 parent 2219c7d commit 97fc8bd
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 1 deletion.
11 changes: 11 additions & 0 deletions src/Illuminate/Http/Request.php
Original file line number Diff line number Diff line change
Expand Up @@ -243,6 +243,17 @@ public function pjax()
return $this->headers->get('X-PJAX') == true;
}

/**
* Determine if the request is the result of an prefetch call.
*
* @return bool
*/
public function prefetch()
{
return strcasecmp($this->server->get('HTTP_X_MOZ'), 'prefetch') === 0 ||
strcasecmp($this->headers->get('Purpose'), 'prefetch') === 0;
}

/**
* Determine if the request is over HTTPS.
*
Expand Down
2 changes: 1 addition & 1 deletion src/Illuminate/Session/Middleware/StartSession.php
Original file line number Diff line number Diff line change
Expand Up @@ -154,7 +154,7 @@ protected function configHitsLottery(array $config)
*/
protected function storeCurrentUrl(Request $request, $session)
{
if ($request->method() === 'GET' && $request->route() && ! $request->ajax()) {
if ($request->method() === 'GET' && $request->route() && ! $request->ajax() && ! $request->prefetch()) {
$session->setPreviousUrl($request->fullUrl());
}
}
Expand Down
22 changes: 22 additions & 0 deletions tests/Http/HttpRequestTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -223,6 +223,28 @@ public function testAjaxMethod()
$this->assertFalse($request->ajax());
}

public function testPrefetchMethod()
{
$request = Request::create('/', 'GET');
$this->assertFalse($request->prefetch());

$request->server->set('HTTP_X_MOZ', '');
$this->assertFalse($request->prefetch());
$request->server->set('HTTP_X_MOZ', 'prefetch');
$this->assertTrue($request->prefetch());
$request->server->set('HTTP_X_MOZ', 'Prefetch');
$this->assertTrue($request->prefetch());

$request->server->remove('HTTP_X_MOZ');

$request->headers->set('Purpose', '');
$this->assertFalse($request->prefetch());
$request->headers->set('Purpose', 'prefetch');
$this->assertTrue($request->prefetch());
$request->headers->set('Purpose', 'Prefetch');
$this->assertTrue($request->prefetch());
}

public function testPjaxMethod()
{
$request = Request::create('/', 'GET', [], [], [], ['HTTP_X_PJAX' => 'true'], '{}');
Expand Down

0 comments on commit 97fc8bd

Please sign in to comment.