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

Adapts html to the inertia style to update headers and scripts #325

Open
wants to merge 3 commits into
base: master
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
16 changes: 16 additions & 0 deletions src/SEOTools/Integrations/Inertia.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
<?php

declare(strict_types=1);

namespace Artesaos\SEOTools\Integrations;

final class Inertia
{
public function convertHeadToInertiaStyle(string $seo)
{
$pattern = '/<(meta|script|title|link)/';
$replacement = '<$1 inertia';

return preg_replace($pattern, $replacement, $seo);
}
}
7 changes: 6 additions & 1 deletion src/SEOTools/SEOTools.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
namespace Artesaos\SEOTools;

use Artesaos\SEOTools\Contracts\SEOTools as SEOContract;
use Artesaos\SEOTools\Integrations\Inertia;
use Illuminate\Support\Traits\Macroable;

/**
Expand All @@ -13,7 +14,7 @@
class SEOTools implements SEOContract
{
use Macroable;

/**
* {@inheritdoc}
*/
Expand Down Expand Up @@ -135,6 +136,10 @@ public function generate($minify = false)
// Use jsonLdMulti by default; since it is just a wrapper
$html .= $this->jsonLdMulti()->generate();

if (config('seotools.inertia') === true) {
$html = app(Inertia::class)->convertHeadToInertiaStyle($html);
}

return ($minify) ? str_replace(PHP_EOL, '', $html) : $html;
}
}
1 change: 1 addition & 0 deletions src/resources/config/seotools.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
*/

return [
'inertia' => env('SEO_TOOLS_INERTIA', false),
'meta' => [
/*
* The default configurations to be used by the meta generator.
Expand Down
66 changes: 66 additions & 0 deletions tests/SEOTools/Integrations/InertiaTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
<?php

declare(strict_types=1);

namespace Artesaos\SEOTools\Tests\Integrations;

use Artesaos\SEOTools\Integrations\Inertia;
use Artesaos\SEOTools\Tests\BaseTest;

class InertiaTest extends BaseTest
{
/***
* @var Inertia
*/
protected $inertia;
protected function setUp(): void
{
parent::setUp();

$this->inertia = $this->app->make(Inertia::class);
}

public function test_convert_meta_to_inertia_style()
{
$seo = '<meta name="keywords" content="viewing, mobile TV, iphone, ipad">';

$expected = '<meta inertia name="keywords" content="viewing, mobile TV, iphone, ipad">';

$converted = $this->inertia->convertHeadToInertiaStyle($seo);

$this->assertEquals($expected, $converted);
}

public function test_convert_canonical_link_to_inertia_style()
{
$seo = '<link rel="canonical" href="https://www.example.com">';

$expected = '<link inertia rel="canonical" href="https://www.example.com">';

$converted = $this->inertia->convertHeadToInertiaStyle($seo);

$this->assertEquals($expected, $converted);
}

public function test_convert_title_to_inertia_style()
{
$seo = '<title>Title</title>';

$expected = '<title inertia>Title</title>';

$converted = $this->inertia->convertHeadToInertiaStyle($seo);

$this->assertEquals($expected, $converted);
}

public function test_convert_script_to_inertia_style()
{
$seo = '<script type="application/ld+json">{"@context":"https://schema.org","@type":"WebPage","name":"example"}</script>';

$expected = '<script inertia type="application/ld+json">{"@context":"https://schema.org","@type":"WebPage","name":"example"}</script>';

$converted = $this->inertia->convertHeadToInertiaStyle($seo);

$this->assertEquals($expected, $converted);
}
}