Skip to content

Defer chunks being preloaded when they should not be #30541

Closed

Description

@thePunderWoman

Command

run

Is this a regression?

  • Yes, this behavior used to work in the previous version

The previous version in which this bug was not present was

19

Description

It seems that regular defer triggers are being prefetched despite not having prefetch on or prefetch when trigger. This was reported by one of our GDEs. This did work in past versions of Angular. So something must have changed with how these files are being bundled and inlined.

Minimal Reproduction

Minimal repro: https://stackblitz.com/~/github.com/AhsanAyaz/ng-defer-bundle-preload-example?file=src/app/app.html:L338

While running, you can view the SSR page source and see the files listed as modulepreload.

Image

Exception or Error


Your Environment

_                      _                 ____ _     ___
    / \   _ __   __ _ _   _| | __ _ _ __     / ___| |   |_ _|
   / △ \ | '_ \ / _` | | | | |/ _` | '__|   | |   | |    | |
  / ___ \| | | | (_| | |_| | | (_| | |      | |___| |___ | |
 /_/   \_\_| |_|\__, |\__,_|_|\__,_|_|       \____|_____|___|
                |___/
    

Angular CLI: 20.0.2
Node: 20.19.1
Package Manager: npm 10.8.2
OS: linux x64

Angular: 20.0.3
... common, compiler, compiler-cli, core, forms
... platform-browser, platform-server, router

Package                      Version
------------------------------------------------------
@angular-devkit/architect    0.2000.2
@angular-devkit/core         20.0.2
@angular-devkit/schematics   20.0.2
@angular/build               20.0.2
@angular/cli                 20.0.2
@angular/ssr                 20.0.2
@schematics/angular          20.0.2
rxjs                         7.8.2
typescript                   5.8.3
zone.js                      0.15.1

Anything else relevant?

No response

Activity

  1. alan-agius4 commented on Jun 16, 2025

    @alan-agius4
    Collaborator

    Preloading occurs when the visited route includes a lazy-loaded bundles, this feature was added in 19.1.0.

    In this case, since there's a timer involved, we probably shouldn’t trigger preloading but the bundler lacks the contextual awareness. One possible solution is to enhance the Angular compiler to insert specific instructions into import statements: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/import/with](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/import/with

    Ultimately, this would require compiler-level changes to annotate certain import calls with alternative behavior. Alternatively, we could simply avoid adding preloads for modules that aren't explicitly defined in the router configuration. However, this approach would prevent all deferred blocks from being preloaded, which may not be desirable.

  2. AhsanAyaz commented on Jun 16, 2025

    @AhsanAyaz
    Contributor

    As per the current documentation of the defer blocks, especially about prefetch, the alternative approach makes more sense to not preload deferred blocks until prefetch is specified. I can see how it can make it really confusing for developers to understand what @defer really does. I.e. does it download the javascript bundle on the on and when triggers? Or only render the component/load in the component tree when the triggers happen?

    I think not preloading the deferred blocks makes much more sense. If someone wants to preload, they can use the prefetch keyword with their deferred blocks.

    The documentation also doesn't mention that the default behavior is prefetch on idle.

  3. added a commit that references this issue on Jun 19, 2025
    c7bb75c
  4. added 2 commits that reference this issue on Jun 23, 2025
    861a61a
    3097422
  5. angular-automatic-lock-bot commented on Jul 24, 2025

    @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.

  6. locked and limited conversation to collaborators on Jul 24, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions