-
Notifications
You must be signed in to change notification settings - Fork 12k
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
Providing a DI token in commonEngine.render() does not provide its value to application #26323
Comments
Hi, have a look at #26175 which updated the schematics to import create the tokens that aren't exported anymore. |
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. |
This is expected as for 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 |
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 |
|
This is a serious issue IMO, we should be able to inject |
I've been confused by this as well. I was hoping there is an alternative way for Since the |
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. I don't see any way to make that work with the current setup. |
Imagine we created a fresh new (SSR) project with latest version of |
This comment was marked as spam.
This comment was marked as spam.
Not a fix for the issue but if you want to use esbuild found there is a browser-esbuild builder you can drop into an existing angular.json build. If, like myself, you do most development without SSR and then just test in SSR this is an alright method of getting the esbuild benefits in development, but it does warn that its temporary so hopefully before its removed we will be able to use application builder. |
I have ran into same issue (confusion). I generated fresh app using angular 17, with It was confusing and I waste time trying to fix it. It was super unintuitive, I had fresh app, generated by CLI, I've run app using |
One of the |
I wasted so much time because it was indeed rendered server side but yet the request provider was empty. The changelog of the last version (adding node run dist/xxx) put me on the path to this issue. So confusing behavior. It has to be at least clearly documented on the getting started page... |
In reference to my closed but related thread, I believe that the lack of use of server.ts in development mode is a very large regression. There are often things that we need to change there and be able to debug based on the RESPONSE and REQUEST objects. The configuration of this and the work on SSR has unnecessarily become more complicated. I hope that this will be somehow sensibly resolved because in its current form it is impossible for me to work with it on a daily basis and I am forced to use the classic approach in the Angular 16 version. |
How do they expect to work authentication with cookies without access to REQUEST RESPONSE from the server.ts? |
I agree, in development mode, we should be able to get the request object to do, for example, authentication through the cookie. This is a breaking change between v16 and v17, and I don't know if it was documented. |
Hello @santoshyadavdev, This PR fixes #476. What I did: - I couldn't use `yarn` at all this time, I switched to `npm` (even after everything was updated with `npm` and switching back to `yarn`) - I updated all packages to latest versions (except `jasmine-core`) - The nx cache is now located in the root folder of the repo, I added `.nx` in `.gitignore` - There was some problem with how the tsconfigs were done, I fixed all of them (it was causing problems with nx migrations) - I applied the migrations from nx - I migrated `ngu-carousel-example` to use the new control-flow - I did not update the `README.md` - I removed the cypress component testing config from `ngu-carousel-example` as it was more or less useless - I fixed errors found by the linter - I fixed unit tests - I removed `updateBuildableProjectDepsInPackageJson` from `ngu-carousel` as it was deprecated - removing it keeps the same behavior as if it was enable, that's what we want - I added missing `tsconfig.json` (cypress) in the `.eslintrc.json` of `ngu-carousel` - I updated the current SSR setup - I first tried to migrate the new `application` builder, but it was not working properly with modules. ~~I will investigate later if it comes from `nx` or the `Angular CLI`~~ (thanks @JeanMeche for his help) - Could be related to angular/angular#52998 - As said in https://riegler.fr/blog/2023-10-13-v17-builders: - `An interesting point to note is that if either 'ssr' or 'prerender' are set to 'true' in the configuration file, the pages served with 'ng serve' will be generated server side.` - This was not working, that's one of the reason I kept the same setup with `ssr-dev-server` & co. - I already did the migration to `browser-esbuild` when I migrated to `Angular 16`, so it is already using esbuild. - Another reason is that `ng serve` using the ssr is not using `server.ts` (which should not affect this repo, but let's play it safe), track angular/angular-cli#26323 - I cleaned `polyfills` usage - I updated the `CI` setup - Angular supports node starting version `18.13.0`, the CI now runs on `['18.13.0', '20']` - Updated to `actions/checkout@v4` & `actions/setup-node@v4` - Updated to use `npm` - I bumped `ngu-carousel` for `Angular 17` - I bumped `ngu-carousel` to `9.0.0` and I updated `README.md` TODO on your side: - Release a new major version `9.0.0` - Close branches related to package update Tell me if everything looks ok for you. Have a nice day.
In order to run |
Assuming you don't want to prerender your site, you can set |
Sooo can we get someone from Core team to answer this how and when they are going to address it? And sorry for being rude - as I said previously providing the REQUEST/RESPONSE tokens is just not enough... |
There is ongoing work to solve this issue here: #28139 Basically what was talked about in the RFC angular/angular#56785 |
I've been following this thread and thank you to all who have suggested work arounds. I am able to get DI tokens from server.ts to my app using InjectionTokens, Does anyone know how to solve this? I'm wanting to use Firebase's App Hosting to host the project so can't really have the server listen on a fixed IP Address. Thanks |
In case it helps anyone, I found the solution to my problem. Angular was having the browser cache the server rendered page so my tokens wouldn't repopulate on page loads/reloads. To fix this I had to add |
Any workaround for this in angular 18? |
This will fix in v19.0.0 3c9697a (previously the team said ssr fix come with v18.0.0 🙄) |
https://github.com/angular/angular-cli/releases/tag/19.0.0-next.0 U can access it on npm -> https://www.npmjs.com/package/@angular/ssr/v/19.0.0-next.0 (Be aware it is a pre-release for now) |
No fixes yet. |
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 ! |
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 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>
<location path="." inheritInChildApplications="false">
<system.webServer>
<rewrite>
<rules>
<!-- Remove the 'cookie' parameter from the query string for resources and the vite server -->
<rule name="Reverse Proxy new site (files and vite)" stopProcessing="true">
<match url="^([^?]+(?:\.\w+|vite/client))" />
<action type="Rewrite" url="http://localhost:4200/{R:1}" />
</rule>
<rule name="Reverse Proxy new site (ws)" stopProcessing="true">
<match url="^$" />
<action type="Rewrite" url="http://localhost:4200/" />
</rule>
<!-- Inject the cookie header as a query string parameter -->
<rule name="Reverse Proxy new site and inject cookie" stopProcessing="true">
<match url="^(.*)" />
<conditions>
<add input="{HTTP_COOKIE}" pattern="(.+)" />
</conditions>
<action type="Rewrite" url="http://localhost:4200/{R:1}?cookie={C:1}" />
</rule>
</rules>
</rewrite>
</system.webServer>
</location>
</configuration> To access the cookie, I use the following code: const cookiePrefix = "cookie=";
const location = inject(PlatformLocation);
const queryString = location.href.split("?")[1];
if (queryString) {
const cookieParam = queryString.split("&").filter(p => p.startsWith(cookiePrefix))[0];
if (cookieParam) {
cookieString = decodeURIComponent(cookieParam.substring(cookiePrefix.length));
}
} 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 |
Thanks angular team |
Thank you very much |
angular would be on fire with these updates |
This will only available in v19, which is currently in pre-release. |
Thank you! Finally we'll be able to migrate 💪 |
are there any changes we need to do to get this working on our side? |
How does this work? Is there a demo script? |
Probably available in next v19 release ! |
https://github.com/JeanMeche/ssr-v19 |
This issue has been automatically locked due to inactivity. Read more about our automatic conversation locking policy. This action has been performed automatically by a bot. |
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
https://github.com/Ozzcer/angular-ssr-di-issue
Please provide the exception or error you saw
Please provide the environment you discovered this bug in (run
ng version
)Anything else?
No response
The text was updated successfully, but these errors were encountered: