ngneat / edit-in-place

A flexible and unopinionated edit in place library for Angular applications

Home Page:https://ngneat.github.io/edit-in-place

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

ssr - document is not defined

CharlieGreenman opened this issue · comments

I'm submitting a

Feature/improvment (a behavior that used to work and stopped working in a new release)

Current behavior

Not sure if worth addressing but when using edit-on-place with Angular 17 I am getting the following error:

ERROR ReferenceError: document is not defined
at editMode (./node_modules/@ngneat/edit-in-place/fesm2020/ngneat-edit-in-place.mjs:83:55)
at editMode (./node_modules/@ngneat/edit-in-place/fesm2020/ngneat-edit-in-place.mjs:90:43)
at value (./node_modules/rxjs/dist/esm/internal/operators/switchMap.js:14:23)
at _next (./node_modules/rxjs/dist/esm/internal/operators/OperatorSubscriber.js:13:21)
at next (./node_modules/rxjs/dist/esm/internal/Subscriber.js:31:18)
at _subscribe (./node_modules/rxjs/dist/esm/internal/BehaviorSubject.js:12:44)
at _trySubscribe (./node_modules/rxjs/dist/esm/internal/Observable.js:37:25)
at _trySubscribe (./node_modules/rxjs/dist/esm/internal/Subject.js:74:22)
at cb (./node_modules/rxjs/dist/esm/internal/Observable.js:31:30)
at errorContext (./node_modules/rxjs/dist/esm/internal/util/errorContext.js:19:9)

Expected behavior

Should work out of the box with SSR

Minimal reproduction of the problem with instructions

NA will try and get around to given time

What is the motivation / use case for changing the behavior?

NA. For some reason started coming across this issue with Angular. I suspect because domino which mocks window and document does not work with Angular 17.

Environment

1. Mac OS 
2. Angular 17

I have limited experience with Angular and SSR.

Based on the documentation, the fix would be to move the ngOnInit block of the EditableComponent to afterRendernot to rely on the document API on server side: https://angular.dev/guide/ssr#authoring-server-compatible-components.

ng neat is registered in the component html itself, via a module. To fix this as ngneat used in the HTML itself, would be to make this fix in the library itself

Indeed, a fix is in progress to support SSR by using afterNextRender.
It means the minimum Angular version required for this new version will be Angular v16.

It'll requires more time than expected: update to Angular 17 is done (from previous Angular 13 version).
Using afterNextRenderrequires to move away from using Observable towards Signals API (events are not emitted anymore).

By all means, you have my thanks

Hello @CharlieGreenman, can you give a try to 1.9.0 version?

It is causing my ngModules to break

Module was removed and changed to standalone component: https://github.com/ngneat/edit-in-place/pull/94/files#diff-3d83580a41cb8bd79a4248b846ad31e50f2c369ad86cabc5a996cb9e02a1dffa

I will look into importing directives individually but will require refactor of codebase

I need to update the documentation: the new APi is using the Standalone API.
There is no EditableModule anymore.

sure, i am trying that now

Successfully migrated over to the next syntax. Will keep you posted if i still experience document issue

No longer getting the error. I use EditableCOmponent and directives. Can mark this as complete until further notice. Thank you and good work

I am now getting

ERROR TypeError: Cannot read properties of undefined (reading 'tpl')
at EditableComponent_Template (./node_modules/@ngneat/edit-in-place/fesm2022/ngneat-edit-in-place.mjs:22:103)
at executeTemplate (./node_modules/@angular/core/fesm2022/core.mjs:12159:9)
at refreshView (./node_modules/@angular/core/fesm2022/core.mjs:13392:13)
at detectChangesInView (./node_modules/@angular/core/fesm2022/core.mjs:13617:9)
at detectChangesInViewIfAttached (./node_modules/@angular/core/fesm2022/core.mjs:13580:5)
at detectChangesInComponent

Can you share related html code?
Given the error I'd say either the editMode or viewMode directive is not applied with an editable component.

<editable>
    <ng-template viewMode><span class="displayText" #existingTextSpan>{{defaultValue}}</span></ng-template>
    <ng-template editMode>
        <input matInput [style.width]="defaultValue.length * 14 + 'px'" editableFocusable editableOnEnter [placeholder]="defaultValue" (keyup.enter)="saveEdit($event)"/>
    </ng-template>
</editable>

Using your template, i can only reproduce it if the directives are not imported: https://stackblitz.com/edit/angular-ugpjwc
Can you check your imports?

that is it, thank you

resolved.