Skip to content

Use defer attribute on all script tags, remove inline JS #1108

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

Merged
merged 3 commits into from
Oct 25, 2018

Conversation

adamchainz
Copy link
Contributor

I'm deploying the experimental Feature-Policy on my site. I tried disabling the sync-script policy, which blocks HTML-parse-blocking JavaScript, and django-debug-toolbar broke because its script tags aren't async or deferred.

There's a bit more information about this feature, and a demo at https://feature-policy-demos.appspot.com/sync-script.html?on

I've added defer to all the <script> tags, which makes them execute after HTML parsing has finished, in order. There was also one inline script for redirects which I've extracted - this would break for sync-script but also for use of Content-Security-Policy blocking inline JavaScript, which has been supported since #704 but it seems this was missed.

Tested by using the example app, redirect interception worked and every panel loaded and looked right.

I'm deploying the experimental `Feature-Policy` on my site. I tried disabling the `sync-script` policy, which blocks HTML-parse-blocking JavaScript, and django-debug-toolbar broke because its script tags aren't async or deferred.

There's a bit more information about this feature, and a demo at https://feature-policy-demos.appspot.com/sync-script.html?on

I've added `defer` to all the `<script>` tags, which makes them execute after HTML parsing has finished, in order. There was also one inline script for redirects which I've extracted - this would break for `sync-script` but also for use of `Content-Security-Policy` blocking inline JavaScript, which has been supported since django-commons#704 but it seems this was missed.

Tested by using the `example` app, redirect interception worked and every panel loaded and looked right.
@adamchainz
Copy link
Contributor Author

@graingert you added CSP and like security features, perhaps you'd like to check this?

@@ -0,0 +1,3 @@
document.addEventListener('DOMContentLoaded', function () {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This should be redundant with defer

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Idk how far back django-debug-toolbar tries to support, but ancient browsers don't support defer, so best to leave it here?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

https://caniuse.com/#feat=script-defer goes back to IE6

Also the script tag is below the element so even if it fell back to sync it would work

@@ -0,0 +1,3 @@
document.addEventListener('DOMContentLoaded', function () {
document.getElementById('redirect_to').focus();
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is focussing a link not a form element :(

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Dang

@matthiask matthiask merged commit 4618f82 into django-commons:master Oct 25, 2018
@matthiask
Copy link
Member

Thanks!

@adamchainz adamchainz deleted the scripts_defer branch October 25, 2018 15:30
@adamchainz
Copy link
Contributor Author

No worries, thanks for active maintenance!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants