Skip to content

Instantly share code, notes, and snippets.

@ndavison
Created April 30, 2017 09:47
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ndavison/29708215fd5b4af2f2e2379dd2c24c96 to your computer and use it in GitHub Desktop.
Save ndavison/29708215fd5b4af2f2e2379dd2c24c96 to your computer and use it in GitHub Desktop.
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