Support importing Plyr in Node.js without errors #960
Merged
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
This injects a type check for
navigator
before the module header, so it's only run if the environment is a browser. If not (if it's Node.js) then no errors will be thrown, but the imported module will just be an empty object literal. For browsers it won't have any effect.I think this should fix the SSR issue (#935).
The advantage to this is that it's just a small change and the regression risk is minimal. Alternative solutions would require wrapping the main module (not possible with
import
andexport
statements to begin with since they need to be at the top level) or wrapping supports.js,defaults.captions.language
andutils.getBrowser()
separately, as well as remembering to add this type check to new methods in the future.The detection will fail if you define
navigator
globally in Node.js, as some jsdom users might do to actually support other front end libraries (not just imoprting them) in Node.js, but I don't think we should bother supporting this use case.