Skip to content

Commit

Permalink
Fix Octane caching issue (#460)
Browse files Browse the repository at this point in the history
* Revert #417

* Cache generated named route list in Ziggy class

* Remove unused `$port` property and reorder methods
  • Loading branch information
bakerkretzmar authored Sep 24, 2021
1 parent b361e8a commit 2188a26
Show file tree
Hide file tree
Showing 5 changed files with 42 additions and 26 deletions.
10 changes: 3 additions & 7 deletions src/BladeRouteGenerator.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,28 +5,24 @@
class BladeRouteGenerator
{
public static $generated;
public static $payload;

public function generate($group = null, $nonce = null)
{
if (! static::$payload) {
static::$payload = new Ziggy($group);
}
$payload = new Ziggy($group);

$nonce = $nonce ? ' nonce="' . $nonce . '"' : '';

if (static::$generated) {
return $this->generateMergeJavascript(json_encode(static::$payload->toArray()['routes']), $nonce);
return $this->generateMergeJavascript(json_encode($payload->toArray()['routes']), $nonce);
}

$ziggy = static::$payload->toJson();
$routeFunction = $this->getRouteFunction();

static::$generated = true;

return <<<HTML
<script type="text/javascript"{$nonce}>
const Ziggy = {$ziggy};
const Ziggy = {$payload->toJson()};
$routeFunction
</script>
Expand Down
17 changes: 13 additions & 4 deletions src/Ziggy.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,8 @@

class Ziggy implements JsonSerializable
{
protected $port;
protected static $cache;

protected $url;
protected $group;
protected $routes;
Expand All @@ -22,9 +23,17 @@ public function __construct($group = null, string $url = null)
$this->group = $group;

$this->url = rtrim($url ?? url('/'), '/');
$this->port = parse_url($this->url)['port'] ?? null;

$this->routes = $this->nameKeyedRoutes();
if (! static::$cache) {
static::$cache = $this->nameKeyedRoutes();
}

$this->routes = static::$cache;
}

public static function clearRoutes()
{
static::$cache = null;
}

private function applyFilters($group)
Expand Down Expand Up @@ -120,7 +129,7 @@ public function toArray(): array
{
return [
'url' => $this->url,
'port' => $this->port,
'port' => parse_url($this->url)['port'] ?? null,
'defaults' => method_exists(app('url'), 'getDefaultParameters')
? app('url')->getDefaultParameters()
: [],
Expand Down
22 changes: 15 additions & 7 deletions tests/TestCase.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,25 @@
use Closure;
use Orchestra\Testbench\TestCase as OrchestraTestCase;
use PHPUnit\Framework\Constraint\StringContains;
use Tightenco\Ziggy\Ziggy;
use Tightenco\Ziggy\ZiggyServiceProvider;

class TestCase extends OrchestraTestCase
{
public static function assertStringContainsString(string $needle, string $haystack, string $message = ''): void
{
$constraint = new StringContains($needle, false);

static::assertThat($haystack, $constraint, $message);
}

protected function tearDown(): void
{
Ziggy::clearRoutes();

parent::tearDown();
}

protected function getPackageProviders($app)
{
return [ZiggyServiceProvider::class];
Expand All @@ -27,11 +42,4 @@ protected function noop(): Closure
return '';
};
}

public static function assertStringContainsString(string $needle, string $haystack, string $message = ''): void
{
$constraint = new StringContains($needle, false);

static::assertThat($haystack, $constraint, $message);
}
}
8 changes: 0 additions & 8 deletions tests/Unit/BladeRouteGeneratorTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,13 +9,6 @@

class BladeRouteGeneratorTest extends TestCase
{
protected function tearDown(): void
{
BladeRouteGenerator::$payload = null;

parent::tearDown();
}

/** @test */
public function can_resolve_generator_from_container()
{
Expand Down Expand Up @@ -126,7 +119,6 @@ public function can_generate_routes_for_given_group_or_groups()
$this->assertArrayHasKey('posts.show', $ziggy['routes']);

BladeRouteGenerator::$generated = false;
BladeRouteGenerator::$payload = null;
$output = (new BladeRouteGenerator)->generate(['guest', 'admin']);
$ziggy = json_decode(Str::after(Str::before($output, ";\n\n"), ' = '), true);

Expand Down
11 changes: 11 additions & 0 deletions tests/Unit/ZiggyTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -491,4 +491,15 @@ public function route_payload_can_automatically_json_itself_in_a_response()
$response->assertSuccessful();
$this->assertSame((new Ziggy)->toJson(), $response->getContent());
}

/** @test */
public function can_cache_compiled_route_list_internally_on_repeated_instantiations()
{
$routes = (new Ziggy)->toArray()['routes'];

app('router')->get('/users', $this->noop())->name('users.index');
app('router')->getRoutes()->refreshNameLookups();

$this->assertSame($routes, (new Ziggy)->toArray()['routes']);
}
}

0 comments on commit 2188a26

Please sign in to comment.