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

Fixed potential error where head does not exist in doc #273

Merged
merged 4 commits into from
Nov 17, 2018

Conversation

onionhammer
Copy link
Contributor

@onionhammer onionhammer commented Oct 14, 2018

This issue occurred when I was doing some transformations to certain pages to generate partial static HTML snippits for inclusion via ajax; react-snap would fail because the element doesnt exist

index.js Outdated
@@ -334,7 +334,7 @@ const inlineCss = async opt => {
style = document.createElement("style");
style.type = "text/css";
style.appendChild(document.createTextNode(allCss));
head.appendChild(style);
head && head.appendChild(style);
Copy link
Owner

Choose a reason for hiding this comment

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

this will hide potential error. I would prefer visible error instead of hiding

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Maybe a console.warning instead? or we could just append it to the end of the document rather than the head if the head isnt there?

Copy link
Owner

Choose a reason for hiding this comment

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

if the head isn't there it will not work as expected, where shall we insert those instead? We can't just ignore it, the whole thing could fail. What we can do is to create more user friendly error, like "Hey, we use head to do this manipulation, but we failed to find one, so tool doesn't work. Please add head"

Copy link
Contributor Author

@onionhammer onionhammer Oct 15, 2018

Choose a reason for hiding this comment

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

@stereobooster the thing now is it fails regardless of whether or not there are any stylesheets to even add. Also if the <style> is in the body, it will still work the same as it would in the head

Copy link
Owner

Choose a reason for hiding this comment

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

May I ask why there is no head in the first place?

Copy link
Contributor Author

@onionhammer onionhammer Oct 16, 2018

Choose a reason for hiding this comment

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

@stereobooster Im not asking to optimize for this specific case, I'm offering to fix a bug where a null reference exception occurs. Null reference exceptions in libraries are never features

Copy link
Owner

Choose a reason for hiding this comment

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

yes, but this fix also can hide error for other case. Fix would be to show human readable error

Copy link
Contributor Author

Choose a reason for hiding this comment

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

@stereobooster Sure, but also just the fact that adding a style element when there are no linked stylesheets is not necessary, is it?

Copy link
Owner

Choose a reason for hiding this comment

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

Sure, but also just the fact that adding a style element when there are no linked stylesheets is not necessary, is it?

For sure, but you can check exactly this case if (criticalCss.length === 0) { return; }.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I will update my PR in the next few days

@onionhammer
Copy link
Contributor Author

onionhammer commented Oct 16, 2018

TODO Summary:

  • If no CSS links, return early
  • Otherwise if no head, throw nicely worded error

@onionhammer
Copy link
Contributor Author

onionhammer commented Oct 19, 2018

@stereobooster Updated, still in the process of testing this..

Edit: Confirmed that this fixes the null reference error

@stereobooster
Copy link
Owner

Code is cleaner now. Can you please add test-case for this?

@onionhammer
Copy link
Contributor Author

@stereobooster added a test

@stereobooster
Copy link
Owner

Looks ok. I need some time to actually merge it and release

const head = document.head || document.getElementsByTagName("head")[0],
style = document.createElement("style");
style.type = "text/css";
style.appendChild(document.createTextNode(allCss));

if (!head)
throw "No <head> element found in document";
Copy link
Owner

Choose a reason for hiding this comment

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

Suggested change
throw "No <head> element found in document";
throw new Error("No <head> element found in document");

@stereobooster stereobooster merged commit 3c0a20b into stereobooster:master Nov 17, 2018
@stereobooster
Copy link
Owner

There is small thing to change, but I can do it. Thanks

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.

2 participants