Skip to content

ng serve (@angular/build:dev-server) provides broken chunk files #30774

@RaederDev

Description

@RaederDev

Which @angular/* package(s) are the source of the bug?

Don't known / other

Is this a regression?

Yes

Description

This is only an issue with the dev server as far as I can tell. My production build works fine. I declared a canActivate guard that is included too late in the chunk file causing some routes to break. I have included a heavily redacted chunk file below to explain the issue so only relevant code is included:

/* start of file with a bunch of code */

// src/app/shared/guards/auth-guard.ts
import { Router as Router3 } from "/@fs/Users/myuser/projects/redacted-frontend/angular/.angular/cache/20.0.2/redacted/vite/deps/@angular_router.js?v=cdbf4a34";
import { inject as inject5 } from "/@fs/Users/myuser/projects/redacted-frontend/angular/.angular/cache/20.0.2/redacted/vite/deps/@angular_core.js?v=cdbf4a34";

// src/app/shared/services/app-init-service.ts
import { inject as inject4, Injectable as Injectable4 } from "/@fs/Users/myuser/projects/redacted-frontend/angular/.angular/cache/20.0.2/redacted/vite/deps/@angular_core.js?v=cdbf4a34";
import { HttpErrorResponse } from "/@fs/Users/myuser/projects/redacted-frontend/angular/.angular/cache/20.0.2/redacted/vite/deps/@angular_common_http.js?v=cdbf4a34";

// src/app/auth/services/auth-service.ts
import { DOCUMENT, inject as inject3, Injectable as Injectable3 } from "/@fs/Users/myuser/projects/redacted-frontend/angular/.angular/cache/20.0.2/redacted/vite/deps/@angular_core.js?v=cdbf4a34";

// src/app/user/constants/user-routes.ts
var USER_ROUTES_PREFIX = "user";
var UserRoutes = {
  PROFILE: [USER_ROUTES_PREFIX, "profile"],
  SETTINGS: [USER_ROUTES_PREFIX, "settings"],
};
var userRoutes = [
  {
    path: UserRoutes.PROFILE.join("/"),
    loadComponent: () => import("/chunk-BY5FYYLS.js").then((m) => m.UserProfilePage),
    canActivate: [authGuard]
  },
  {
    path: UserRoutes.SETTINGS.join("/"),
    loadComponent: () => import("/chunk-H56AUES5.js").then((m) => m.UserSettingsPage),
    canActivate: [authGuard]
  },
];

/* more removed code */

// src/app/shared/guards/auth-guard.ts
import { filter, map, take } from "/@fs/Users/myuser/projects/redacted-frontend/angular/.angular/cache/20.0.2/redacted/vite/deps/rxjs.js?v=cdbf4a34";
var authGuard = () => {
  const appInitService = inject5(AppInitService);
  const storage = inject5(StorageService);
  const router = inject5(Router3);
  return appInitService.$appInitialized.pipe(filter((v) => v), take(1), map(() => {
    if (storage.getItem(StorageKey.AUTH_TOKEN)) {
      return true;
    }
    return router.createUrlTree(["/", ...AuthRoutes.WELCOME_LOGIN]);
  }));
};

/* end of file with a bunch of code */

As you can see userRoutes depends on authGuard to be set however it's declared later in the chunk file leading to the angular router throwing an Exception because undefined was passed instead of a canActivate guard.

This is an abbreviated version of the original source file:

import { Routes as NgRoutes } from '@angular/router';
import { authGuard } from '../../shared/guards/auth-guard';

export const USER_ROUTES_PREFIX = 'user';

export const UserRoutes = {
  PROFILE: [USER_ROUTES_PREFIX, 'profile'],
  SETTINGS: [USER_ROUTES_PREFIX, 'settings'],
};

export const userRoutes: NgRoutes = [
  {
    path: UserRoutes.PROFILE.join('/'),
    loadComponent: () =>
      import('../pages/user-profile-page/user-profile-page').then(m => m.UserProfilePage),
    canActivate: [authGuard],
  },
  {
    path: UserRoutes.SETTINGS.join('/'),
    loadComponent: () =>
      import('../pages/user-settings-page/user-settings-page').then(m => m.UserSettingsPage),
    canActivate: [authGuard],
  },
];

As you can see it's being imported correctly and should therefore be declared before the user routes in the chunk file.

Please provide a link to a minimal reproduction of the bug

No response

Please provide the exception or error you saw


Please provide the environment you discovered this bug in (run ng version)

Angular CLI: 20.0.2
Node: 22.16.0
Package Manager: npm 10.9.2
OS: darwin arm64

Angular: 20.0.3
... common, compiler, compiler-cli, core, forms
... platform-browser, 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/cdk                 20.1.2
@angular/cli                 20.0.2
@schematics/angular          20.0.2
rxjs                         7.8.2
typescript                   5.8.3
zone.js                      0.15.1

Anything else?

No response

Metadata

Metadata

Assignees

No one assigned

    Labels

    needs: repro stepsWe cannot reproduce the issue with the information given

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions