-
Notifications
You must be signed in to change notification settings - Fork 446
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Separate third party inline scripts #241
Separate third party inline scripts #241
Conversation
9d361f6
to
211dcdb
Compare
- and typo in `next-config.js`
Google Analytics and WidgetPack Rating on Pjax sites seem to have been broken ever since Pjax is introduced, long before this PR.
An unexpected thing is that Widget Pack's official script uses |
- Not listen to Pjax related events since GrowingIO listens to URL change events on its own
- Resolve a conflict with copy_button
- Replace `getScript` by original `getScriptPromise`, `loadComments` by original `loadCommentsPromise` - Expose less properties of CONFIG
- Deprecate quicklink.ignores but not obsolete it - Rename `load-config.js` in source/js/ to `config.js`
I have an idea: hexo.extend.filter.register('after_generate', () => {
// minify js files
hexo.route.list().filter(path => (all_js.includes(path) && !used_js.includes(path))).forEach(path => {
hexo.route.remove(path);
});
});
|
I tried before but failed. It seems like I might be wrong anyway. |
Status Update: Function and Regex in config file's Theme config file has been updated: Lines 554 to 557 in 7a5b348
If used, error message will be shown as: hexo-theme-next/source/js/third-party/quicklink.js Lines 15 to 16 in 7a5b348
Users using this config should turn to configure |
The rest comment systems are hard for me to test, I suggest merging now and getting some feedback. |
To NexT plugin authors, Sorry that I forgot the plugins which are outside this repo and use As you may have seen in the PR description,
// Before:
NexT.utils.getScript('example.js', onLoad);
// After:
NexT.utils.getScript('example.js').then(onLoad);
NexT.utils.getScript('example.js').then(onLoad, onError); // better The // Before:
NexT.utils.getScript('example.js', onLoad, window.example);
// After:
NexT.utils.getScript('example.js', { condition: window.example }).then(onLoad, onError);
// Before:
const script = document.createElement('script');
script.async = true;
script.dataset.foo = 'bar';
script.onload = onLoad;
script.url = 'example.js';
document.getElementById('example').appendChild(script);
// After:
NexT.utils.getScript('example.js', {
attributes: {
async: true,
dataset: { foo: 'bar' }
},
parentNode: document.getElementById('example')
}).then(onLoad, onError); To have more precise and elegant control,
and when needed,
{# Before: #}
{%- if theme.example.enable %}
<script src="example.js"></script>
{%- if is_home() %}
<link rel="stylesheet" href="example.css">
<script src="example.home.js"></script>
<script>
window.example.home.load({{ theme.example.id }});
</script>
{%- endif %}
{%- endif %}
{# After: #}
{%- if theme.example.enable %}
<link rel="stylesheet" href="example.css">
<script{{ pjax }} src="example.js"></script>
{{ next_data('example', theme.example) }}
<script>
document.addEventListener('page:loaded', () => {
if (!CONFIG.page.isHome) return;
NexT.utils.getScript('example.home.js', { condition: window.example.home })
.then(() => {
window.example.home.load(CONFIG.example.id);
});
});
</script>
{%- endif %} The new Besides, as the initial focus of this PR, it's recommended to seperate the inline scripts into single file(s). If you want to keep supporting older versions of NexT, see #259 (comment), or use your previous code directly when {%- if next_data %}
{# For NexT>=8.4.0 #}
{%- else %}
{# For NexT<=8.3.0 #}
{%- endif %} Kind regards, |
PR Checklist
The main problem is there are so many third party scrips to test.
PR Type
What is the current behavior?
Continue working on #226! Another pull request for more dedicated tests!
Issue resolved: #220
What is the new behavior?
Links below, if not specific, redirect to the related code line(s) / file.
source/js/third-party/[*/]*.js
.CONFIG.[third-party-name].*
and are updated on the first evaluation after a page load.next_data
. This new helper uses<script type="application/json">
to store data, which is documented in MDN, discussed in Stack Overflow, and doesn't violate any CSP rule.NexT.utils.getScriptPromise
to load scrips instead of inserting them to DOM directly without callback control. And it potentially replacesNexT.utils.getScript
in all use cases.NexT.utils.loadCommentsPromise
potentially replacesNexT.utils.loadComments
.bodyEnd
, along with math scripts and quicklink, will not be reloaded by Pjax anymore. Corresponding third party code have been edited for this change in their own files. Corresponding Pjax code, line 19 insource/js/pjax.js
, will be edited in future commits.IMPORTANT & Requires Discussions:
scripts/filters/comment/*.js
to corresponding position insidelayout/_third-party/comments/*.njk
. Potentially, scripts inscripts/filters/comment/*.js
can be combined into one if this is done. Besides, to achieve this, alayout/_third-party/comments/common.njk
has been created to replacescripts/filters/comment/common.js
.Not finished yet, possible samples have been made in
layout/_third-party/comments/disqus.njk
withscripts/filters/comment/disqus.js
,layout/_third-party/comments/changyan.njk
withscripts/filters/comment/changyan.js
.minify.js
Updated) Should we keep usingminify.js
to clean third party scripts? Or add third party scripts to route only when needed? If the latter is better, how should we achieve it?quicklink.js
,quicklink.ignores
is used and may include functions by design. But functions can not be parsed from strings safely in JS runtime. Allow usingeval
orFunction
here, disallow functions inignores
, or ...?