Has anyone tried using script defer with phpBB?

Need some custom code changes to the phpBB core simple enough that you feel doesn't require an extension? Then post your request here so that community members can provide some assistance.

NOTE: NO OFFICIAL SUPPORT IS PROVIDED IN THIS SUB-FORUM
Forum rules
READ: phpBB.com Board-Wide Rules and Regulations

NOTE: NO OFFICIAL SUPPORT IS PROVIDED IN THIS SUB-FORUM
Post Reply
User avatar
Gumboots
Registered User
Posts: 211
Joined: Fri Oct 11, 2019 1:59 am

Has anyone tried using script defer with phpBB?

Post by Gumboots » Thu Nov 14, 2019 9:40 pm

Just wondering if anyone has messed around with deferred loading of external javascript files in phpBB?

By this I don't mean sticking the tags at the end of the body tag (as per default) but putting the tags in the head and using the defer attribute on them. This seems to have some performance advantages (loading of js files becomes non-blocking, giving faster times to domInteractive) but like anything may have some pitfalls for dependencies, etc.

Has any adventurous soul tried it? :D

KYPREO
Registered User
Posts: 162
Joined: Fri Feb 02, 2018 9:56 am
Contact:

Re: Has anyone tried using script defer with phpBB?

Post by KYPREO » Thu Nov 14, 2019 10:14 pm

If you use Cloudflare, you can turn on Rocket Loader which does precisely this: https://support.cloudflare.com/hc/en-us ... Loader-do-
phpBB user since 2002
www.AusRotary.com

User avatar
Gumboots
Registered User
Posts: 211
Joined: Fri Oct 11, 2019 1:59 am

Re: Has anyone tried using script defer with phpBB?

Post by Gumboots » Thu Nov 14, 2019 10:17 pm

I don't use Cloudflare. ;)
I don't have any problem coding the script tags. I'm just wondering if anyone has found pitfalls.

(And I see that Cloudflare doesn't know how to code a web page anyway. FFS people, putting a background on the body tag is CSS 101.)

User avatar
JLA
Registered User
Posts: 515
Joined: Tue Nov 16, 2004 5:23 pm
Location: USA
Name: JLA FORUMS
Contact:

Re: Has anyone tried using script defer with phpBB?

Post by JLA » Sat Nov 16, 2019 5:05 pm

Gumboots wrote:
Thu Nov 14, 2019 9:40 pm
Just wondering if anyone has messed around with deferred loading of external javascript files in phpBB?

By this I don't mean sticking the tags at the end of the body tag (as per default) but putting the tags in the head and using the defer attribute on them. This seems to have some performance advantages (loading of js files becomes non-blocking, giving faster times to domInteractive) but like anything may have some pitfalls for dependencies, etc.

Has any adventurous soul tried it? :D
Can you give an example on how you would try to do this?

Thanks

User avatar
Gumboots
Registered User
Posts: 211
Joined: Fri Oct 11, 2019 1:59 am

Re: Has anyone tried using script defer with phpBB?

Post by Gumboots » Sat Nov 16, 2019 8:14 pm

Sure. An obvious one would be jQuery, as that's the biggest js file. Instead of the default, which is basically:

Code: Select all

<script src="{T_JQUERY_LINK}"></script>
...a bit more stuff...
</body>
You'd be using:

Code: Select all

<head>
...meta tags, etc...
<script src="{T_JQUERY_LINK}" defer></script>
That's the gist of it. What it allows is for js files to not block rendering, and not block downloading of other files. Any script tag that has an external src and is set to defer will download independent of whatever else is going on, then will execute on domInteractive instead of executing as soon as it is downloaded. There a decent explanation here: https://flaviocopes.com/javascript-async-defer/

User avatar
JLA
Registered User
Posts: 515
Joined: Tue Nov 16, 2004 5:23 pm
Location: USA
Name: JLA FORUMS
Contact:

Re: Has anyone tried using script defer with phpBB?

Post by JLA » Sat Nov 16, 2019 8:50 pm

Gumboots wrote:
Sat Nov 16, 2019 8:14 pm
Sure. An obvious one would be jQuery, as that's the biggest js file. Instead of the default, which is basically:

Code: Select all

<script src="{T_JQUERY_LINK}"></script>
...a bit more stuff...
</body>
You'd be using:

Code: Select all

<head>
...meta tags, etc...
<script src="{T_JQUERY_LINK}" defer></script>
That's the gist of it. What it allows is for js files to not block rendering, and not block downloading of other files. Any script tag that has an external src and is set to defer will download independent of whatever else is going on, then will execute on domInteractive instead of executing as soon as it is downloaded. There a decent explanation here: https://flaviocopes.com/javascript-async-defer/
If the script is hosted locally would you want to do this or not?

User avatar
Gumboots
Registered User
Posts: 211
Joined: Fri Oct 11, 2019 1:59 am

Re: Has anyone tried using script defer with phpBB?

Post by Gumboots » Sat Nov 16, 2019 8:54 pm

If it's not cached, it still has to be downloaded. Doesn't matter where it's coming from.

User avatar
JLA
Registered User
Posts: 515
Joined: Tue Nov 16, 2004 5:23 pm
Location: USA
Name: JLA FORUMS
Contact:

Re: Has anyone tried using script defer with phpBB?

Post by JLA » Sat Nov 16, 2019 9:00 pm

We use a couple scripts for some things that go on in our header menus and media players. This can be seen on pretty much any of our pages. Would doing this make a significant enough difference to justify the effort to make the change? Any cons to doing this? Thanks for the answers – never occurred to think about this before.

User avatar
JLA
Registered User
Posts: 515
Joined: Tue Nov 16, 2004 5:23 pm
Location: USA
Name: JLA FORUMS
Contact:

Re: Has anyone tried using script defer with phpBB?

Post by JLA » Sat Nov 16, 2019 11:40 pm

Ok - so have tried this on our viewtopic pages for both mobile and desktop. Still need to address an older script on desktop but...

Page insights reported a higher number with these scripts removed from just before the footer and put into the header with "defer"

The header menus seem to be working properly so it might be good.

Will keep an eye on it and see if anyone complains.

Thanks

User avatar
Gumboots
Registered User
Posts: 211
Joined: Fri Oct 11, 2019 1:59 am

Re: Has anyone tried using script defer with phpBB?

Post by Gumboots » Sun Nov 17, 2019 12:42 am

Ok. Must admit I haven't tried it myself yet. I was kinda hoping there had been previous guinea pigs so I knew where the traps were. :lol:

User avatar
JLA
Registered User
Posts: 515
Joined: Tue Nov 16, 2004 5:23 pm
Location: USA
Name: JLA FORUMS
Contact:

Re: Has anyone tried using script defer with phpBB?

Post by JLA » Sun Nov 17, 2019 12:52 am

Looking through the documentation – can’t see anything that looks of concern. The JavaScript on our view topic page basically just handles a simple little pop out header menu. We made the change that you suggested and everything is working as expected and seems to be loading faster as well.

User avatar
Gumboots
Registered User
Posts: 211
Joined: Fri Oct 11, 2019 1:59 am

Re: Has anyone tried using script defer with phpBB?

Post by Gumboots » Sun Nov 17, 2019 7:23 pm

RIghto. I have a test site anyway, so I'll just go nuts on that and see if anything breaks. Should be amusing.

User avatar
JLA
Registered User
Posts: 515
Joined: Tue Nov 16, 2004 5:23 pm
Location: USA
Name: JLA FORUMS
Contact:

Re: Has anyone tried using script defer with phpBB?

Post by JLA » Sun Nov 17, 2019 7:32 pm

Google also gave a suggestion about doing deferral on CSS files but I’m not sure if that will be such a good idea since a lot of the layout is dependent upon CSS and do not want it to look messed up during the load

User avatar
Gumboots
Registered User
Posts: 211
Joined: Fri Oct 11, 2019 1:59 am

Re: Has anyone tried using script defer with phpBB?

Post by Gumboots » Sun Nov 17, 2019 8:22 pm

Yes I've been reading around and there are quite a few tricks available if you want to get into splitting things down*. At this stage I don't want to try all of them, but there are some interesting ideas out there. I'm not sure how useful some of them would be for a basic phpBB forum though.

*I think what you're referring to is the option of splitting large CSS files into "above the fold" and "below the fold", and loading them sequentially.

Post Reply

Return to “phpBB Custom Coding”