Skip to content

Angular SSR platform providers issue #52906

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

Closed
newvladimirov opened this issue Nov 14, 2023 · 2 comments
Closed

Angular SSR platform providers issue #52906

newvladimirov opened this issue Nov 14, 2023 · 2 comments
Labels
area: server Issues related to server-side rendering
Milestone

Comments

@newvladimirov
Copy link

Which @angular/* package(s) are the source of the bug?

platform-server

Is this a regression?

Yes

Description

I'm trying to provide some tokens in server.ts, and use them in the application, for example:

server.get('*', (req, res, next) => {
  const { protocol, originalUrl, baseUrl, headers } = req;

  commonEngine
    .render({
      bootstrap,
      documentFilePath: indexHtml,
      url: `${protocol}://${headers.host}${originalUrl}`,
      publicPath: browserDistFolder,
      providers: [
        { provide: APP_BASE_HREF, useValue: baseUrl },
        { provide: REQUEST, useValue: req },
        { provide: TEST_TOKEN, useValue: 'huest'}
      ],
    })
    .then((html) => res.send(html))
    .catch((err) => next(err));
});

@Component({
  selector: "app-root",
  standalone: true,
  imports: [CommonModule, RouterOutlet],
  templateUrl: "./app.component.html",
  styleUrls: ["./app.component.scss"],
})
export class AppComponent implements OnInit {
  private platformId = inject(PLATFORM_ID);
  private injector = inject(Injector);

  ngOnInit(): void {
    if(isPlatformServer(this.platformId)) {
      const val = this.injector.get(TEST_TOKEN); <---------------------------------
      const req = this.injector.get(REQUEST); <---------------------------------
      console.log({val})
    }
  }
}

The problem is that it only works in normal build mode, if i use the serve mode, i get NullInjectorError: No provider error on the server side.

To double check this issue i created a new app from scratch using ng new but it didn't help.

Please provide a link to a minimal reproduction of the bug

https://github.com/newvladimirov/ng17-ssr-di-issue-2

Please provide the exception or error you saw

No response

Please provide the environment you discovered this bug in (run ng version)

Angular CLI: 17.0.0
Node: 20.9.0
Package Manager: npm 10.1.0
OS: darwin arm64

Angular: 17.0.2
... animations, common, compiler, compiler-cli, core, forms
... platform-browser, platform-browser-dynamic, platform-server
... router

Package                         Version
---------------------------------------------------------
@angular-devkit/architect       0.1700.0
@angular-devkit/build-angular   17.0.0
@angular-devkit/core            17.0.0
@angular-devkit/schematics      17.0.0
@angular/cli                    17.0.0
@angular/ssr                    17.0.0
@schematics/angular             17.0.0
rxjs                            7.8.1
typescript                      5.2.2
zone.js                         0.14.2

Anything else?

No response

@AndrewKushnir AndrewKushnir added the area: server Issues related to server-side rendering label Nov 14, 2023
@ngbot ngbot bot added this to the needsTriage milestone Nov 14, 2023
@eneajaho
Copy link
Contributor

This is the same as:
angular/angular-cli#26323
angular/angular-cli#26354

So I think this issue can be closed in favor of those other two.

@JeanMeche JeanMeche closed this as not planned Won't fix, can't repro, duplicate, stale Nov 14, 2023
@angular-automatic-lock-bot
Copy link

This issue has been automatically locked due to inactivity.
Please file a new issue if you are encountering a similar or related problem.

Read more about our automatic conversation locking policy.

This action has been performed automatically by a bot.

@angular-automatic-lock-bot angular-automatic-lock-bot bot locked and limited conversation to collaborators Dec 15, 2023
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
area: server Issues related to server-side rendering
Projects
None yet
Development

No branches or pull requests

4 participants
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy