Skip to content

Commit 27848c6

Browse files
committed
Fix query string hydration
1 parent 8e55cf5 commit 27848c6

6 files changed

Lines changed: 226 additions & 35 deletions

File tree

config/config.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@
1616
// The addon will calculate the number of entries for each filter value (can be slow for a large number of entries)
1717
'enable_filter_values_count' => false,
1818

19-
// Enable custom query string
19+
// Enable custom query string (ignored when enable_query_string is true)
2020
'custom_query_string' => false,
2121

2222
// Set the aliases for each custom query string parameter

src/Http/Livewire/LivewireCollection.php

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
use Livewire\Attributes\Locked;
66
use Livewire\Attributes\On;
77
use Livewire\Component;
8+
use Reach\StatamicLivewireFilters\Support\CustomQueryString;
89
use Reach\StatamicLivewireFilters\Support\Nocache;
910
use Statamic\Support\Traits\Hookable;
1011
use Statamic\Tags\Collection\Entries;
@@ -145,7 +146,7 @@ public function sortUpdated($sort)
145146

146147
protected function queryString()
147148
{
148-
if (config('statamic-livewire-filters.enable_query_string')) {
149+
if (CustomQueryString::livewireQueryStringEnabled()) {
149150
return [
150151
'params' => ['except' => []],
151152
];
@@ -154,7 +155,7 @@ protected function queryString()
154155
// When using custom query string, disable Livewire's pagination URL handling
155156
// to prevent a double pushState (which causes an extra render).
156157
// We handle pagination URL ourselves in updateCustomQueryStringUrl().
157-
if (config('statamic-livewire-filters.custom_query_string') !== false) {
158+
if (CustomQueryString::enabled()) {
158159
return [
159160
'paginators.page' => ['except' => '', 'history' => false],
160161
];

src/Http/Livewire/Traits/HandleParams.php

Lines changed: 7 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
use Livewire\Attributes\On;
77
use Livewire\Attributes\Renderless;
88
use Reach\StatamicLivewireFilters\Http\Livewire\LfTags;
9+
use Reach\StatamicLivewireFilters\Support\CustomQueryString;
910

1011
trait HandleParams
1112
{
@@ -28,11 +29,7 @@ public function setParameters($params)
2829

2930
protected function handleCustomQueryStringParams(): array|bool
3031
{
31-
if (
32-
config('statamic-livewire-filters.custom_query_string') !== false &&
33-
config('statamic-livewire-filters.enable_query_string') === false &&
34-
request()->has('params')
35-
) {
32+
if (CustomQueryString::enabled() && request()->has('params')) {
3633
// Get and validate params
3734
$params = request()->validate([
3835
'params' => 'array',
@@ -253,14 +250,14 @@ protected function handlePresetParams()
253250
#[Renderless, On('preset-params')]
254251
public function updateCustomQueryStringUrl(): void
255252
{
256-
if (config('statamic-livewire-filters.custom_query_string') === false) {
253+
$prefix = CustomQueryString::prefix();
254+
255+
if ($prefix === false) {
257256
return;
258257
}
259258

260259
$aliases = $this->getConfigAliases();
261260

262-
$prefix = config('statamic-livewire-filters.custom_query_string', 'filters');
263-
264261
// Only include params that have aliases configured
265262
$segments = collect($this->params)
266263
->filter(fn ($value, $key) => isset($aliases[$key]))
@@ -338,10 +335,10 @@ protected function getConfigAliases(): array
338335

339336
protected function stripCustomQueryStringPrefix(string $path): string
340337
{
341-
$prefix = config('statamic-livewire-filters.custom_query_string', 'filters');
338+
$prefix = CustomQueryString::prefix();
342339
$segments = array_values(array_filter(explode('/', trim($path, '/')), fn ($segment) => $segment !== ''));
343340

344-
if (! $prefix) {
341+
if ($prefix === false) {
345342
return implode('/', $segments);
346343
}
347344

src/Http/Middleware/HandleFiltersQueryString.php

Lines changed: 11 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -5,15 +5,18 @@
55
use Closure;
66
use Illuminate\Http\Request;
77
use Illuminate\Support\Str;
8+
use Reach\StatamicLivewireFilters\Support\CustomQueryString;
89
use Reach\StatamicLivewireFilters\Support\Nocache;
910

1011
class HandleFiltersQueryString
1112
{
1213
/**
1314
* Keys we must never overwrite when hydrating the request from the target URL's
1415
* query string, since Statamic's nocache controller and Livewire rely on them.
16+
* Note that `params` is intentionally not reserved: it is the query key Livewire
17+
* uses to restore filter state when `enable_query_string` is on.
1518
*/
16-
protected const RESERVED_INPUT_KEYS = ['url', 'params', '_token', '_method', 'fingerprint', 'serialized', 'effects'];
19+
protected const RESERVED_INPUT_KEYS = ['url', '_token', '_method', 'fingerprint', 'serialized', 'effects'];
1720

1821
public function handle(Request $request, Closure $next): mixed
1922
{
@@ -42,10 +45,10 @@ public function handle(Request $request, Closure $next): mixed
4245
} else {
4346
$path = $request->path();
4447
}
45-
$prefix = config('statamic-livewire-filters.custom_query_string', 'filters');
48+
$prefix = CustomQueryString::prefix();
4649

4750
$segments = explode('/', $path);
48-
$filterIndex = array_search($prefix, $segments);
51+
$filterIndex = array_search($prefix, $segments, true);
4952

5053
if ($filterIndex !== false) {
5154
// Extract and parse filter segments
@@ -92,15 +95,15 @@ protected function hydrateNocacheRequestFromUrl(Request $request): void
9295
return;
9396
}
9497

95-
if (config('statamic-livewire-filters.enable_query_string') === true) {
98+
if (CustomQueryString::livewireQueryStringEnabled()) {
9699
$this->hydrateFromOriginalQueryString($request, $parsed);
97100

98101
return;
99102
}
100103

101-
$prefix = config('statamic-livewire-filters.custom_query_string', 'filters');
104+
$prefix = CustomQueryString::prefix();
102105

103-
if (! $prefix) {
106+
if ($prefix === false) {
104107
return;
105108
}
106109

@@ -130,7 +133,7 @@ protected function hydrateNocacheRequestFromUrl(Request $request): void
130133
$normalized .= ':'.$parsed['port'];
131134
}
132135

133-
$normalized .= $basePath === '/' ? '' : $basePath;
136+
$normalized .= $basePath === '/' ? '/' : $basePath;
134137

135138
if (! empty($parsed['query'])) {
136139
$normalized .= '?'.$parsed['query'];
@@ -166,7 +169,7 @@ protected function hydrateFromOriginalQueryString(Request $request, array $parse
166169

167170
protected function shouldSkip(): bool
168171
{
169-
return config('statamic-livewire-filters.custom_query_string') === false || config('statamic-livewire-filters.enable_query_string') === true;
172+
return ! CustomQueryString::enabled();
170173
}
171174

172175
protected function shouldProcessRequest(Request $request): bool

src/Support/CustomQueryString.php

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
<?php
2+
3+
namespace Reach\StatamicLivewireFilters\Support;
4+
5+
class CustomQueryString
6+
{
7+
public static function livewireQueryStringEnabled(): bool
8+
{
9+
return (bool) config('statamic-livewire-filters.enable_query_string');
10+
}
11+
12+
/**
13+
* The active custom query string prefix, or false when the feature is off.
14+
*
15+
* Only one URL mode may be active at a time: when `enable_query_string`
16+
* is on, Livewire's own query string handling wins and the custom
17+
* prefix is treated as disabled.
18+
*/
19+
public static function prefix(): string|false
20+
{
21+
if (static::livewireQueryStringEnabled()) {
22+
return false;
23+
}
24+
25+
$prefix = config('statamic-livewire-filters.custom_query_string', 'filters');
26+
27+
return is_string($prefix) && $prefix !== '' ? $prefix : false;
28+
}
29+
30+
public static function enabled(): bool
31+
{
32+
return static::prefix() !== false;
33+
}
34+
}

0 commit comments

Comments
 (0)