question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

ExternalJsFileLoader service does not support server side rendering

See original GitHub issue

Describe 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:

  1. Generate a new Spartacus Storefront
  2. Follow Steps to activate Server Side Rendering
  3. Start local instance
  4. 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:closed
  • Created 3 years ago
  • Comments:5 (1 by maintainers)

github_iconTop GitHub Comments

1reaction
janwidmercommented, Jan 12, 2021

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:

interface ILoadConfig {
	callback?: EventListener;
	errorCallback?: EventListener;
	loadInSSR?: boolean;
}

public load(src: string, params?: object, config?: ILoadConfig): void {
	if (isPlatformServer(this.platformId) && config?.loadInSSR === false) return;
	
	// ... rest of the code
}
0reactions
janwidmercommented, Jan 29, 2021

@mirkosoft thanks, will do the update soon

Read more comments on GitHub >

github_iconTop Results From Across the Web

Next.js 13 support (server-side components) #3707 - GitHub
js and the new app/ directory, it appears that the react-spectrum modules are unable to transpile and be used in the latest Next.js...
Read more >
External js file is not working - Stack Overflow
ClientID %> ') server side code which need to be render before going on client side so it cannot be separated in to...
Read more >
Adding a server-side rendering support for an existing React ...
entry : We have not created the file but we need a new entry for SSR. We will store our server entry under...
Read more >
Server-Side Rendering - SurviveJS
Server -Side Rendering (SSR) is a technique that allows you to serve an initial payload with HTML, JavaScript, CSS, and even application state....
Read more >
A hands-on guide for a Server-Side Rendering React app
Although there are a bunch of JavaScript files, they are not executed. This can be proved by turning off JavaScript from the bowser....
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found