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
docs: describe how to create server-side only services #53168
P3An issue that is relevant to core functions, but does not impede progress. Important, but not urgentarea: docsRelated to the documentationarea: serverIssues related to server-side rendering
Which @angular/* package(s) are relevant/related to the feature request?
platform-browser, platform-server
Description
As a developer, I would need a service to be injected and executed only on the server side as it contains a connection Token in the request URL.
Proposed solution
To define Injectable parameters you can define a platform where you would like a service to be injectable: server or browser
Alternatives considered
For the moment I'm using StateTransfer, to provide data, but still the service with a secure token is present the browser bundle, where I have a token exposed
This doesn't transfer the state. Should we use transferState on this? How would we get the server data hydrated without replacing it with the browser version?
I would think I would use the resolver to call a server service, then use the abstract for the browser version, but this will not work as the useClass never gets triggered.
This should be updated in the docs with a way to do this for the whole app AND some way to do this per route.
Hi @jdgamble555 I agree that there should be some proper way to do this at route level, but I think if you just call server only service in ngOnInit by checking isPlatformServer & save return data in transfer state then it should be readily available on client side & angular should also automatically cache this service response on server.
& I still think there should be some more explicit way of setting transfer state at route level.
Hi @imaksp. I appreciate your input but there are several problems here.
Async input should be done in the resolver before the component is loaded. ngOninit will not wait for async by default, even when you put async in front of it
I'm not calling an endpoint, I want to load data directly from the server on the first load, not call another endpoint.
If I use isPlatformServer, it will add my server-only data to the client application. Not only is this a security problem, it is adding extra overhead to my client bundle I don't need.
But this post is about the docs with loading in the resolver (or provider) before the component is loaded ONLY from the server and available on the server and browser version. Having transferState load anything from the resolver should also be done automatically, but that is another issue.
Yes if you are not using endpoint then some code might get exposed but if its api server code won't be exposed & server only secret from env variable could be used for api.
I think here function like next's getServerSideProps or getStaticProps would be more appropriate. Something like server only resolve function for route.
If I use isPlatformServer, it will add my server-only data to the client application. Not only is this a security problem, it is adding extra overhead to my client bundle I don't need.
J
Hi @jdgamble555 , I believe if you return data from server only service & call it from component's constructor by checking isPlatformServer then that code won't be included in client bundle & secrets used in server service won't be exposed. & you can get that data on client using transferState.get after checking isPlatformBrowser.
P3An issue that is relevant to core functions, but does not impede progress. Important, but not urgentarea: docsRelated to the documentationarea: serverIssues related to server-side rendering
Activity
alan-agius4 commentedon Nov 27, 2023
This is currently already possible by using DI and providing a different provider based on the platform.
data-service.service.ts
app.config.server.ts
app.config.ts
AndrewKushnir commentedon Nov 28, 2023
It looks like this might be a good addition to the docs, I'm updating ticket title to reflect that.
[-]Server side only service[/-][+]docs: describe how to create server-side only services[/+]jdgamble555 commentedon Feb 28, 2024
This doesn't transfer the state. Should we use
transferStateon this? How would we get the server data hydrated without replacing it with the browser version?J
jdgamble555 commentedon Feb 29, 2024
I tried to use DI, but the useState cannot be ran before the bootstrap:
https://github.com/jdgamble555/angular-state-test
We need a way to transfer the state.
J
jdgamble555 commentedon Mar 3, 2024
Got it working with
APP_INITIALIZER.https://dev.to/jdgamble555/preloading-data-in-angular-universal-20lj
J
This really should be some kind of pre-made provider.
jdgamble555 commentedon Mar 14, 2024
@alan-agius4 - I keep coming back to this. I don't want to load the same service on every route, some routes may have a specific service.
How would you do this per route?
If I use this method on the server, it will not get populated.
I would think I would use the resolver to call a server service, then use the abstract for the browser version, but this will not work as the
useClassnever gets triggered.This should be updated in the docs with a way to do this for the whole app AND some way to do this per route.
J
imaksp commentedon Mar 19, 2024
Hi @jdgamble555 I agree that there should be some proper way to do this at route level, but I think if you just call server only service in ngOnInit by checking isPlatformServer & save return data in transfer state then it should be readily available on client side & angular should also automatically cache this service response on server.
& I still think there should be some more explicit way of setting transfer state at route level.
jdgamble555 commentedon Mar 19, 2024
Hi @imaksp. I appreciate your input but there are several problems here.
ngOninitwill not wait for async by default, even when you put async in front of itisPlatformServer, it will add my server-only data to the client application. Not only is this a security problem, it is adding extra overhead to my client bundle I don't need.But this post is about the docs with loading in the resolver (or provider) before the component is loaded ONLY from the server and available on the server and browser version. Having
transferStateload anything from the resolver should also be done automatically, but that is another issue.J
imaksp commentedon Mar 19, 2024
Yes if you are not using endpoint then some code might get exposed but if its api server code won't be exposed & server only secret from env variable could be used for api.
I think here function like next's getServerSideProps or getStaticProps would be more appropriate. Something like server only resolve function for route.
imaksp commentedon Dec 19, 2024
Hi @jdgamble555 , I believe if you return data from server only service & call it from component's constructor by checking
isPlatformServerthen that code won't be included in client bundle & secrets used in server service won't be exposed. & you can get that data on client usingtransferState.getafter checkingisPlatformBrowser.jdgamble555 commentedon Jan 15, 2025
@imaksp - This is incorrect.
Using
isPlatformServerwill prevent server code from running in the browser, but it still gets put into the JS.However, this may be soon possible with something similar to "server-only" components.
analogjs/analog#989
I have not tested any of this.
J
alan-agius4 commentedon Dec 16, 2025
Closing as this is covered in https://angular.dev/guide/ssr#providing-platform-specific-implementations
angular-automatic-lock-bot commentedon Jan 16, 2026
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.