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 573a4e7
Show file tree
Hide file tree
Showing 4 changed files with 41 additions and 18 deletions.
9 changes: 6 additions & 3 deletions docs/configuration.md
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,10 @@ In the `production` environment these decorators are ignored by design. So this

### $storePreviousURL

Saving the previous URL between requests.
Sometimes this breaks the logic - the previous URL is part of the HTMX instead of the request page.
Specifies whether the HTMX request URL should be stored in the session, for use with the `previous_url()` helper function.
For more information, see the [user guide](https://codeigniter.com/user_guide/helpers/url_helper.html#previous_url).

In the **/home > /htmx-request > /home** query chain, you can sometimes get an unexpected result if the previous URL is saved between requests. Instead of **/home**, you will get **/htmx-request**. The value `false` will save only page from which request was received
Basically, if you use HTMX extensively, including for navigating your site, you will probably want to leave it as `true`,
and in cases where storing the request is not desirable, even if it uses HTMX, you can use custom header, to indicate the
AJAX call or [ajax-header](https://github.com/bigskysoftware/htmx-extensions/blob/main/src/ajax-header/README.md) extension,
which will add the necessary headers automatically. URLs from AJAX requests are always excluded from session storage.
15 changes: 10 additions & 5 deletions src/CodeIgniter.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,12 @@
use CodeIgniter\CodeIgniter as BaseCodeIgniter;
use CodeIgniter\HTTP\CLIRequest;
use CodeIgniter\HTTP\DownloadResponse;
use CodeIgniter\HTTP\IncomingRequest;
use CodeIgniter\HTTP\RedirectResponse;
use CodeIgniter\HTTP\URI;
use Michalsn\CodeIgniterHtmx\HTTP\IncomingRequest as HTMXIncomingRequest;
use Michalsn\CodeIgniterHtmx\HTTP\IncomingRequest;

/**
* @property CLIRequest|HTMXIncomingRequest|IncomingRequest|null $request
* @property CLIRequest|IncomingRequest|null $request
*/
class CodeIgniter extends BaseCodeIgniter
{
Expand Down Expand Up @@ -41,8 +40,14 @@ public function storePreviousURL($uri)
return;
}

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

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

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

/**
* Enable / disable storing previous URLs for HTMX requests.
*/
public bool $storePreviousURL = true;
}
30 changes: 20 additions & 10 deletions tests/CodeIgniterTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,30 +25,40 @@ protected function tearDown(): void
$this->resetServices();
}

public function testIsHTMXNotSavePreviousURL(): void
public function testStorePreviousURLIsHTMX(): void
{
// Default request behavior
$uri = service('uri');
$request = service('incomingrequest');
$_SESSION['_ci_previous_url'] = 'https://example.com/index.php/?previous=original';

$uri->setPath('/')->setQuery('previous=original');
config('Htmx')->storePreviousURL = true;

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

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

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

public function testDontStorePreviousURLIsHTMX(): void
{
$_SESSION['_ci_previous_url'] = 'https://example.com/index.php/?previous=original';

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

config('Htmx')->storePreviousURL = false;

// HTMX request
$uri->setPath('/')->setQuery('previous=htmx');
$request->appendHeader('HX-Request', 'true');
service('uri')->setPath('/')->setQuery('previous=not_saved_from_htmx');
service('request')->appendHeader('HX-Request', 'true');

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

$this->assertTrue($request->isHTMX());
$this->assertTrue(service('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 573a4e7

Please sign in to comment.