-
Notifications
You must be signed in to change notification settings - Fork 126
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
ReferenceError: window is not defined #62
Comments
I'm not sure if Leaflet works with server rendering. If it does, then it sounds like you need to shim the window object or maybe ensure that a browser lib is getting loaded by your bundler. |
Thank you for your reply. I do not think leaflet will work on the server side. https://stackoverflow.com/questions/43800987/angular-2-and-implementing-leaflet-maps-open-street-map Of course, if the leaflet works on the server side, it has not been better. |
I'm gonna close this for now as I don't think there's an action here. Thanks for using the plugin! |
closed? what was the cause? when I run npm run build:ssr && npm run serve:ssr I am getting the same error! window not defined??
my angular.json |
The point is that not all libraries are easily compatible with server-side rendering. In this case, the window object doesn't exist on the server. Because on the server, there's no browser. And, with no browser, there's no window. Leaflet relies on there being a window object. You'd either need to shim the window object (provide a fake global window object), or you'd need to make all the Leaflet stuff run on the client (which from my basic google research is totally a thing you can do with Angular Universal). Since Leaflet relies heavily on Javascript-driven interactions, you aren't going to be able to get all of it to render on the server. You will need to configure your app to render the map and run all the Leaflet JS code on the client. The Leaflet JS code needs to run on the client for all of the interactions to work. I closed this issue because this isn't something I can solve in this plugin. And, Angular Universal is a whole other layer that I had no plans to support when I made this plugin. This is a free and open source project. I'm completely sharing my work. But, if you need something that's outside of the scope of my work, I rely on outside contributors to solve those issues and make contributions. |
I get a different error, at the import level.
|
@philip-firstorder That's a basic parse error. It suggests something is wrong with the build pipeline cause it's trying to parse a Typescript file as a JS file or something. It's odd though, cause it's referencing the JS file. |
@reblace I think there is a way where you can use isPlatformBrowser from the common module (https://angular.io/api/common/isPlatformBrowser) and return on init of all directives. this will hopefully stop leafletjs from being included in ssr (not sure though yet). Thing is, I tried to fork, and tried to build locally, it works fine, but then when I link to my other angular project to test it in ssr, using
I can't even know where to begin to fix that :/ can you help? |
@ayyash do you have a repo I could check out that would provide me a starting point? I've never used ssr with leaflet/angular, so it'd be a bit of work for me to get spun up on it. |
Here is the minimal code you need to reproduce. Note that everyone is likely OK with having to use some JavaScript to run your library. They likely have SEO requirements for their library that require them to use Angular Universal Pre-rendering. Also be aware that React had a similar issue with the Leaflet library and pre-rendering. |
Additionally, others appear to have fixed this with their leaflet wrappers: |
Hello.
I would like to use this module with angular universal.
However, the following error occurred.
I tried using isPlatformBrowser with client only but leafletOptions parse error appears
Is there any good solution?
The text was updated successfully, but these errors were encountered: