-
Notifications
You must be signed in to change notification settings - Fork 11.9k
feat(@angular/ssr): add server routing configuration API #28346
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
Conversation
0582d70
to
90bb423
Compare
90bb423
to
60be7e5
Compare
60be7e5
to
01df293
Compare
6fe6e24
to
40c6fe7
Compare
40c6fe7
to
7815301
Compare
44c284d
to
c1da755
Compare
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Great stuff here @alan-agius4! Glad to see this all coming together. 😁
); | ||
} | ||
|
||
const parameters = await runInInjectionContext(parentInjector, () => getPrerenderPaths()); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Question: How does runInInjectionContext
handle async callbacks? getPrerenderPaths()
might return a Promise
. What happens for:
function getPrerenderPaths() {
await someAsyncWork();
const service = inject(SomeService); // Can we inject after an `await`?
}
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
No, the inject
needs to happen before any async work.
function getPrerenderPaths() {
const service = inject(SomeService); // Can we inject after an `await`?
await someAsyncWork();
}
See: https://angular.dev/api/core/runInInjectionContext#runInInjectionContext_0
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Is there any precedent for Angular APIs which are naturally async but run in an injection context? This feels like an easy tripping hazard.
I guess the workaround would be to keep a reference to the injector (or inject before the await
like you suggested)?
async function getPrerenderPaths() {
const injector = inject(Injector);
const result = await someAsyncWork();
if (result === something) {
injector.inject(SomethingElse);
}
}
/cc @AndrewKushnir is this something we should be concerned about?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Both guards and interceptors run in an injection context.
d82eda1
to
bb0ca53
Compare
bb0ca53
to
3a932de
Compare
3a932de
to
22b4016
Compare
22b4016
to
041f1df
Compare
Thanks for working through this @alan-agius4! My only remaining concern is the choice of default value for |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
LGTM, just a few minor comments, mostly related to error message text improvements.
cb69759
to
f14ef08
Compare
77744f4
to
df3715f
Compare
This commit introduces a new server routing configuration API, as discussed in RFC angular/angular#56785. The new API provides several enhancements: ```ts const serverRoutes: ServerRoute[] = [ { path: '/error', renderMode: RenderMode.Server, status: 404, headers: { 'Cache-Control': 'no-cache' } } ]; ``` ```ts const serverRoutes: ServerRoute[] = [ { path: '/product/:id', renderMode: RenderMode.Prerender, async getPrerenderPaths() { const dataService = inject(ProductService); const ids = await dataService.getIds(); // Assuming this returns ['1', '2', '3'] return ids.map(id => ({ id })); // Generates paths like: [{ id: '1' }, { id: '2' }, { id: '3' }] } } ]; ``` ```ts const serverRoutes: ServerRoute[] = [ { path: '/product/:id', renderMode: RenderMode.Prerender, fallback: PrerenderFallback.Server, // Can be Server, Client, or None async getPrerenderPaths() { } } ]; ``` ```ts const serverRoutes: ServerRoute[] = [ { path: '/product/:id', renderMode: RenderMode.Server, }, { path: '/error', renderMode: RenderMode.Client, }, { path: '/**', renderMode: RenderMode.Prerender, }, ]; ``` These additions aim to provide greater flexibility and control over server-side rendering configurations and prerendering behaviors.
df3715f
to
b54d412
Compare
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. |
This commit introduces a new server routing configuration API, as discussed in RFC angular/angular#56785. The new API provides several enhancements:
These additions aim to provide greater flexibility and control over server-side rendering configurations and prerendering behaviors.