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

Add amp script if amp html attr present #718

Merged
merged 30 commits into from
Oct 2, 2018

Conversation

pjlee11
Copy link

@pjlee11 pjlee11 commented Sep 25, 2018

Resolves #678

Checks if helmet.htmlAttributes in Document/index.jsx contains amp if so injects the amp script (<script async src="https://cdn.ampproject.org/v0.js" />) and does not render other custom js, including the client side application

  • Tests added for new features
  • Test engineer approval

@pjlee11 pjlee11 self-assigned this Sep 25, 2018
);
}

if (!isAmpPayload()) {
Copy link
Author

Choose a reason for hiding this comment

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

Keeping this as a seperate if statement, not an else, as we are likely to have additional requirements to conditionally inject the client JS or application.

let ampScript;
let serialisedData;

if (isAmpPayload(helmet.htmlAttributes)) {
Copy link
Author

Choose a reason for hiding this comment

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

I've added the logic to handle the injection of the amp <script> and the removal of other <script>'s here in Document as it is currently an isolated use case. In the future it is likely that we will need to inject different scripts for different use cases, at this point it may be more beneficial to abstract the logic into a Scripts component similarly to how we handle MetaData within an isolated component.

Copy link
Contributor

Choose a reason for hiding this comment

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

We should just handle injection of the AMP script in the MetaData component. This component already has access to a boolean of amp so the logic is simple to add.

Would just need to extract scripts out of react-helmet and inject it into the document.

@bbc bbc deleted a comment from pjlee11 Sep 26, 2018
let ampScript;
let serialisedData;

if (isAmpPayload(helmet.htmlAttributes)) {
Copy link
Contributor

Choose a reason for hiding this comment

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

We should just handle injection of the AMP script in the MetaData component. This component already has access to a boolean of amp so the logic is simple to add.

Would just need to extract scripts out of react-helmet and inject it into the document.

</head>
<body>
{/* eslint-disable react/no-danger */
/* disabling the rule that bans the use of dangerouslySetInnerHTML until a more appropriate implementation can be implemented */}
Copy link
Contributor

Choose a reason for hiding this comment

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

Can this be added back to the root element?

);
}

if (!isAmpPayload(helmet.htmlAttributes)) {
Copy link
Contributor

Choose a reason for hiding this comment

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

Rather than do this logic here, data returns an amp boolean, which would be cleaner to use.

We could extract all of the script creation to that level, including the creation of of the data script, async AMP script, and deferred bundle scripts. It can all go in a single script array, which can be pased to the Document for it to use.

@pjlee11
Copy link
Author

pjlee11 commented Sep 26, 2018

Following @jtart's change requests we spoke offline and have decided to move forward by changing this PR to just add the amp <script>, then we are opening an issue to address the changes.

Update: Issue #728

jtart
jtart previously approved these changes Sep 26, 2018
@codeclimate
Copy link

codeclimate bot commented Sep 26, 2018

Code Climate has analyzed commit 09cb4d4 and detected 0 issues on this pull request.

The test coverage on the diff in this pull request is 100.0% (90% is the threshold).

This pull request will bring the total coverage in the repository to 96.2% (0.0% change).

View more on Code Climate.

Copy link
Contributor

@sareh sareh left a comment

Choose a reason for hiding this comment

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

Looks good! Just a comment about a slight change to the Cypress test.

@@ -17,6 +17,12 @@ describe('AMP Tests', () => {
getElement('html').should('have.attr', 'amp');
});

it('should load the AMP framework', () => {
// .eq(1) gets the amp <script> as the first loaded is a Cypress <script>
const ampScript = getElement('html script').eq(1);
Copy link
Contributor

Choose a reason for hiding this comment

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

It would be good to update this cypress test to be more specific - to check this script is in the head of the page. e.g. with this selector: 'head script'

@codeclimate
Copy link

codeclimate bot commented Oct 2, 2018

Code Climate has analyzed commit edcb50e and detected 0 issues on this pull request.

The test coverage on the diff in this pull request is 100.0% (90% is the threshold).

This pull request will bring the total coverage in the repository to 96.4% (0.0% change).

View more on Code Climate.

@jamesbrumpton jamesbrumpton merged commit 21e675e into latest Oct 2, 2018
@jamesbrumpton jamesbrumpton deleted the add-amp-script-if-amp-html-attr-present branch October 2, 2018 10:39
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.

5 participants