You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
Providing a DI token in commonEngine.render() does not provide its value to application #26323
Which @angular/* package(s) are the source of the bug?
Don't known / other
Is this a regression?
Yes
Description
Prior to upgrading to v17 our application passed express' req/res as REQUEST/RESPONSE DI Tokens through the render function of the engine in server.ts. Since upgrading to v17 from v16 these DI Token values are always null.
This can be seen in the minimal reproduction where all three of APP_BASE_HREF / REQUEST / RESPONSE are null in the console output while running 'ng serve'.
If using @angular-devkit/build-angular:browser instead of @angular-devkit/build-angular:application this problem does not occur. I assume there is a different way to provide dependencies from server.ts now but I have not been able to find any updated documentation on it.
Please provide a link to a minimal reproduction of the bug
Reacted by Saeed, Richard Sustek, Jonas, Emmanuel B., Bartosz Zygmunt, Alfonso Andrés López Molina, jmmp, Krzysztof Platis, Paweł Fraś, Nikola Zarić and 137 more
Reacted by Lukas Chladek, hiepxanh, Salathiel Genese, Daranix, 4567890987654, William Lahti, kenarius777, Ajit Singh, ryohei-izumi-0702, Troy Steuwer and 3 more
changed the title [-]Providing a DI token in commonEngine.render() does not provide this value[/-][+]Providing a DI token in commonEngine.render() does not provide its value to application[/+]on Nov 10, 2023
Hi, have a look at #26175 which updated the schematics to import create the tokens that aren't exported anymore.
You will need to provide the tokens yourself.
This is expected as for ng serve the server.ts is not used.
Whilst we can provide an optional request token, this will be not be same interface of the Express tokens as the dev-server does not use express. Also the mentioned tokens will not available when using prerendering.
The recommended approach to treat the tokens set in server.ts as optional.
Reacted by German Fica
Reacted by Troy Steuwer, Sergei Zheleshchenko, Frédérik Charest and Ricky Nguyễn
Ah ok, got it working now, I have had to run build and run server.mjs independently. Is there a way to get the dev-ssr-server to work with the application output / is there an equivalent builder available for application output? Or do we still need to use dev-ssr-server with seperate server + browser builder instead of application? The current documentation is quite confusing as it appears like running the application builder through ng serve should allow local ssr rendering
Reacted by Saeed, Emmanuel B., Alfonso Andrés López Molina, Salathiel Genese, Julius Wittekind, Charles Samborski, Brad Kovach, Ali Naderlou, amir.al.omari, Dmitriy Stepanenko and 4 more
ng serve does allow SSR, but it does not use the server.ts, Instead it has it's own middlewares and server instance. You should definitly update the docs to make this clear.
Reacted by Ali Naderlou, neistow, Sebastián Rojas Ricaurte, Aurélien Dussauge, João Monteiro, Apurba Jitu Hansda, Vlad Ioffe, Alban, Chris Rogers, Raphaël Roux and 2 more
ng serve does allow SSR, but it does not use the server.ts, Instead it has it's own middlewares and server instance. You should definitly update the docs to make this clear.
This is a serious issue IMO, we should be able to inject REQUEST and RESPONSE with ng serve
How we can debug our app then ?
By building and running server.mjs in dist folder ?!
So server.ts is used only for production ??
Or am I missing something ??
I will continue using old ssr-dev-server for now
(I remember that in v16 and below, server.ts was used for both development and production. Why shouldn't it be the same as before? Or if we can use server.ts for both envs + esbuild, how can we do that?)
Reacted by Alexey Vladimirov, Emmanuel B., Alfonso Andrés López Molina, Krzysztof Platis, Nurlan Joldibaev, Salathiel Genese, shady-ahmed-bdr, chriswoodie, Albert Vos, Mateusz Roszczyk and 36 more
I've been confused by this as well. I was hoping there is an alternative way for ssr-dev-server in the application builder so that we can both develop the app, have it rebuild on the fly and just use easily.
Since the server.ts is not used for ng serve, it also means that none of the custom API routes that are defined are registered. What is the recommended approach here?
Reacted by Saeed, Emmanuel B., Alfonso Andrés López Molina, Krzysztof Platis, Salathiel Genese, chriswoodie, Stefan Stanchev, busypika, amir.al.omari, Aurélien Dussauge and 4 more
Imagine we created a fresh new (SSR) project with latest version of @angular/cli and we want to make sure that angular ssr server returns 404 HTTP Status Code instead of 200 for unregistered routes or we want to redirect a path to another based on some conditions only, so we should set 301 HTTP Status Code and ...
We definitely need server response object to set the right HTTP Status Code (and also request for other use cases)
My question is: How we can do it in development environment ?!
Reacted by Nurlan Joldibaev, Richard Sustek, Nik, Alexey Vladimirov, Emmanuel B., Alfonso Andrés López Molina, Krzysztof Platis, Evhenii Hudzenko, Salathiel Genese, caolaiz and 29 more
Already spend a whole day to search a way to make providers / DI token from SSR server work in client without results : tried with v18.2.5 AND v19.0.0-next.7 (new project to try with the new version), same (no) results.
My setup :
Tokens :
import {InjectionToken} from '@angular/core';
import {Request, Response} from 'express';
export const REQUEST = new InjectionToken<Request>('REQUEST');
export const RESPONSE = new InjectionToken<Response>('RESPONSE');
export const SERVER_TOKEN = new InjectionToken<string>('ServerToken');
I use a workaround that is not applicable in every situation but may help someone else. This is not a generic solution but allows to read specific headers from the request. In this case it is the cookie, but it should work with other headers as well.
I have a web server that acts as a reverse proxy for the application. I configured that reverse proxy to take the "Cookie" header and pass it as a query string parameter. I the access the cookie by parsing the URL from PlatformLocation.
This is the reverse proxy configuration that I use, in this case for IIS. It should be fairly easy to reproduce this with other servers such as nginx.
<?xml version="1.0" encoding="utf-8"?>
<configuration>
<locationpath="."inheritInChildApplications="false">
<system.webServer>
<rewrite>
<rules>
<!-- Remove the 'cookie' parameter from the query string for resources and the vite server -->
<rulename="Reverse Proxy new site (files and vite)"stopProcessing="true">
<matchurl="^([^?]+(?:\.\w+|vite/client))" />
<actiontype="Rewrite"url="http://localhost:4200/{R:1}" />
</rule>
<rulename="Reverse Proxy new site (ws)"stopProcessing="true">
<matchurl="^$" />
<actiontype="Rewrite"url="http://localhost:4200/" />
</rule>
<!-- Inject the cookie header as a query string parameter -->
<rulename="Reverse Proxy new site and inject cookie"stopProcessing="true">
<matchurl="^(.*)" />
<conditions>
<addinput="{HTTP_COOKIE}"pattern="(.+)" />
</conditions>
<actiontype="Rewrite"url="http://localhost:4200/{R:1}?cookie={C:1}" />
</rule>
</rules>
</rewrite>
</system.webServer>
</location>
</configuration>
Of course that code is only used in development and on the server-side. I use an environment parameter to know which environment the code is running on, and also check isPlatformServer.
Activity
[-]Providing a DI token in commonEngine.render() does not provide this value[/-][+]Providing a DI token in commonEngine.render() does not provide its value to application[/+]JeanMeche commentedon Nov 10, 2023
Hi, have a look at #26175 which updated the schematics to import create the tokens that aren't exported anymore.
You will need to provide the tokens yourself.
Ozzcer commentedon Nov 10, 2023
Hi @JeanMeche , tokens are defined under express.tokens.ts in minimal reproduction (https://github.com/Ozzcer/angular-ssr-di-issue/blob/c08049cc39e2993eff903c29902826a1aa1f7d10/src/express.tokens.ts), this file was created automatically by 'ng update' in our original project and I copied the file over to the minimal reproduction.
Also APP_BASE_HREF is coming through as null and I thought that didn't need to be manually defined.
alan-agius4 commentedon Nov 10, 2023
This is expected as for
ng servetheserver.tsis not used.Whilst we can provide an optional request token, this will be not be same interface of the Express tokens as the dev-server does not use express. Also the mentioned tokens will not available when using prerendering.
The recommended approach to treat the tokens set in
server.tsas optional.Ozzcer commentedon Nov 10, 2023
Ah ok, got it working now, I have had to run build and run server.mjs independently. Is there a way to get the dev-ssr-server to work with the application output / is there an equivalent builder available for application output? Or do we still need to use dev-ssr-server with seperate server + browser builder instead of application? The current documentation is quite confusing as it appears like running the application builder through
ng serveshould allow local ssr renderingalan-agius4 commentedon Nov 10, 2023
ng servedoes allow SSR, but it does not use theserver.ts, Instead it has it's own middlewares and server instance. You should definitly update the docs to make this clear.SaeedDev94 commentedon Nov 11, 2023
This is a serious issue IMO, we should be able to inject
REQUESTandRESPONSEwithng serveHow we can debug our app then ?
By building and running server.mjs in dist folder ?!
So
server.tsis used only for production ??Or am I missing something ??
I will continue using old
ssr-dev-serverfor now(I remember that in v16 and below,
server.tswas used for both development and production. Why shouldn't it be the same as before? Or if we can useserver.tsfor both envs + esbuild, how can we do that?)Enngage commentedon Nov 12, 2023
I've been confused by this as well. I was hoping there is an alternative way for
ssr-dev-serverin theapplicationbuilder so that we can both develop the app, have it rebuild on the fly and just use easily.Since the
server.tsis not used forng serve, it also means that none of the custom API routes that are defined are registered. What is the recommended approach here?eneajaho commentedon Nov 13, 2023
One other use-case where running server.ts in dev mode (ng serve) would be to be able to change providers.
Ex. In browser I want to use normal assets loading, while in SSR mode I want to be able to read from file-system and inline icons for better perf.
Usecase: https://github.com/push-based/ngx-fast-svg#ssr-usage
I don't see any way to make that work with the current setup.
SaeedDev94 commentedon Nov 14, 2023
Imagine we created a fresh new (SSR) project with latest version of
@angular/cliand we want to make sure that angular ssr server returns404HTTP Status Code instead of200for unregistered routes or we want to redirect a path to another based on some conditions only, so we should set301HTTP Status Code and ...We definitely need server
responseobject to set the right HTTP Status Code (and alsorequestfor other use cases)My question is: How we can do it in development environment ?!
oriollpz commentedon Nov 16, 2023
132 remaining items
jhereck commentedon Sep 19, 2024
Hi any news on that pb pls ?
Already spend a whole day to search a way to make providers / DI token from SSR server work in client without results : tried with v18.2.5 AND v19.0.0-next.7 (new project to try with the new version), same (no) results.
My setup :
Tokens :
Server :
Component :
I get results in server console, but nothing (null / undefined) in browser console.
It seems that providers are not transferred to the client, like many others already told.
Any help would be really appreciated !
aaubry commentedon Sep 20, 2024
I use a workaround that is not applicable in every situation but may help someone else. This is not a generic solution but allows to read specific headers from the request. In this case it is the cookie, but it should work with other headers as well.
I have a web server that acts as a reverse proxy for the application. I configured that reverse proxy to take the "Cookie" header and pass it as a query string parameter. I the access the cookie by parsing the URL from
PlatformLocation.This is the reverse proxy configuration that I use, in this case for IIS. It should be fairly easy to reproduce this with other servers such as nginx.
To access the cookie, I use the following code:
Of course that code is only used in development and on the server-side. I use an environment parameter to know which environment the code is running on, and also check
isPlatformServer.0ba100 commentedon Sep 24, 2024
Thanks angular team
fluoxa commentedon Sep 24, 2024
Thank you very much
shady-ahmed-bdr commentedon Sep 24, 2024
angular would be on fire with these updates
thank you angular team for the effort.
btw when we will be able to use it next patch ? or V19 ?
alan-agius4 commentedon Sep 24, 2024
This will only available in v19, which is currently in pre-release.
zygarios commentedon Sep 24, 2024
Thank you! Finally we'll be able to migrate 💪
kewur commentedon Sep 24, 2024
are there any changes we need to do to get this working on our side?
humanely commentedon Sep 24, 2024
How does this work? Is there a demo script?
jhereck commentedon Sep 24, 2024
Probably available in next v19 release !
shady-ahmed-bdr commentedon Sep 25, 2024
https://github.com/JeanMeche/ssr-v19
from a post on mgechev page
angular-automatic-lock-bot commentedon Oct 26, 2024
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.