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

window[LOADABLE_STATE] is not working #67

Closed
KrustyC opened this issue Apr 30, 2018 · 4 comments
Closed

window[LOADABLE_STATE] is not working #67

KrustyC opened this issue Apr 30, 2018 · 4 comments

Comments

@KrustyC
Copy link

KrustyC commented Apr 30, 2018

I'm trying to use this lubrary with SSR, but I keep receiving this error loadable-components state not found. You have a problem server-side. Please verify that you have calledloadableState.getScriptTag() server-side. I dive into the module and I put a console.log(window) in the loadComponents() function, the result contains this:
image

but if I do console.log(window[LOADABLE_STATE]); is returning undefined.

Do you have any clue about what is happening?

@KrustyC
Copy link
Author

KrustyC commented May 1, 2018

@neoziro I think I found a possible solution:
basically I change the loadComponents() to this:

function loadComponents() {

  if (typeof window === 'undefined') {
throw new Error('loadable-components: loadComponents must be called client-side: window is undefined');
 }   
  return new Promise(function (resolve, reject) {
    window.onload = function() {
      var state = window[LOADABLE_STATE];
      if (!state) {
        throw new Error('loadable-components state not found. ' + 'You have a problem server-side. ' + 'Please verify that you have called `loadableState.getScriptTag()` server-side.');
      }
      resolve(loadState(state));
    }
  });
}

The function has to wait for the window to be fully loaded and then I have to wrap it in a Promise, in order to return only when the window is loaded.

Do you think this is a bug or do you think I might missing something somewhere?

@gregberge
Copy link
Owner

Ah good workaround. In fact loadableState.getScriptTag() must be called before your loadComponents() and it should work without having to rewrite the loadComponents method.

@KrustyC
Copy link
Author

KrustyC commented May 2, 2018

Thanks for the reply, I moved the loadableState.getScriptTag() before importing my main js file and it is working now. By the way, I'm not 100% sure the window is gonna be loaded every time, do you think might be useful to wrap it on a promise and wait for onload event before resolving the promise?

@gregberge
Copy link
Owner

No I don't think so. You should add scripts at the end of the body, this is the way to setup all React stuff so I think it is OK like that.

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

No branches or pull requests

2 participants