Skip to content
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

feat(@angular/ssr): add modulepreload for lazy-loaded routes #28919

Open
wants to merge 1 commit into
base: main
Choose a base branch
from

Conversation

alan-agius4
Copy link
Collaborator

Enhance performance when using SSR by adding modulepreload links to lazy-loaded routes. This ensures that the required modules are preloaded in the background, improving the user experience and reducing the time to interactive.

Closes #26484

@alan-agius4 alan-agius4 added action: review The PR is still awaiting reviews from at least one requested reviewer target: minor This PR is targeted for the next minor release labels Nov 21, 2024
@angular-robot angular-robot bot added detected: feature PR contains a feature commit area: @angular/ssr labels Nov 21, 2024
@alan-agius4 alan-agius4 force-pushed the preload-ssr branch 2 times, most recently from 95f2b19 to 9b4c01a Compare November 21, 2024 11:40
@alan-agius4 alan-agius4 force-pushed the preload-ssr branch 5 times, most recently from 913f62e to 1eb3c20 Compare November 22, 2024 10:22
Copy link
Collaborator

@dgp1130 dgp1130 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for putting this together! I left some initial thoughts.

I think my biggest concern is that I'm not sure how comfortable I am .toString()-ing a function and parsing out the dynamic import() statement at runtime in a production server. It's hard to explain concrete concerns, but it feels off to have a program inspect its own source code and alter behavior as a result.

I'm curious to explore if there is an opportunity with a compiler transform to create a similar result without having to toString an arbitrary function? Let's chat about this and see what our options are. @AndrewKushnir might also be interested in this.

packages/angular/ssr/src/app.ts Show resolved Hide resolved
return html;
}

const bodyCloseIdx = html.lastIndexOf('</body>');
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Consider: Users can do pretty weird things in the HTML like:

<!DOCTYPE html>
<html>
  <body>
    <!-- ... -->
  </body>
  <script>
    document.documentElement.outerHTML.replace('</body>', '</body><script src="..."></script>');
  </script>
</html>

Technically the last </body> would be in the JS, not the HTML.

It's a pretty extreme scenario, so I don't know that we really need to be too concerned about it. The "correct" solution is likely using a real HTML parser, but that might be worth it for this kind of edge case.

I mainly just wanted to point out this potential issue, up to you if it's worth solving. If we do choose to leave this, I would recommend at least having a comment which calls out that behavior.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is quite an extreme edge case, that I feel that we should not worry about right now.

packages/angular/ssr/src/app.ts Show resolved Hide resolved
packages/angular/ssr/src/routes/ng-routes.ts Outdated Show resolved Hide resolved
packages/angular/ssr/src/app.ts Show resolved Hide resolved
packages/angular/ssr/src/app.ts Show resolved Hide resolved
packages/angular/ssr/src/routes/ng-routes.ts Outdated Show resolved Hide resolved
@alan-agius4
Copy link
Collaborator Author

I think my biggest concern is that I'm not sure how comfortable I am .toString()-ing a function and parsing out the dynamic import() statement at runtime in a production server.

This only happens during the build, when building the manifest, on a production server the preloads are already extracted.

Enhance performance when using SSR by adding `modulepreload` links to lazy-loaded routes. This ensures that the required modules are preloaded in the background, improving the user experience and reducing the time to interactive.

Closes angular#26484
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
action: review The PR is still awaiting reviews from at least one requested reviewer area: @angular/ssr detected: feature PR contains a feature commit target: minor This PR is targeted for the next minor release
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Preload all js bundles required for a specific route
4 participants