Angular 19 REQUEST is `null` (on server runtime) even with RenderMode.Server and SSG disabled

Discussão em 'Angular' iniciado por Alex Pappas, Dezembro 3, 2024.

  1. Alex Pappas

    Alex Pappas Guest

    In Angular 19, there's a new way to access the Request object from the Angular application scope which was not available on Angular 18.

    According to the docs, expect null if you hit any of the following condition:

    • During the build processes.
    • When the application is rendered in the browser (client-side rendering).
    • When performing static site generation (SSG).
    • During route extraction in development (at the time of the request).
    ​

    I made sure to disable SSG with the following change in the default angular.json:

    "architect": {
    "build": {
    "options": {
    "prerender": false,


    And I made sure that the renderMode is not prerender in app.routes.server.ts:

    import { RenderMode, ServerRoute } from '@angular/ssr';

    export const serverRoutes: ServerRoute[] = [
    {
    path: '**',
    renderMode: RenderMode.Server
    }
    ];


    Both approach of DI still returns null for REQUEST:

    import { Component, inject, Inject, REQUEST } from '@angular/core';

    //...
    export class AppComponent {
    // constructor(@Inject(REQUEST) public req: Request) {
    // console.log('req', this.req); //null
    // }

    constructor() {
    const req = inject(REQUEST);
    console.log('req', req); // null
    }
    }

    Continue reading...

Compartilhe esta Página