angular-cli icon indicating copy to clipboard operation
angular-cli copied to clipboard

PWA ng-add schematic does not work if bootstrapApplication is used (standalone)

Open ngfelixl opened this issue 2 years ago • 4 comments

🐞 Bug report

Command (mark with an x)

  • [ ] new
  • [ ] build
  • [ ] serve
  • [ ] test
  • [ ] e2e
  • [x] generate
  • [x] add
  • [ ] update
  • [ ] lint
  • [ ] extract-i18n
  • [ ] run
  • [ ] config
  • [ ] help
  • [ ] version
  • [ ] doc

Is this a regression?

In the previous version of Angular (13) there was no support for standalone components.

Description

The ng-add schematic for the package @angular/pwa does not work if the app bootstraps using bootstrapApplication from @angular/platform-browser instead of the good old bootstrapModule function in the apps main.ts file.

🔬 Minimal Reproduction

1.) Create a new Angular app with version 14:

npx @angular/cli@14 new pwa-test-app
cd pwa-test-app

2.) Remove the app.module.ts file and change the app.component.ts decorator to

@Component({
  standalone: true,
  selector: 'app-root',
  imports: [CommonModule],
  templateUrl: './app.component.html',
  styleUrls: ['./app.component.css']
})

3.) Change the main.ts file to

import { enableProdMode } from '@angular/core';
import { bootstrapApplication } from '@angular/platform-browser';
import { AppComponent } from './app/app.component';
import { environment } from './environments/environment';

if (environment.production) {
  enableProdMode();
}

bootstrapApplication(AppComponent);

4.) ng-add @angular/pwa

Run

npx ng add @angular/pwa

Answer yes to the following terminal output

ℹ Using package manager: npm
✔ Found compatible package version: @angular/[email protected].
✔ Package information loaded.

The package @angular/[email protected] will be installed and executed.
Would you like to proceed? (Y/n) y

🔥 Exception or Error


Bootstrap call not found

🌍 Your Environment


Angular CLI: 14.0.0
Node: 16.14.0
Package Manager: npm 8.3.1 
OS: darwin x64

Angular: 14.0.0
... animations, cli, common, compiler, compiler-cli, core, forms
... platform-browser, platform-browser-dynamic, router

Package                         Version
---------------------------------------------------------
@angular-devkit/architect       0.1400.0
@angular-devkit/build-angular   14.0.0
@angular-devkit/core            14.0.0
@angular-devkit/schematics      14.0.0
@schematics/angular             14.0.0
rxjs                            7.5.5
typescript                      4.7.3

I would be very happy to support you with this issue if some guidance is provided.

ngfelixl avatar Jun 08 '22 07:06 ngfelixl

Instead of only looking for bootstrapModule, it should also search for bootstrapApplication. If the first one is found, nothing to be changed. If the second one is found it should add it to the providers array as it does for modules:

import { importProvidersFrom } from '@angular/core';
import { bootstrapApplication } from '@angular/platform-browser';
// [...]

bootstrapApplication(AppComponent, {
  providers: [
    importProvidersFrom(
      ServiceWorkerModule.register('ngsw-worker.js', {
        enabled: environment.production,
        // Register the ServiceWorker as soon as the application is stable
        // or after 30 seconds (whichever comes first).
        registrationStrategy: 'registerWhenStable:30000',
      })
    ),
  ],
});

This works fine for my app. For everyone looking for a solution: As a workaround, you can setup a temporary NgModule AppModule and use the bootstrapModule function. This way the schematic succeeds. Once it's done, change it back to bootstrapApplication, remove the NgModule and use the above snippet to register the ServiceWorker.

ngfelixl avatar Jun 08 '22 12:06 ngfelixl

Developer Preview APIs currently do not have full tooling support. Additional tooling support for Standalone Components will be added in the 14.x timeframe as the APIs are stabilized.

clydin avatar Jun 08 '22 13:06 clydin

I see! Thanks for the clarification 😊

ngfelixl avatar Jun 08 '22 20:06 ngfelixl

I faced same issue today, detail I described here https://stackoverflow.com/questions/73274104/ng-add-angular-pwa-bootstrap-call-not-found-in-angular-standalone-project. Look forward to it ....

sefatanam avatar Aug 08 '22 08:08 sefatanam

Still waiting for the fix

DDD-fx avatar Jan 28 '23 16:01 DDD-fx

Today I tried to add angular-pwa to my project running Angular v15 but unfortunately it still does not work with bootstrapApplication :

The package @angular/[email protected] will be installed and executed.
Would you like to proceed? Yes
✔ Packages successfully installed.
Bootstrap call not found

This is not a preview feature anymore right ?

Yutsa avatar Apr 04 '23 05:04 Yutsa

This is available in version 16 which is currently in prerelease.

alan-agius4 avatar Apr 04 '23 06:04 alan-agius4

Closing as per above.

alan-agius4 avatar May 02 '23 10:05 alan-agius4

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.