Skip to content

Commit 0f5fb7e

Browse files
alan-agius4dgp1130
authored andcommitted
fix(@schematics/angular): replace existing BrowserModule.withServerTransition calls when running universal schematic
This change fixes an issue where calling the universal schematic on an application with existing `BrowserModule.withServerTransition` will cause an additional `.withServerTransition` call to be added. With this change we now remove the previous `withServerTransition` call to avoid misconfiguration. Closes #24563 (cherry picked from commit c8a3b30)
1 parent bc5555c commit 0f5fb7e

File tree

2 files changed

+48
-7
lines changed

2 files changed

+48
-7
lines changed

packages/schematics/angular/universal/index.ts

Lines changed: 15 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,6 @@ import {
2323
import { NodePackageInstallTask } from '@angular-devkit/schematics/tasks';
2424
import * as ts from '../third_party/github.com/Microsoft/TypeScript/lib/typescript';
2525
import { findNode, getDecoratorMetadata } from '../utility/ast-utils';
26-
import { InsertChange } from '../utility/change';
2726
import {
2827
NodeDependencyType,
2928
addPackageJsonDependency,
@@ -202,13 +201,22 @@ function addServerTransition(
202201
);
203202

204203
const browserModuleImport = findBrowserModuleImport(host, bootstrapModulePath);
205-
const appId = options.appId;
206-
const transitionCall = `.withServerTransition({ appId: '${appId}' })`;
207-
const position = browserModuleImport.pos + browserModuleImport.getFullText().length;
208-
const transitionCallChange = new InsertChange(bootstrapModulePath, position, transitionCall);
209-
210204
const transitionCallRecorder = host.beginUpdate(bootstrapModulePath);
211-
transitionCallRecorder.insertLeft(transitionCallChange.pos, transitionCallChange.toAdd);
205+
const position = browserModuleImport.pos + browserModuleImport.getFullWidth();
206+
const browserModuleFullImport = browserModuleImport.parent;
207+
208+
if (browserModuleFullImport.getText() === 'BrowserModule.withServerTransition') {
209+
// Remove any existing withServerTransition as otherwise we might have incorrect configuration.
210+
transitionCallRecorder.remove(
211+
position,
212+
browserModuleFullImport.parent.getFullWidth() - browserModuleImport.getFullWidth(),
213+
);
214+
}
215+
216+
transitionCallRecorder.insertLeft(
217+
position,
218+
`.withServerTransition({ appId: '${options.appId}' })`,
219+
);
212220
host.commitUpdate(transitionCallRecorder);
213221
};
214222
}

packages/schematics/angular/universal/index_spec.ts

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -158,6 +158,39 @@ describe('Universal Schematic', () => {
158158
expect(contents).toMatch(/BrowserModule\.withServerTransition\({ appId: 'serverApp' }\)/);
159159
});
160160

161+
it('should replace existing `withServerTransition` in BrowserModule import', async () => {
162+
const filePath = '/projects/bar/src/app/app.module.ts';
163+
appTree.overwrite(
164+
filePath,
165+
`
166+
import { NgModule } from '@angular/core';
167+
import { BrowserModule } from '@angular/platform-browser';
168+
169+
import { AppRoutingModule } from './app-routing.module';
170+
import { AppComponent } from './app.component';
171+
172+
@NgModule({
173+
declarations: [
174+
AppComponent
175+
],
176+
imports: [
177+
BrowserModule.withServerTransition({ appId: 'foo' }),
178+
AppRoutingModule
179+
],
180+
providers: [],
181+
bootstrap: [AppComponent]
182+
})
183+
export class AppModule { }
184+
`,
185+
);
186+
const tree = await schematicRunner.runSchematic('universal', defaultOptions, appTree);
187+
const contents = tree.readContent(filePath);
188+
console.log(contents);
189+
190+
expect(contents).toContain(`BrowserModule.withServerTransition({ appId: 'serverApp' }),`);
191+
expect(contents).not.toContain(`withServerTransition({ appId: 'foo' })`);
192+
});
193+
161194
it('should wrap the bootstrap call in a DOMContentLoaded event handler', async () => {
162195
const tree = await schematicRunner.runSchematic('universal', defaultOptions, appTree);
163196
const filePath = '/projects/bar/src/main.ts';

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy