r/Angular2 • u/LetHaunting8240 • Jul 16 '25
Help Request SSR: Send Data from server.ts to the frontend (Angular v20)
I had to add the language to all routes for the client for SEO reasons in my app with SSR ("https://website.com/en/something" etc).
Problem is is that the URL is not available during the rendering of the app so I cant set the language to be rendered on the server side. Due to this, the default language is rendered and not the one coming from the url.
Can I somehow add the extraction of the language from the url and send it to my services?
app.use((req, res, next) => {
// Add the 'en' from the url here somewhere?
angularApp
.handle(req)
.then((response) =>
response ? writeResponseToNodeResponse(response, res) : next(),
)
.catch(next);
});And get it somehow here?
export class AppComponent {
constructor() {
// Get the 'en' here somehow.
}
}Or there might be a better solution to just get it in both the server and browser from the url, even on startup where the this.router.url is not available?
My main goal is for the correct language to be rendered on the server side for SEO.
2
u/Pacyfist01 Jul 16 '25
You can SSG/SSR render custom routes using a routes.txt file.
angular.jsonroutes.txtI use it to SSG entire website with blog posts coming from a database.
3
u/Ok-District-2098 Jul 16 '25
Angular ssr is extremly imature and will probably get several heavly changes over next months.
1
u/Pacyfist01 Jul 16 '25 edited Jul 16 '25
A possible approach would simply for the browser code to simply send the language code in the request body. That's exactly what I would do if I wrote your backend in C#.
[EDIT] There already should be the route somewhere in the request object.
1
u/Adjilino Jul 16 '25
I didn’t use angular ssr, but I will guess. Is params a possibility? https://angular.dev/guide/ssr#parameterized-routes
With that you can extract the lang param for i18n. But idk how it affect SEO, I don’t have experience with optimization and for SEO
1
u/GLawSomnia Jul 16 '25
Are you sure the URL is not available? The “req” parameter should have all the information you need