#Correct use of ssr hybrid aproach

1 messages · Page 1 of 1 (latest)

sullen oracle
#

it's just a button, why can't you render it on the server?

#

the only things i don't render on the server are ones that literally can't be (like chart.js stuff that requires a canvas) and instead i render a skeleton to avoid layout shifts

#

but in this case, i don't see any reason why you would do that

drowsy gorge
#

<div class="mx-auto max-w-7xl">
  <div class="flex items-center justify-end p-8">
    @if (platformService.isBrowser) {
      @if (!editMode()) {
        <button
          mat-flat-button
          color="primary"
          i18n
          (click)="editMode.set(true)">
          Enable Edit Mode
        </button>
      } @else {
        <button mat-flat-button color="primary" i18n (click)="openDialog()">
          Add Card
        </button>
      }
    }
  </div>

  <!-- Keep existing layout but add drag-drop -->
  <div
    class="grid grid-cols-1 gap-4 lg:grid-cols-3"
    cdkDropList
    [cdkDropListDisabled]="!editMode() || platformService.isServer"
    cdkDropListOrientation="horizontal"
    (cdkDropListDropped)="drop($event)">
    @for (item of layoutService.layout(); track item.id) {
      <div
        cdkDrag
        [cdkDragDisabled]="!editMode() || platformService.isServer"
        class="h-[350px]">
        @if (platformService.isBrowser) {
          <div class="cdkDragPlaceholder" *cdkDragPlaceholder></div>
        }
        <app-cards
          [type]="item.type"
          (remove)="layoutService.removeItem(item.id)" />
      </div>
    }
  </div>
</div>


#

im still learning ssr on angular, sSo I thought that because it was an interactive element it would have to be run on the client?

#

what you think about my current logic?

#

maybe its bad

sullen oracle
#

if it can be rendered on the server, do it

drowsy gorge
#

but its what im trying to learn

drowsy gorge
#

so what you would change on my logic?

#

what about cdkDropList

sullen oracle
#

remove the @if that checks if you're in the browser

#

and remove the || platformService.isServer too

drowsy gorge
#

right

#

and what about incremental hydration?

sullen oracle
#

maybe that's what was being said or read

#

and you misunderstood it or something

sullen oracle
drowsy gorge
# sullen oracle what about it?

openDialog() {
    // ! if is rendering on server, ignore
    if (this.platformService.isServer) return;

    const dialogRef = this.dialog.open(CardSelectDialogComponent, {
      height: "400px",
      width: "600px",
    });

but on typescript i need to make this logic right?

sullen oracle
#

no

#

as i said

#

the click handler doesn't matter

#

nobody is clicking it on the server

drowsy gorge
#

right, do you recomend something where i can learn more ssr on angular? the docs are ok, but i need something more

sullen oracle
#

no idea tbh sorry. i never did any tutorial

#

just suffered through it

#

it's not really complicated imo.
You just don't do anything special until it breaks

#

then you can add your isServer/isBrowser checks when required

next vault
#

More specifically, you barely ever need those checks. And even if "something breaks" there are often better options than a direct "isServer" check.

sullen oracle
#

and it usually only breaks when you or a 3rd party accesses browser only apis

#

like window/document

drowsy gorge
#

right, do you recomend using incremental hydration?

sullen oracle
#

that depends on your specific app

#

if there are scenarios where using it would make sense

#

personally i haven't come across something yet i had to defer

drowsy gorge
#

It's an app that will be used more by users within companies, so as it's starting to be made from scratch, I'd like to learn and make it as performant as possible, so I don't go back later

next vault
#

Incremental hydration is something you can add later if you want to squeeze out every last drop of performance

sullen oracle
#

^ was about to say that

#

just build the app and add it later if you need to

next vault
#

I wouldn't worry about it first when just starting out with SSR/SSG

drowsy gorge
#

and when should i use ngSkipHydration

sullen oracle
#

ideally, never

drowsy gorge
#

yeah, but in what cases will require that?

sullen oracle
#

i know one case where it's required lemme try to find the github issue

#

which is very visible when using the page

#

so i guess rule of thumb, if you don't see any visual errors or errors in the console, no need to use it

drowsy gorge
#

ok, so i have this service that allow me save layout changes on localstorage, in that case we need to verifie if is running on browser right? but since that the helper methods like additem, does not manipulate localstorage direcly , but the signal, i need that if statment there?

sullen oracle
#

if addItem is only called from user interactions, it doesn't need a check

#

just like the click handler

#

for the rest, yeah. you technically need some kind of check.
however, if it's layout changes, i would assume those affect how some pages look?

#

which would mean it will render differently on the server

#

cause you don't have that information there

drowsy gorge
#

the additems is called from the openDialog i shared

sullen oracle
#

then no check is needed there

next vault
#

May I ask why you are even doing SSR? SSR for things that require login is an immensely more complicated task than otherwise.

sullen oracle
drowsy gorge
#

the layout is basically an state for my dashboard items position, for allow user to save there preferences, and later i will also save this data on db or kv, to allow the user to always have the same layout on every device he login, maybe its overkill, but if you guys have some suggestions?

next vault
#

Sure that's fine. The question is still why this app needs SSR.

drowsy gorge
# next vault Sure that's fine. The question is still why this app needs SSR.

I'm new to angular, because in this project it happened to be in angular, but I came from the react, next js, tanstack start ecosystem, and as the concept of SSR is very common there and in theory nowadays it is very good, so I decided to start this with ssr, it could be overkill, or I'm adding more complexity. but on the one hand, this way I learn a little bit about everything in angular

next vault
#

SSR in general is a great tool. However when it comes to login suddenly the complexcity explodes. You now have to worry about:

  • Where to store the user login token / session
  • How to access any APIs using that token when you're rendering server side

This might sound trivial, but it is not at all, especially if you want to keep writing your normal HttpClient code in Angular, but it must now work server- and client-side.
For an app like this you have to ask yourself it that is worth it for what benefit. The most useful aspect of SSR is making your site more SEO-friendly. I would argue that is not really relevant for an app that you need to log in to.

drowsy gorge
#

yh, maybe I'll have some difficulties doing the authentication part, but maybe it'll be worth it, and it's always a learning experience. But by chance I ended up here in an article, which seems to be a good starting point, using interceptors and middleware in the express server, and it seems to work the way I would need.