Skip to content

Commit 573a4e7

Browse files
committed
feat: Add in config parameter storePreviousURL
1 parent d47b223 commit 573a4e7

File tree

4 files changed

+41
-18
lines changed

4 files changed

+41
-18
lines changed

docs/configuration.md

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,10 @@ In the `production` environment these decorators are ignored by design. So this
3939

4040
### $storePreviousURL
4141

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

45-
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
45+
Basically, if you use HTMX extensively, including for navigating your site, you will probably want to leave it as `true`,
46+
and in cases where storing the request is not desirable, even if it uses HTMX, you can use custom header, to indicate the
47+
AJAX call or [ajax-header](https://github.com/bigskysoftware/htmx-extensions/blob/main/src/ajax-header/README.md) extension,
48+
which will add the necessary headers automatically. URLs from AJAX requests are always excluded from session storage.

src/CodeIgniter.php

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -5,13 +5,12 @@
55
use CodeIgniter\CodeIgniter as BaseCodeIgniter;
66
use CodeIgniter\HTTP\CLIRequest;
77
use CodeIgniter\HTTP\DownloadResponse;
8-
use CodeIgniter\HTTP\IncomingRequest;
98
use CodeIgniter\HTTP\RedirectResponse;
109
use CodeIgniter\HTTP\URI;
11-
use Michalsn\CodeIgniterHtmx\HTTP\IncomingRequest as HTMXIncomingRequest;
10+
use Michalsn\CodeIgniterHtmx\HTTP\IncomingRequest;
1211

1312
/**
14-
* @property CLIRequest|HTMXIncomingRequest|IncomingRequest|null $request
13+
* @property CLIRequest|IncomingRequest|null $request
1514
*/
1615
class CodeIgniter extends BaseCodeIgniter
1716
{
@@ -41,8 +40,14 @@ public function storePreviousURL($uri)
4140
return;
4241
}
4342

44-
// Ignore AJAX and HTMX requests
45-
if ((method_exists($this->request, 'isHTMX') && $this->request->isHTMX()) || (method_exists($this->request, 'isAJAX') && $this->request->isAJAX())) {
43+
// Ignore AJAX requests
44+
if (method_exists($this->request, 'isAJAX') && $this->request->isAJAX()) {
45+
return;
46+
}
47+
48+
// Ignore HTMX requests
49+
if (config('Htmx')->storePreviousURL === false
50+
&& method_exists($this->request, 'isHTMX') && $this->request->isHTMX()) {
4651
return;
4752
}
4853

src/Config/Htmx.php

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,4 +22,9 @@ class Htmx extends BaseConfig
2222
* when they are enabled.
2323
*/
2424
public string $skipViewDecoratorsString = 'htmxSkipViewDecorators';
25+
26+
/**
27+
* Enable / disable storing previous URLs for HTMX requests.
28+
*/
29+
public bool $storePreviousURL = true;
2530
}

tests/CodeIgniterTest.php

Lines changed: 20 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -25,30 +25,40 @@ protected function tearDown(): void
2525
$this->resetServices();
2626
}
2727

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

34-
$uri->setPath('/')->setQuery('previous=original');
32+
config('Htmx')->storePreviousURL = true;
33+
34+
service('uri')->setPath('/')->setQuery('previous=saved_from_htmx');
35+
service('request')->appendHeader('HX-Request', 'true');
3536

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

41+
$this->assertTrue(service('request')->isHTMX());
4042
$this->assertArrayHasKey('_ci_previous_url', $_SESSION);
41-
$this->assertSame('https://example.com/index.php/?previous=original', $_SESSION['_ci_previous_url']);
43+
$this->assertSame('https://example.com/index.php/?previous=saved_from_htmx', $_SESSION['_ci_previous_url']);
44+
}
45+
46+
public function testDontStorePreviousURLIsHTMX(): void
47+
{
48+
$_SESSION['_ci_previous_url'] = 'https://example.com/index.php/?previous=original';
49+
50+
service('uri')->setPath('/')->setQuery('previous=original');
51+
52+
config('Htmx')->storePreviousURL = false;
4253

43-
// HTMX request
44-
$uri->setPath('/')->setQuery('previous=htmx');
45-
$request->appendHeader('HX-Request', 'true');
54+
service('uri')->setPath('/')->setQuery('previous=not_saved_from_htmx');
55+
service('request')->appendHeader('HX-Request', 'true');
4656

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

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

0 commit comments

Comments
 (0)