fix: allow amplitude to be imported/required during SSR #436
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.
Summary
We have a number of NextJS applications using amplitude, but we currently need to do something like the following to be able to use the library:
If instead we were to have a static
at the top of our modules, we'd see errors due to amplitude trying to access global browser variables such as
window
andnavigator
when trying to load the module.While I understand that this library is intended for use in the browser and there's a separate package for use in Node environments, our use case is to simply be able to
import
the module rather than having to use a dynamic import. We don't intend on calling any of the amplitude functions as part of the server-side render.This PR adds the minimal amount of
typeof
checks in front of places where browser global variables were being accessed so that we could successfullyimport
amplitude and build the app. We could go even further with this and add similar checks in all the places (i.e. in case functions likegetInstance
andlogEvent
were to be called during SSR) so that things fail more gracefully if you prefer.Related: #138 #164
Checklist