ExternalJsFileLoader service does not support server side rendering
See original GitHub issueDescribe the bug
When using the ExternalJsFileLoader
to e.g. load the google maps api via GoogleMapRendererService
, the method load
uses the code this.document.createElement('script')
to create the script tag on the document.
Accessing the document like this throughs an error, when executing the code in Node JS (SSR).
Tell us the version of Spartacus
- Library version: 3.0.0-rc.1
To Reproduce Steps to reproduce the behaviour:
- Generate a new Spartacus Storefront
- Follow Steps to activate Server Side Rendering
- Start local instance
- See error in console:
ERROR ReferenceError: document is not defined
at ExternalJsFileLoader.load (/Users/jwidmer/projects/conforama/workspace/js-storefront/conforama-storefront/dist/conforama-storefront-server/main.js:219361:9)
at GoogleMapRendererService.renderMap (/Users/jwidmer/projects/conforama/workspace/js-storefront/conforama-storefront/dist/conforama-storefront-server/main.js:108067:39)
at StoreFinderMapComponent.renderMap (/Users/jwidmer/projects/conforama/workspace/js-storefront/conforama-storefront/dist/conforama-storefront-server/main.js:124587:39)
at StoreFinderMapComponent.ngOnChanges (/Users/jwidmer/projects/conforama/workspace/js-storefront/conforama-storefront/dist/conforama-storefront-server/main.js:124575:18)
at StoreFinderMapComponent.rememberChangeHistoryAndInvokeOnChangesHook (/Users/jwidmer/projects/conforama/workspace/js-storefront/conforama-storefront/dist/conforama-storefront-server/main.js:149714:14)
Expected behavior
The method externalJsFileLoader.load
can be used in both SSR and CSR scenarios
Desktop (please complete the following information):
- OS: all
- Browser all
- Version all
Smartphone (please complete the following information):
- Device: all
- OS: all
- Browser all
- Version all
Additional context
In the constructor
of the ExternalJsFileLoader
, the document is injected by using the type
any
. Should that maybe changed to constructor(@Inject(DOCUMENT) protected document: DOCUMENT) {}
?
Issue Analytics
- State:
- Created 3 years ago
- Comments:5 (1 by maintainers)
Another Input: As most 3rd Party Scripts complain, when they get included in the page twice (Code get’s executed in Server Side rendering and again in Client Side Rendering) (e.g. Google Maps), it would be great, if there would be an option within the load option to disable it for Server side rendering:
@mirkosoft thanks, will do the update soon