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

Prerender Error - document is not defined #22

Open
yalesnikov opened this issue Jul 26, 2021 · 6 comments
Open

Prerender Error - document is not defined #22

yalesnikov opened this issue Jul 26, 2021 · 6 comments
Labels
bug Something isn't working

Comments

@yalesnikov
Copy link

Hello, angular-dark-mode crashed building with prerender mode.

It's document is not defined error.

It'll be good to add condition to check is server or browser platform and run scripts only in the browser.

@yalesnikov yalesnikov added the bug Something isn't working label Jul 26, 2021
@talohana
Copy link
Owner

Thank you for pointing it out!
I'll look into it later today.

If you could share a reproduction in online editor or your own repo it would be much appreciated!

@talohana
Copy link
Owner

@yalesnikov

I have been looking into it, without prior knowledge of Angular Universal, does shimming with domino (as explained here) can help solving this?

@yalesnikov
Copy link
Author

I wrote my custom dark-mode.service and that is example of my source code:

/*
  * Injecting Document is safe when used with server-side rendering
  * Injecting Platform id is to avoid error when SSR compile (there is not Local Storage on SSR)
  * */
  constructor(
    @Inject(DOCUMENT) private document: Document,
    @Inject(PLATFORM_ID) private platformId: object
  ) {
    if (isPlatformBrowser(this.platformId))
      this.getStateFromLocalStorage() ? this.document.body.classList.add('dark-mode') : null
  }

It works great for prerender mode on the Angular Universal.

You can use @Inject(DOCUMENT) for works with document, but for Local Storage you can don't use it on the server side.

talohana added a commit that referenced this issue Jul 29, 2021
Guarded and abstracted DOM access

BREAKING CHANGE: Must include DarkModeModule for proper injection of default options

fix #22
talohana added a commit that referenced this issue Aug 7, 2021
Guarded and abstracted DOM access

BREAKING CHANGE: Must include DarkModeModule for proper injection of default options

fix #22
@Takudzwamz
Copy link

Hi, I've tried the plugin it's now working with Angular Universal, can you please advise on what to do for it to work with Angular Universal.

@talohana
Copy link
Owner

talohana commented Oct 8, 2022

Hi, @Takudzwamz could you please provide a reproduction? possibly with @yalesnikov advice.

@Takudzwamz
Copy link

Hi @talohana I followed what @yalesnikov suggested and I got it to work, thank you so much guys.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

3 participants