-
Notifications
You must be signed in to change notification settings - Fork 327
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
Ensure font is not loaded twice on slow networks #1242
Ensure font is not loaded twice on slow networks #1242
Conversation
608a768
to
82d1b83
Compare
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Have we tested this with GOV.UK Template to make sure this behaves as expected?
src/helpers/_typography.scss
Outdated
// If the user is using the default NTA font we need to include the font-face declarations. | ||
// | ||
// We do not need to include the NTA font-face declarations if alphagov/govuk_template is being used since they will already be included. | ||
@if ($govuk-font-family == $govuk-font-family-nta and not($govuk-compatibility-govuktemplate)) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Do you think it's worth introducing a setting specifically for this, tied to compatibility mode by default, like we have $govuk-typography-use-rem
?
It'd allow users to override it, if they need to.
(I can't actually think of a reason, other than perhaps if they don't have compatibility mode on but they want to declare the font face separately because e.g. they've got a departmental CDN or something?)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yeah could be useful, I guess I was just being a bit lazy and didn't want to add and document a feature I wasn't sure people needed.
Happy to do this though.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Added this, not sure about the naming though...
src/helpers/typography.test.js
Outdated
expect(results.css.toString()).toContain(`font-family: "ntatabularnumbers"`) | ||
}) | ||
describe('should not output NTA @font-face declarations', () => { | ||
it('when a custom font-family is set', async () => { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This feels a bit backwards, because the expectation is being described in the describe
block, and the context is being described in the it
block.
I'd probably just remove the describe block and move the context into the it block?
For example:
it('does not include the font-face when a different font-family is used' ...)
src/helpers/typography.test.js
Outdated
|
||
const results = await sassRender({ data: sass, ...sassConfig }) | ||
|
||
expect(results.css.toString()).toContain(`@font-face`) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Can we cast to string once rather than three times?
@36degrees I tested this by setting the compatibility flag to true for GOV.UK Template, and including the font.css file from GOV.UK Template in the review application. I'm hoping to get some help testing this in a real world scenario from GOV.UK Publishing. |
@NickColley sure, I'll test it on gov.uk - this was sitting in the backlog for quite a while - awesome to see it tackled 🏉 |
@alex-ju if you want to test this, you can run the following npm install --save alphagov/govuk-frontend#bdd839a7 |
a95ddc2
to
8c21245
Compare
8c21245
to
bd68e7b
Compare
@NickColley tested this and works as expected 👏 |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
👍
Prior to this change if you were loading GOV.UK Frontend alongisde alphagov/govuk_template you would have two @font-face declarations that tell the browser how to load the 'nta' font. This is normally not an issue since as soon as the browser downloads a version it won't download a second copy of the font. However when a user is on a slow connection, the browser can try to download the font from the alphagov/govuk_template declaration and GOV.UK Frontend declaration. Which means that the end user will download the font twice (and it's variants) resulting in a much slower experience. This is even more of a problem for users on a slow network since their bandwidth may already be constrained. To avoid this we check if the user has indicated they are using GOV.UK Template by checking the relevant compatibility flag. If they have set this flag, we avoid outputting the declarations with the assumption that they will already have them, avoiding the chance of the font being downloaded twice. This feature can also be disabled with a top level $govuk-include-default-font-face flag to ensure that our users have full control.
bd68e7b
to
632f98c
Compare
Prior to this change if you were loading GOV.UK Frontend alongside alphagov/govuk_template you would have two @font-face declarations that tell the browser how to load the 'nta' font.
This is normally not an issue since as soon as the browser downloads a version it won't download a second copy of the font.
However when a user is on a slow connection, the browser can try to download the font from both the alphagov/govuk_template declaration and GOV.UK Frontend declaration.
Which means that the end user will download the font twice (and it's variants) resulting in a much slower experience.
This is even more of a problem for users on a slow network since their bandwidth may already be constrained.
To avoid this we check if the user has indicated they are using GOV.UK Template by checking the relevant compatibility flag.
If they have set this flag, we avoid outputting the declarations with the assumption that they will already have them, avoiding the chance of the font being downloaded twice.
We should take care to consider how this could impact bringing the new version of the NTA font @dashouse has been working on.
Fixes #1001
How to test this in the GOV.UK Frontend review application.
_generic.njk
template to load the GOV.UK Template fontSet the compatibility flag for GOV.UK Template on.
Throttle network connection in Chrome Developer tools so and check if two fonts load.
You can do the same but changing the flag set in 2. to test the different variations.