Skip to content

Commit

Permalink
feat: Add in config parameter storePreviousURL
Browse files Browse the repository at this point in the history
  • Loading branch information
neznaika0 committed Nov 6, 2024
1 parent d47b223 commit 777ca8c
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 4 deletions.
8 changes: 7 additions & 1 deletion src/CodeIgniter.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
use CodeIgniter\HTTP\IncomingRequest;
use CodeIgniter\HTTP\RedirectResponse;
use CodeIgniter\HTTP\URI;
use Michalsn\CodeIgniterHtmx\Config\Htmx;
use Michalsn\CodeIgniterHtmx\HTTP\IncomingRequest as HTMXIncomingRequest;

/**
Expand Down Expand Up @@ -41,8 +42,13 @@ public function storePreviousURL($uri)
return;
}

$storePreviousURL = config(Htmx::class)->storePreviousURL;

// Ignore AJAX and HTMX requests
if ((method_exists($this->request, 'isHTMX') && $this->request->isHTMX()) || (method_exists($this->request, 'isAJAX') && $this->request->isAJAX())) {
if (
($storePreviousURL === false && (method_exists($this->request, 'isHTMX') && $this->request->isHTMX()))
|| (method_exists($this->request, 'isAJAX') && $this->request->isAJAX())
) {
return;
}

Expand Down
6 changes: 6 additions & 0 deletions src/Config/Htmx.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,4 +22,10 @@ class Htmx extends BaseConfig
* when they are enabled.
*/
public string $skipViewDecoratorsString = 'htmxSkipViewDecorators';

/**
* Saving the previous URL between requests
* Sometimes this breaks the logic - the previous URL is part of the HTMX instead of the request page
*/
public bool $storePreviousURL = true;
}
23 changes: 20 additions & 3 deletions tests/CodeIgniterTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
namespace Tests;

use CodeIgniter\Test\CIUnitTestCase;
use Michalsn\CodeIgniterHtmx\Config\Htmx;

/**
* @internal
Expand All @@ -29,7 +30,7 @@ public function testIsHTMXNotSavePreviousURL(): void
{
// Default request behavior
$uri = service('uri');
$request = service('incomingrequest');
$request = service('request');

$uri->setPath('/')->setQuery('previous=original');

Expand All @@ -40,14 +41,30 @@ public function testIsHTMXNotSavePreviousURL(): void
$this->assertArrayHasKey('_ci_previous_url', $_SESSION);
$this->assertSame('https://example.com/index.php/?previous=original', $_SESSION['_ci_previous_url']);

// HTMX request
$uri->setPath('/')->setQuery('previous=htmx');
// HTMX request with the previous URL saved
config(Htmx::class)->storePreviousURL = true;

$uri->setPath('/')->setQuery('previous=saved_from_htmx');
$request->appendHeader('HX-Request', 'true');

ob_start();
service('codeigniter', null, false)->setContext('web')->run();
ob_get_clean();

$this->assertTrue($request->isHTMX());
$this->assertArrayHasKey('_ci_previous_url', $_SESSION);
$this->assertSame('https://example.com/index.php/?previous=saved_from_htmx', $_SESSION['_ci_previous_url']);

// HTMX request without saving the previous URL
$_SESSION['_ci_previous_url'] = 'https://example.com/index.php/?previous=original';

config(Htmx::class)->storePreviousURL = false;
$uri->setPath('/')->setQuery('previous=not_saved_from_htmx');

ob_start();
service('codeigniter', null, false)->setContext('web')->run();
ob_get_clean();

$this->assertTrue($request->isHTMX());
$this->assertArrayHasKey('_ci_previous_url', $_SESSION);
$this->assertSame('https://example.com/index.php/?previous=original', $_SESSION['_ci_previous_url']);
Expand Down

0 comments on commit 777ca8c

Please sign in to comment.