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

Integrate ssr #157

Merged
merged 17 commits into from
Apr 6, 2024
Merged

Conversation

birkskyum
Copy link
Member

This work is to enable SSR, allowing loading pages outside of the "/" url

@birkskyum
Copy link
Member Author

birkskyum commented Mar 28, 2024

The landing page, docs and Most examples are working here now. The remaining issues with some examples are related to the ngOnRender() in the map.component still running server-side, as I mention in here, which it of course shouldn't, but I'm still not clear on how to make it client-only e.g. with afterNextRender/afterRender.

Effectively, I just take the content of ngOnInit and put it inside an afterNextRender in the constructor. I have had to disable the content of the ngOnDestroy on multiple occations, because it's being ran servers-side not having access to a global window object etc. - this is somewhat ad-odds when using the library in a SPA where it's important to do the cleanup, but I don't know how to conditionally disable it yet or mark the ngOnDestoy, or entire component for that matter, as client-only.

package.json Show resolved Hide resolved
if (this.mapService.mapInstance.hasControl(this.control)) {
this.mapService.removeControl(this.control);
}
// if (this.mapService.mapInstance.hasControl(this.control)) {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I guess you could use "?" here so that if this is not initialized, then it won't do anything, can't you?
I'm not very worried about SSR memory leaks for this lib, right?

Copy link
Member Author

@birkskyum birkskyum Mar 28, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm more concerned about leaks on the client actually, where the objects are created, because the SSR apps today often do soft navigation where possible, so they won't necessarily clean up the client until a proper page refresh is performed. I can see this when debugging the examples - I had to go into each one and only when i hit refresh they'd trigger some error. Initializing on the client, and cleaning up on the server, or vice-versa, looks to me like asking for trouble.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hmm, yea I get your point here.
I'll see if I can look into it in the next few days...
Documentation is so lacking that it makes this whole process very painful...

Copy link
Collaborator

@marcjulian marcjulian Apr 3, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What about injecting the platform to check if its the browser and then do the clean up?

private readonly _isBrowser: boolean = inject(Platform).isBrowser;

ngOnDestroy() {
  if(this._isBrowser) {
    ...
  }
}

It's used by the angular team in the components repo too e.g.

https://github.com/angular/components/blob/5a327b6741701a00cd3a409236e3f98a470c3820/src/material/datepicker/calendar-body.ts#L274-L277

Co-authored-by: Harel M <harel.mazor@gmail.com>
angular.json Outdated Show resolved Hide resolved
@HarelM
Copy link
Collaborator

HarelM commented Apr 3, 2024

@marcjulian this PR is against a branch to solve github SPA problem and allow SEO for the demo pages.
If you would like to work on it and solve this issue I can merge this to the branch and you can continue the work there.
I was planning to do it but haven't got to it yet...

@marcjulian
Copy link
Collaborator

@HarelM I can work on this, when I find more time. Yes I saw the Githup SPA issue, will be great when the library supports SSR anyways.

@HarelM
Copy link
Collaborator

HarelM commented Apr 4, 2024

I have no clue how to continue.
It seems that the ssr build is passing and I get all the examples with no console error message, but when I set prerender to true I get all the fun error message as before.
I've added a comment here:
angular/angular#54965
But I don't have high hopes to get a solution for this...

@HarelM
Copy link
Collaborator

HarelM commented Apr 4, 2024

@marcjulian feel free to tackle this. These error messages are too cryptic for me to understand and it seems like the prerender is in experimental state or something... IDK...

@marcjulian
Copy link
Collaborator

@HarelM I got prerendering working, however, only with routes.txt file at the moment.

I still had some issues with the MatIconRegistry behaving differently on the server. This was also due to the base-href used during build. Icons are now loaded with the custom loader option and that work on the server and browser.

I also removed the base-href from build, because the map wouldn't load otherwise when using npm run serve:ssr:showcase. The browser printed the following errors:

CleanShot 2024-04-05 at 12 18 17

When I set prerender in angular.json to true and use npm run build:showcase it never ends. Maybe one of the demo pages is still making trouble during SSR prerendering. I added 4 routes to routes.txt for testing and when you build the showcase you can start just the browser page (e.g. npx serve dist/showcase/browser - open localhost:3000). There you can navigate around and a browser refresh will work on the prerendered pages.

I will try to find some time to add all routes to the routes.txt to see which route interferes with prerendering. Let me know what you think.

}

ngOnDestroy() {
this.portalOutlet.detach();
if (this._isBrowser) {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can't we simply check if portalOutlet os not null?

@HarelM
Copy link
Collaborator

HarelM commented Apr 5, 2024

Besides the above nit picking this is a good step forward.
I'll merge this to the branch so we can continue the work there.
I suggest to try and add it to the build and add routes one by one until everything is working.
If prerender will work for most of the routes it is good enough for the demo page.
Thanks for all the work on this!

@HarelM HarelM marked this pull request as ready for review April 6, 2024 10:26
@HarelM HarelM merged commit ee2943d into maplibre:update-to-application Apr 6, 2024
HarelM added a commit that referenced this pull request Apr 9, 2024
* update to browser-esbuild

* upgrade to application

* bump deps

* ng update changed tsconfig

* Fix package version

* update deploy path

* fix lockfile

* Integrate ssr (#157)

* Enable SSR with migration script

* Add zone.js to polyfills

* Make Map component SSR compatible

* Make "Scroll demo into view" client only

* afterNextRender in layout-toolbar-menu

* afterNextRender in control.component.ts - fixes terrain example

* Lint - Update projects/showcase/src/app/app.config.ts

Co-authored-by: Harel M <harel.mazor@gmail.com>

* use withFetch to fix Error retrieving icon

* add custom loader for svg, fix loading icons on server

* fix loading map for ssr app by removing base-href

* detach portal in browser only

* remove polyfills

* replace window functions

* create popup in browser only with afterNextRender

* cleanup unused lifecycle hooks, remove control only in browser

* prerender with routes file

* Update projects/showcase/src/app/demo/examples/live-update-feature.component.ts

---------

Co-authored-by: Harel M <harel.mazor@gmail.com>
Co-authored-by: Marc Stammerjohann <8985933+marcjulian@users.noreply.github.com>

* Small changes to docs

* Fix test due to change in life cycle

* Added  more routes

* ignore problematic routes

* Add ignore to another test

* Allow prerender for all routes (46)

* Fix live update of image source

* Simplify code a bit

* fix multiple selectors for clear search button

---------

Co-authored-by: Harel M <harel.mazor@gmail.com>
Co-authored-by: Marc Stammerjohann <8985933+marcjulian@users.noreply.github.com>
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

Successfully merging this pull request may close these issues.

3 participants