Skip to content

Enable declaration of async routes for static prerendering #25721

Closed

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?

Activity

  1. wagnermaciel commented on Oct 15, 2020

    @wagnermaciel
    Contributor

    @alan-agius4 I think this would actually be a good idea. It would also help alleviate some of the problems we were seeing in this issue: angular/universal#1769

  2. MarcoGlauser commented on Feb 9, 2021

    @MarcoGlauser

    From a user perspective, the current way of passing routes is one of the pain points when using prerendering.

    We wrote quite some code to dynamically generate the routes for content living in a cms.(Also building on angular/universal#1736) This code feels quite fragile, as it's dependent on both angular routes not changing, and evolving content from the CMS. Additionally, it duplicates code which is already present in the Angular application.

    I don't know how Universal works under the hood but in my opinion, the best place to define the routes for prerendering would be within the angular application. It creates all the rules for routing in the first place and anything external ends up trying to replicate the same. So I would love to see option b) at some point in the future and be able to alleviate some of our technical debt :)

  3. added
    feature
    on Feb 13, 2023
  4. added
    feature: votes required
    on Aug 29, 2023
  5. angular-robot commented on Aug 29, 2023

    @angular-robot
  6. michaeljota commented on Sep 3, 2023

    @michaeljota
    Author

    @alexcibotari I saw you opened another issue related to this one, and with this one requesting upvotes, maybe you want to upvote. Still, I don't think this will get the upvotes it deserves.

    Is there something else we could do here @alan-agius4?

  7. angular-robot commented on Sep 18, 2023

    @angular-robot
  8. added
    feature: insufficient votes
    and removed
    feature: votes required
    on Sep 18, 2023
  9. self-assigned this
    on Oct 2, 2023
  10. added
    feature: in backlog
    and removed
    feature: insufficient votes
    on Oct 2, 2023
  11. alan-agius4 commented on Sep 13, 2024

    @alan-agius4
    Collaborator

    Closed via #28346

  12. angular-automatic-lock-bot commented on Oct 14, 2024

    @angular-automatic-lock-bot

    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.

  13. locked and limited conversation to collaborators on Oct 14, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

angular/build:applicationarea: @angular-devkit/build-angulardevkit/build-angular:prerenderfeaturefeature: in backlog

Type

No type

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions