Skip to content
This repository was archived by the owner on Nov 22, 2024. It is now read-only.
This repository was archived by the owner on Nov 22, 2024. It is now read-only.

Separate the building of browser and server code in pre-rendering script #1736

Closed as not planned

Description

@musso

🚀 Feature request

Separate the building of browser and server code in pre-rendering script

What modules are relevant for this feature request?

  • aspnetcore-engine
  • builders
  • common
  • express-engine
  • hapi-engine
  • module-map-ngfactory-loader

Description

Currently when running ng run myApp:prerender, it consists of building the server and browser code. When the server code is done, the build script will move on building browser code for each route.

In most CMS scenario, the code base is the same. Only the data in database or the deletion/additional of route are needed. It will be faster if there exists something like ng run myApp:prerender --only route, only building the routes from existing server code.

Activity

  1. changed the title [-]Separate the build browser and server code in pre-rendering script[/-] [+]Separate the building of browser and server code in pre-rendering script[/+] on Jun 17, 2020
  2. CaerusKaru commented on Jun 17, 2020

    @CaerusKaru
    Member

    There's a routes option for the prerender builder. Is that what you're looking for?

  3. musso commented on Jun 17, 2020

    @musso
    Author

    There's a routes option for the prerender builder. Is that what you're looking for?

    What I suggest is having an option to skip the server build progress, just build the browser progress. Since the server code will be the same every time if there is only change in database, not code itself.

    Say there is content management system, user update the post to trigger a CI/CD to render the static page again. It will take minutes to build everything again. If it is possible to cache the pre-built server code and just run the necessary route, it will be much faster.

    image

    As far as I know, the current script build /server folder first and then the browser folder. Then finally the static page html for particular route.

  4. MarcoGlauser commented on Aug 4, 2020

    @MarcoGlauser

    We have a use case that I believe is similar.

    We want to prerender our public pages that change infrequently but are still managed by a cms for the ease of updating content.
    We retrieve the content through an api call.
    In a case of a content update, we need to recompile the whole angular application even though the code itself has not changed.
    This means that it takes serveral minutes until the new prerendered page is online. The routes argument does not help in this case.

    I know that the express-engine exists, which handles dynamic content better but it is an overkill for us, since it adds a lot of complexity for data that only changes every few days or weeks. We're okay with blank pages for authenticated users.

    It would be useful if routes could be prerendered without having to recompile the server and browser completely every time.
    This would greatly reduce the time to deploy and make this solution more workable.

    Splitting the route generation and compiling would be a good solution for us, because we could cache the intermediate compilation result.

    Something along the lines of

    ng run project:prerender                          # compiles the server and browser target. this will be cached.
    ng run project:prerender-routes --routes "/home"  # only compiles the browser target for certain routes.

    EDIT:
    Thinking about this a bit more maybe it would be possible to add an option to prerender to not rebuild the targets.

    ng run project:prerender
    ng run project:prerender --no-browser-target --no-server-target --routes "/homes"
    

    @CaerusKaru I'd be happy to help but I don't understand the inner workings of angular builders well enough. Is there some documentation or information you can point me to?

  5. wagnermaciel commented on Aug 15, 2020

    @wagnermaciel
    Contributor

    Context

    When I was working on the design doc for this with Vikram we talked about adding an option like this but decided against it to avoid developers accidentally prerendering an outdated build. We were especially concerned with this happening if we ever coupled prerendering and ngdeploy - meaning a developer could accidentally deploy the incorrectly generated pages.

    Implementation Details

    If we look at line 162 of prerender/index.ts, we can see that we use the output of the browser and server builds to locate a couple of files like the base index.html and the sever bundle. That's the only major complication I can see here. We'd still need to find a way to provide that information to the _renderUniversal method.

    Conclusion

    If those concerns don't bother us enough, and if someone is willing to put in the work to figure out a neat solution for this, I think these changes are worth looking into. Especially because more than 90% of the time it takes to prerender comes from building the browser and server bundles.

  6. MarcoGlauser commented on Oct 6, 2020

    @MarcoGlauser

    We ended up copying and adapting the render.ts slightly.

    It prerenders 50+ routes within a couple of seconds, which makes content editing in the headless cms content and seeing the result after saving much more responsive. Which in turn creates less confusion.

  7. musso commented on Oct 6, 2020

    @musso
    Author

    @MarcoGlauser Can you explain it more?

  8. MarcoGlauser commented on Oct 6, 2020

    @MarcoGlauser

    We adopted the file to this for Angular 10.1
    https://gist.github.com/MarcoGlauser/80fd3d91529f4ab4c1f9e2e39875f324

    The main difference is that we don't pass the html content of index.html as a parameter and instead read the contents of the file during the process. This adds some overhead and might not work for some edge configurations but should work for most projects.

    We transpile it during the build process. To do it manually, this should work

    tsc prerender.ts
    

    Once transpiled, we call it with node.

    node prerender.js index.html dist/{myproject}/server/main.js dist/{myproject}/browser /route-to-prerender-1 /route-to-prerender-2
    

    To run it concurrently, you can call it with a routes.txt file and xargs:

    cat routes.txt | xargs --max-procs=4 node prerender.js index.html dist/{myproject}/server/main.js dist/{myproject}/browser
    

    This process works well for us, but chances that it breaks with future Angular releases are very high.
    Make sure you have the same version of the server and browser target, otherwise you're in trouble.

  9. webberig commented on Apr 28, 2022

    @webberig

    I have another use case for this feature.

    Our website is built as a docker image. This image is being deployed in different environments where different api hosts are used. Since prerender must use the proper environment variables, it can only run when a container is spinning up right before starting serve.

    Since prerender is also doing another build, spinning up the kubernetes pod takes much longer.

  10. jbeckton commented on May 18, 2022

    @jbeckton

    The best solution for this is to use React / Next.js. Angular just does not have the flexibility needed for this. I have a use case where I need to use Angular Universal and MongoDB Realm. Since Realm has two different SDK's, one for the browser and one for Node you cannot use them together in an Angular SSR app because of the way it builds. Since I don't feel like spending the time to write my backend from scratch I was forced to use React & Next.js.

  11. alan-agius4 commented on Feb 9, 2023

    @alan-agius4
    Collaborator

    Closing as per #1736 (comment)

  12. angular-automatic-lock-bot commented on Mar 12, 2023

    @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 Mar 12, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions