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

Angular 17 SSR - Vite bundler throws error because document is not available on Server. #3331

Closed
CaspianVahagn opened this issue Apr 19, 2024 · 2 comments · Fixed by #3345
Closed
Assignees

Comments

@CaspianVahagn
Copy link

CaspianVahagn commented Apr 19, 2024

Angular 17 can utilized Tolgee only on client site.
Alone the import of tolgee within SSR context, results in an error:

11:54:41 [vite] Internal server error: document is not defined

Minimal example here:
https://stackblitz.com/edit/fuck-yeah-angular-ssr-1e3ut7?file=src%2Fapp%2Fapp.config.ts,angular.json

Since Angular bundler is not extendable via Viteconfig (for now) workarounds with the SDK do not work.

But I could offer an workaround without the SDK, by using @ngx-translate/core and writing a own translation loader that fetches the data from the service / static files.

The custom Translation Loader with a http request to the tolgee api.

import { TranslateLoader } from '@ngx-translate/core';
import { Location } from '@angular/common';
import {  Observable} from 'rxjs';
import { environment } from '../../environments/environment';
import { HttpClient } from '@angular/common/http';

export class TolgeeLoader extends TranslateLoader {
  constructor(
    private location: Location,
    private http: HttpClient,
  ) {
    super();
  }
  getTranslation(lang = "de-DE" ): Observable<any> {
    if (environment.env === 'dev') {
      return this.http.get(
        environment.config.apiHosts.tolgee.url + '/v2/projects/export',
        {
          params: {
            languages: lang,
            format: 'JSON',
            filterNamespace: 'DEFAULT_NS',
            zip: false,
          },
          // ?languages=de-DE&format=JSON&filterNamespace=DEFAULT_NS&zip=false
          headers: {
            accept: '*/*',
            'X-API-Key': environment.config.apiHosts.tolgee.key,
          },
        },
      );
    }

    return this.http.get('/assets/de-DE.json', {
      headers: {
        'x-assets': 'true',
      },
    });
  }
}

in the app.config.ts

import {
  TranslateLoader,
  TranslateModule,
  TranslateService,
} from '@ngx-translate/core';

import { TolgeeLoader } from '../global/translation/TolgeeLoader';

export const appConfig: ApplicationConfig = {
  providers: [
    ...
    TranslateService,
    importProvidersFrom(
      TranslateModule.forRoot({
        loader: {
          provide: TranslateLoader,
          useFactory: (location: Location, http: HttpClient) => {
            return new TolgeeLoader(location, http);
          },
          deps: [Location, HttpClient],
        },
        useDefaultLang: true,
        defaultLanguage: 'de',
      }),
    ),
   ...
  ],
};
@JanCizmar
Copy link
Contributor

I will look into this this week hopefully.

@JanCizmar JanCizmar assigned stepan662 and unassigned JanCizmar May 7, 2024
@JanCizmar
Copy link
Contributor

I have assigned this to @stepan662, as it seems like an issue with @tolgee/web and dev tools.

@stepan662 stepan662 linked a pull request Jun 12, 2024 that will close this issue
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 a pull request may close this issue.

3 participants