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

Fix: async property of scripts is not assigned properly #674

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion src/HelmetConstants.js
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,8 @@ export const TAG_PROPERTIES = {
PROPERTY: "property",
REL: "rel",
SRC: "src",
TARGET: "target"
TARGET: "target",
ASYNC: "async"
};

export const REACT_TAG_MAP = {
Expand Down
8 changes: 8 additions & 0 deletions src/HelmetUtils.js
Original file line number Diff line number Diff line change
Expand Up @@ -454,6 +454,14 @@ const updateTags = (type, tags) => {
: tag[attribute];
newElement.setAttribute(attribute, value);
}

// The async attribute is ignored on dynamic scripts, the property must be set directly
if (
attribute === TAG_PROPERTIES.ASYNC &&
typeof tag.async === "boolean"
) {
newElement.async = tag.async;
}
}
}

Expand Down
29 changes: 29 additions & 0 deletions test/HelmetDeclarativeTest.js
Original file line number Diff line number Diff line change
Expand Up @@ -3732,5 +3732,34 @@ describe("Helmet - Declarative API", () => {
done();
});
});

it("correctly assigns the async property to a script tag", done => {
const spy = sinon.spy();
ReactDOM.render(
<Helmet
script={[
{
src: "http://localhost/test.js",
type: "text/javascript",
async: false
}
]}
onChangeClientState={spy}
/>,
container
);

requestAnimationFrame(() => {
expect(spy.called).to.equal(true);

const [, addedTags] = spy.getCall(0).args;

expect(addedTags).to.have.property("scriptTags");
expect(addedTags.scriptTags).to.have.deep.property("[0]");
expect(addedTags.scriptTags[0].async).to.equal(false);

done();
});
});
});
});
29 changes: 29 additions & 0 deletions test/HelmetTest.js
Original file line number Diff line number Diff line change
Expand Up @@ -3399,5 +3399,34 @@ describe("Helmet", () => {
done();
});
});

it("correctly assigns the async property to a script tag", done => {
const spy = sinon.spy();
ReactDOM.render(
<Helmet
script={[
{
src: "http://localhost/test.js",
type: "text/javascript",
async: false
}
]}
onChangeClientState={spy}
/>,
container
);

requestAnimationFrame(() => {
expect(spy.called).to.equal(true);

const [, addedTags] = spy.getCall(0).args;

expect(addedTags).to.have.property("scriptTags");
expect(addedTags.scriptTags).to.have.deep.property("[0]");
expect(addedTags.scriptTags[0].async).to.equal(false);

done();
});
});
});
});