Skip to content

Commit

Permalink
Adds deferred script loading and autoloading
Browse files Browse the repository at this point in the history
  • Loading branch information
drmad committed Aug 13, 2024
1 parent 3a4951f commit 1e1c312
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 2 deletions.
11 changes: 10 additions & 1 deletion src/Html.php
Original file line number Diff line number Diff line change
Expand Up @@ -112,6 +112,15 @@ public function prependJs(...$source)
$this->js_sources = [...$source, ...$this->js_sources];
}

/**
* Adds one or multiple JavaScript sources loaded with 'defer'
*/
public function addDeferredJs(...$sources) {
foreach ($sources as $source) {
$this->addScript($source, defer: true);
}
}

/**
* Adds a <LINK> tag
*/
Expand Down Expand Up @@ -356,4 +365,4 @@ public function insertContent(): string
return $this->content;
}

}
}
16 changes: 15 additions & 1 deletion src/View.php
Original file line number Diff line number Diff line change
Expand Up @@ -141,6 +141,20 @@ public function render(): string
if ($found) {
$html->addJs($found);
}

// Buscamos también un JS con __defer
$found = $this->resource_locator->find(
$this->source . '__defer',
type: 'js',
ext: ['js'],
return_relative_resource_name: true,
);

if ($found) {
$html->addDeferredJs($found);
}


}

return $html->render();
Expand Down Expand Up @@ -176,4 +190,4 @@ public function renderHttpStatus($code, $args = []): noreturn
$this->renderResponse()->withStatus($code)->send();
exit;
}
}
}

0 comments on commit 1e1c312

Please sign in to comment.