Highlighted

How to use media, preload, prefetch attribute in clientlibs

Avatar

Avatar

rubenf42159101

Avatar

rubenf42159101

rubenf42159101

20-09-2018

Hi,

We are working on improving site performance. We have performed an audit using Google Lighthouse. Our page performance score are low. There are various suggestions to improve the page speed. Some of them are

1. Adding media attribute to header link tag to load device specific CSS rather than loading all.

          <link href="other.css" rel="stylesheet" media="(min-width: 40em)">

2.  Adding preload, prefetch attribute to header link tag to load JS/CSS upfront.

          <link rel="preload" href="style.css" as="style">

          <link rel="preload" href="main.js" as="script">

3. Use Async and Defer for loading JS

Could anyone throw some light on how to use such above attributes to increase site performance as suggested by Google and various articles. We have tried with Async and Defer as explained in this article [1]. But I would to know whether "async" will really help us to gain performance since we load the JS already in footer as AEM best practice.

Thanks and Regards,

Ruben Fernando

Replies

Highlighted

Avatar

Avatar

smacdonald2008

Total Posts

12.7K

Likes

1.4K

Correct Answer

2.3K

Avatar

smacdonald2008

Total Posts

12.7K

Likes

1.4K

Correct Answer

2.3K
smacdonald2008

20-09-2018

I am discussing this with internal AEM ppl. In meantime - check this KB on performance -- Optimize AEM Site Performance

Highlighted

Avatar

Avatar

Arun_Patidar

MVP

Total Posts

2.9K

Likes

999

Correct Answer

829

Avatar

Arun_Patidar

MVP

Total Posts

2.9K

Likes

999

Correct Answer

829
Arun_Patidar
MVP

20-09-2018

Hi,

Check preload here https://developer.mozilla.org/en-US/docs/Web/HTML/Preloading_content

For Async and Defer

  • If async is present: The script is executed asynchronously with the rest of the page (the script will be executed while the page continues the parsing)
  • If async is not present and defer is present: The script is executed when the page has finished parsing
  • If neither async or defer is present: The script is fetched and executed immediately, before the browser continues parsing the page
Highlighted

Avatar

Avatar

rubenf42159101

Avatar

rubenf42159101

rubenf42159101

25-09-2018

Hi,

I went through the KB on performance already. We are working on UI front to optimise the site performance.

Could you share the updates on your discussion with internal AEM folks on how to use media, preload, prefetch attribute in clientlibs.

Thanks and Regards,

Ruben Fernando

Highlighted

Avatar

Avatar

jimmy231

Avatar

jimmy231

jimmy231

05-07-2020

Hi @smacdonald2008 ,
Is there any updates on this?

I would also like to append rel="preload" and as="style" to my css client imports. Following information here from a recent audit - https://web.dev/uses-rel-preload/?utm_source=lighthouse&utm_medium=devtools

 

Currently I'm on aem 6.3 and I load css like:

<sly data-sly-use.clientLib="/libs/granite/sightly/templates/clientlib.html" data-sly-unwrap/>

<link type="text/css" rel="stylesheet" data-sly-call="${clientLib.css @ categories='layout-example'}" data-sly-unwrap />


I can't seem to find any answers on how to do this. Is there a way to load clientlibs without doing data-sly-unwrap and my template setting what attributes I want?
Highlighted

Avatar

Avatar

BharanN

Avatar

BharanN

BharanN

20-07-2020

@Arun_Patidar @smacdonald2008 

Any update on the above query on rel="preload", even for our site the google Lighthouse report has suggested to use rel="preload" instead of rel="stylesheet".

Is there any other post where answer to this has been given, can you redirect to that post by posting that link over here.

Highlighted

Avatar

Avatar

Arun_Patidar

MVP

Total Posts

2.9K

Likes

999

Correct Answer

829

Avatar

Arun_Patidar

MVP

Total Posts

2.9K

Likes

999

Correct Answer

829
Arun_Patidar
MVP

20-07-2020

Highlighted

Avatar

Avatar

BharanN

Avatar

BharanN

BharanN

22-07-2020

@Arun_Patidar Thanks for the response. Will try this on my local.

But however was looking at an OOTB solution for this, like by adding a property or just some conditions in the Sightly code.