Instantly share code, notes, and snippets.

Embed
What would you like to do?
GravCMS convert inline JS into sourced file JS
diff --git a/system/src/Grav/Common/Assets.php b/system/src/Grav/Common/Assets.php
index 12efc1a..383cd8d 100644
--- a/system/src/Grav/Common/Assets.php
+++ b/system/src/Grav/Common/Assets.php
@@ -678,15 +678,20 @@ class Assets
}
}
- // Render Inline JS
+ // Convert Inline JS into a sourced JS file
+ $buffer = '';
foreach ($this->inline_js as $inline) {
if ($group && $inline['group'] == $group) {
- $inline_js .= $inline['asset'] . "\n";
+ $buffer .= $inline['asset'] . "\n";
}
}
-
- if ($inline_js) {
- $output .= "\n<script>\n" . $inline_js . "\n</script>\n";
+ if ($buffer) {
+ $inlineFile = md5($buffer) . '.js';
+ if (!file_exists($this->assets_dir . $inlineFile)) {
+ file_put_contents($this->assets_dir . $inlineFile, $buffer);
+ }
+ $relative_path = "{$this->base_url}{$this->assets_url}/{$inlineFile}";
+ $output .= '<script src="' . $relative_path . '" type="text/javascript"></script>' . "\n";
}
return $output;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment