Created
April 30, 2017 09:47
-
-
Save ndavison/29708215fd5b4af2f2e2379dd2c24c96 to your computer and use it in GitHub Desktop.
GravCMS convert inline JS into sourced file JS
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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