Skip to content
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

Merged

Conversation

NickColley
Copy link
Contributor

@NickColley NickColley commented Mar 7, 2019

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.

  1. Add the following HTML to the _generic.njk template to load the GOV.UK Template font
<link rel="stylesheet" media="all" href="/govuk_template/assets/stylesheets/fonts.css?0.26.0">
  1. Set the compatibility flag for GOV.UK Template on.

  2. 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.

@govuk-design-system-ci govuk-design-system-ci temporarily deployed to govuk-frontend-review-pr-1242 March 7, 2019 18:26 Inactive
@NickColley NickColley force-pushed the ensure-font-is-not-loaded-twice-with-compatibility-mode branch from 608a768 to 82d1b83 Compare March 7, 2019 18:36
@govuk-design-system-ci govuk-design-system-ci temporarily deployed to govuk-frontend-review-pr-1242 March 7, 2019 18:36 Inactive
@NickColley NickColley changed the title Test when NTA @font-face declaration should output Ensure font is not loaded twice on slow networks Mar 7, 2019
@NickColley
Copy link
Contributor Author

@injms @alex-ju if I did a test release branch of this would you be interested in testing it to confirm it resolves things your end?

@NickColley NickColley marked this pull request as ready for review March 7, 2019 18:39
@NickColley NickColley added this to the [NEXT] milestone Mar 11, 2019
Copy link
Contributor

@36degrees 36degrees left a 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.test.js Outdated Show resolved Hide resolved
// 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)) {
Copy link
Contributor

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?)

Copy link
Contributor Author

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.

Copy link
Contributor Author

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...

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 () => {
Copy link
Contributor

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' ...)


const results = await sassRender({ data: sass, ...sassConfig })

expect(results.css.toString()).toContain(`@font-face`)
Copy link
Contributor

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?

@NickColley
Copy link
Contributor Author

NickColley commented Mar 11, 2019

@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.

@govuk-design-system-ci govuk-design-system-ci temporarily deployed to govuk-frontend-review-pr-1242 March 11, 2019 17:10 Inactive
@govuk-design-system-ci govuk-design-system-ci temporarily deployed to govuk-frontend-review-pr-1242 March 11, 2019 17:51 Inactive
@govuk-design-system-ci govuk-design-system-ci temporarily deployed to govuk-frontend-review-pr-1242 March 11, 2019 17:58 Inactive
@alex-ju
Copy link
Contributor

alex-ju commented Mar 12, 2019

@NickColley sure, I'll test it on gov.uk - this was sitting in the backlog for quite a while - awesome to see it tackled 🏉

@NickColley
Copy link
Contributor Author

@alex-ju if you want to test this, you can run the following

npm install --save alphagov/govuk-frontend#bdd839a7

@NickColley NickColley force-pushed the ensure-font-is-not-loaded-twice-with-compatibility-mode branch from a95ddc2 to 8c21245 Compare March 12, 2019 11:48
@govuk-design-system-ci govuk-design-system-ci temporarily deployed to govuk-frontend-review-pr-1242 March 12, 2019 11:48 Inactive
@NickColley NickColley force-pushed the ensure-font-is-not-loaded-twice-with-compatibility-mode branch from 8c21245 to bd68e7b Compare March 12, 2019 11:51
@govuk-design-system-ci govuk-design-system-ci temporarily deployed to govuk-frontend-review-pr-1242 March 12, 2019 11:52 Inactive
@alex-ju
Copy link
Contributor

alex-ju commented Mar 13, 2019

@NickColley tested this and works as expected 👏

Copy link
Contributor

@36degrees 36degrees left a 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.
@NickColley NickColley force-pushed the ensure-font-is-not-loaded-twice-with-compatibility-mode branch from bd68e7b to 632f98c Compare March 18, 2019 15:09
@NickColley NickColley merged commit e7f1315 into master Mar 18, 2019
@NickColley NickColley deleted the ensure-font-is-not-loaded-twice-with-compatibility-mode branch March 18, 2019 15:27
@NickColley NickColley mentioned this pull request Mar 18, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
Development

Successfully merging this pull request may close these issues.

4 participants