Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[5.x] Normalize query string when creating nocache session #11545

Open
wants to merge 2 commits into
base: 5.x
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions src/StaticCaching/ServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
use Illuminate\Support\Facades\Blade;
use Illuminate\Support\Facades\Event;
use Illuminate\Support\ServiceProvider as LaravelServiceProvider;
use Illuminate\Support\Str;
use Statamic\Facades\Cascade;
use Statamic\StaticCaching\NoCache\DatabaseSession;
use Statamic\StaticCaching\NoCache\Session;
Expand Down Expand Up @@ -42,6 +43,10 @@ public function register()
$uri = explode('?', $uri)[0];
}

if (Str::contains($uri, '?')) {
$uri = Str::before($uri, '?').'?'.Request::normalizeQueryString(Str::after($uri, '?'));
}

return match ($driver = config('statamic.static_caching.nocache', 'cache')) {
'cache' => new Session($uri),
'database' => new DatabaseSession($uri),
Expand Down
11 changes: 11 additions & 0 deletions tests/StaticCaching/NoCacheSessionTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -171,6 +171,17 @@ public function it_ignores_the_query_string()
$this->assertEquals('http://localhost/test', $session->url());
}

#[Test]
public function it_normalizes_query_string()
{
$this->get('/test?utm_source=linkedin.com&utm_medium=referral&utm_campaign=foo');
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

When I comment out my changes in the ServiceProvider this test continues to pass.

It looks like Laravel is normalizing the query string itself in tests, which doesn't happen when I'm testing using Herd.


$session = $this->app->make(Session::class);

$this->assertInstanceOf(Session::class, $session);
$this->assertEquals('http://localhost/test?utm_campaign=foo&utm_medium=referral&utm_source=linkedin.com', $session->url());
}

#[Test]
public function it_writes_session_if_a_nocache_tag_is_used()
{
Expand Down