Skip to content

Enable declaration of async routes for static prerendering #25721

@michaeljota

Description

@michaeljota

Proposal

What is the summary of the proposal?

Enable declaration of async routes for static prerendering to be able to calculate dynamic URLs.

What is the proposal?

Right now, Angular Universal has two ways to declare the routes for static prerendering, but both are sync solutions. One is declaring the routes as an array inside the "routes" option of @nguniversal/builders:prerender builder, and the other one is declaring the routes as newline-separated values inside a .txt file and declare it in "routesFile" option of the same builder.

My proposal is to add another option to the @nguniversal/builders:prerender, like "asyncRoutes", that accepts a TypeScript file, exporting a named async function that accepts enough params to be able to make requests on its own and returns a string array with the routes.

This proposal would allow getting the params of dynamic routes, for example, a complete list of posts according to their ID, or friendly URL. Things that would require a lot of work each time a new one is added.

One of the requirements of this would be, be able to make requests and process the responses without external libraries. For this I'm thinking about two different solutions:

a) The function can be called with arguments that allow this behavior. Ideally, an instance of HttpClient would be injected, but as far I can tell, HttpClient uses XMLHttpRequest object, that's only available in the Browser, the HttpClient would be available only inside an Angular application, and other considerations that I think make this unviable.

However, we would only need to call the function with an object with the same shape as HttpClient. The idea behind this is for the programmers to work with a known interface to calculate the routes of their application.

b) A complete Angular Service implementing an interface with the method to be called. I think this would be more complex to do but would allow reusing the same services that are probably being used to gets the details of the routes.

Is there anything else we should know?

Metadata

Metadata

Assignees

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions