Skip to content
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
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
vendor
.idea
.DS_Store
composer.lock
4 changes: 2 additions & 2 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
"description": "Embed your Livewire components anywhere.",
"require": {
"php": "^8.1",
"livewire/livewire": "^v3.4.7"
"livewire/livewire": "^3.4.7|^4.0"
},
"license": "MIT",
"autoload": {
Expand All @@ -26,4 +26,4 @@
},
"minimum-stability": "dev",
"prefer-stable": true
}
}
53 changes: 24 additions & 29 deletions js/wire-extender.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,7 @@ let componentAssets;
let currentScript = document.currentScript;
let livewireStarted = false;

function getUri(append = '')
{
function getUri(append = '') {
let uri = document.querySelector('[data-uri]')?.getAttribute('data-uri');

if (!uri) {
Expand All @@ -18,26 +17,14 @@ function getUri(append = '')
return uri + append;
}

function getLivewireAssetUri()
{
return document.querySelector('[data-livewire-asset-uri]')?.getAttribute('data-livewire-asset-uri') ?? getUri('livewire/livewire.min.js');
}

function getLivewireUpdateUri()
{
return document.querySelector('[data-update-uri]')?.getAttribute('data-update-uri') ?? getUri('livewire/update');
}

function getEmbedUri()
{
function getEmbedUri() {
const base = document.querySelector('[data-embed-uri]')?.getAttribute('data-embed-uri') ?? getUri('livewire/embed');
const queryString = window.location.search;

return base + queryString;
}

function injectLivewire()
{
function injectLivewire(script) {
if (window.Livewire || livewireStarted) {
return;
}
Expand All @@ -46,10 +33,20 @@ function injectLivewire()
style.innerHTML = '[wire\\:loading][wire\\:loading], [wire\\:loading\\.delay][wire\\:loading\\.delay], [wire\\:loading\\.inline-block][wire\\:loading\\.inline-block], [wire\\:loading\\.inline][wire\\:loading\\.inline], [wire\\:loading\\.block][wire\\:loading\\.block], [wire\\:loading\\.flex][wire\\:loading\\.flex], [wire\\:loading\\.table][wire\\:loading\\.table], [wire\\:loading\\.grid][wire\\:loading\\.grid], [wire\\:loading\\.inline-flex][wire\\:loading\\.inline-flex] {display: none;}[wire\\:loading\\.delay\\.none][wire\\:loading\\.delay\\.none], [wire\\:loading\\.delay\\.shortest][wire\\:loading\\.delay\\.shortest], [wire\\:loading\\.delay\\.shorter][wire\\:loading\\.delay\\.shorter], [wire\\:loading\\.delay\\.short][wire\\:loading\\.delay\\.short], [wire\\:loading\\.delay\\.default][wire\\:loading\\.delay\\.default], [wire\\:loading\\.delay\\.long][wire\\:loading\\.delay\\.long], [wire\\:loading\\.delay\\.longer][wire\\:loading\\.delay\\.longer], [wire\\:loading\\.delay\\.longest][wire\\:loading\\.delay\\.longest] {display: none;}[wire\\:offline][wire\\:offline] {display: none;}[wire\\:dirty]:not(textarea):not(input):not(select) {display: none;}:root {--livewire-progress-bar-color: #2299dd;}[x-cloak] {display: none !important;}';
document.head.appendChild(style);

const temp = document.createElement('div');
temp.innerHTML = script.trim();

const scriptEl = temp.querySelector('script');

livewireScript = document.createElement('script');
livewireScript.src = getLivewireAssetUri();
livewireScript.dataset.csrf = '';
livewireScript.dataset.updateUri = getLivewireUpdateUri();
livewireScript.src = scriptEl.src;

for (let attr of scriptEl.attributes) {
if (attr.name.startsWith('data-')) {
livewireScript.setAttribute(attr.name, attr.value);
}
}

document.body.appendChild(livewireScript);
}

Expand All @@ -58,7 +55,7 @@ function waitForLivewireAndStart() {
return;
}

if(window.Livewire) {
if (window.Livewire) {
startLivewire();
}
livewireScript.onload = async function () {
Expand All @@ -68,20 +65,16 @@ function waitForLivewireAndStart() {
livewireStarted = true;
}

async function startLivewire(assets)
{
async function startLivewire(assets) {
Livewire.hook('request', ({ options }) => {
options.headers['X-Wire-Extender'] = '';
options.credentials = 'include';
})
await Livewire.triggerAsync('payload.intercept', {assets: componentAssets});
await Livewire.triggerAsync('payload.intercept', { assets: componentAssets });
Livewire.start();
}

function renderComponents(components)
{
injectLivewire();

function renderComponents(components) {
fetch(getEmbedUri(), {
method: 'POST',
headers: {
Expand All @@ -90,7 +83,7 @@ function renderComponents(components)
body: JSON.stringify({
components: components
}),
'credentials': 'include'
'credentials': 'include'
})
.then(response => response.json())
.then(data => {
Expand All @@ -100,11 +93,13 @@ function renderComponents(components)
}

componentAssets = data.assets;

injectLivewire(data.script);
waitForLivewireAndStart();
});
}

document.addEventListener('DOMContentLoaded', function() {
document.addEventListener('DOMContentLoaded', function () {
let components = [];

document.querySelectorAll('livewire').forEach((el) => {
Expand Down
3 changes: 2 additions & 1 deletion routes/api.php
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
<?php

use WireElements\WireExtender\Http\Controllers\EmbedController;
use WireElements\WireExtender\Http\Controllers\InjectController;

Route::any('livewire/embed', EmbedController::class);
Route::any('livewire/embed', EmbedController::class);
1 change: 1 addition & 0 deletions src/Http/Controllers/EmbedController.php
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ public function __invoke(Request $request)
return [
'components' => $components,
'assets' => SupportScriptsAndAssets::getAssets(),
'script' => Blade::render('@livewireScripts'),
];
}

Expand Down
11 changes: 10 additions & 1 deletion src/WireExtender.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ class WireExtender
public static function isEmbeddable($component): bool
{
try {
$reflectionClass = new ReflectionClass(app(ComponentRegistry::class)->new($component));
$reflectionClass = new ReflectionClass($this->resolveComponentClass($component));
$embedAttribute = $reflectionClass->getAttributes(Embeddable::class)[0] ?? null;

return is_null($embedAttribute) === false;
Expand All @@ -22,4 +22,13 @@ public static function isEmbeddable($component): bool

return true;
}

protected function resolveComponentClass(string $component): string
{
if (class_exists(ComponentRegistry::class)) {
return app(ComponentRegistry::class)->getClass($component);
}

return app('livewire.finder')->resolveClassComponentClassName($component);
}
}