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

Add $option for specifying a nonce for livewire's script tag #1156

Merged
merged 1 commit into from
Jun 24, 2020
Merged
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
4 changes: 3 additions & 1 deletion src/LivewireManager.php
Original file line number Diff line number Diff line change
Expand Up @@ -230,11 +230,13 @@ protected function javaScriptAssets($options)
}
}

$nonce = isset($options['nonce']) ? " nonce=\"{$options['nonce']}\"" : '';

// Adding semicolons for this JavaScript is important,
// because it will be minified in production.
return <<<HTML
{$assetWarning}
<script src="{$fullAssetPath}" data-turbolinks-eval="false"></script>
<script src="{$fullAssetPath}" data-turbolinks-eval="false"{$nonce}></script>
<script data-turbolinks-eval="false">
window.livewire = new Livewire({$jsonEncodedOptions});
window.livewire_app_url = '{$appUrl}';
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
use Livewire\Livewire;
use Illuminate\Support\Facades\View;

class LivewireUsesProperAppAndAssetsPathTest extends TestCase
class LivewireAssetsDirectiveTest extends TestCase
{
/** @test */
public function livewire_js_is_unminified_when_app_is_in_debug_mode()
Expand Down Expand Up @@ -82,4 +82,17 @@ public function asset_url_passed_into_blade_assets_directive()
$output
);
}

/** @test */
public function nonce_passed_into_directive_gets_added_as_script_tag_attribute()
{
$output = View::make('assets-directive', [
'options' => ['nonce' => 'foobarnonce'],
])->render();

$this->assertStringContainsString(
'nonce="foobarnonce">',
$output
);
}
}