Skip to content

Commit c38b739

Browse files
committed
fix(@nguniversal/common): correctly handle lazy loaded routes in Clover
JSDom doesn't support script with `type=module` therefore we need to strip out this from the runtime.js. Closes angular#2433
1 parent 962585f commit c38b739

File tree

9 files changed

+69
-3
lines changed

9 files changed

+69
-3
lines changed

integration/clover/src/app/app-routing.module.ts

+1
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import { HomepageComponent } from './homepage.component';
55

66
const routes: Routes = [
77
{ path: '', component: HomepageComponent },
8+
{ path: 'lazy', loadChildren: () => import('./lazy/lazy.module').then((m) => m.LazyModule) },
89
{ path: '**', component: PokedexComponent },
910
];
1011

Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
import { NgModule } from '@angular/core';
2+
import { RouterModule, Routes } from '@angular/router';
3+
import { LazyComponent } from './lazy.component';
4+
5+
const routes: Routes = [{ path: '', component: LazyComponent }];
6+
7+
@NgModule({
8+
imports: [RouterModule.forChild(routes)],
9+
exports: [RouterModule],
10+
})
11+
export class LazyRoutingModule {}

integration/clover/src/app/lazy/lazy.component.css

Whitespace-only changes.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
<p>lazy works!</p>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
import { ComponentFixture, TestBed } from '@angular/core/testing';
2+
3+
import { LazyComponent } from './lazy.component';
4+
5+
describe('LazyComponent', () => {
6+
let component: LazyComponent;
7+
let fixture: ComponentFixture<LazyComponent>;
8+
9+
beforeEach(async () => {
10+
await TestBed.configureTestingModule({
11+
declarations: [LazyComponent],
12+
}).compileComponents();
13+
});
14+
15+
beforeEach(() => {
16+
fixture = TestBed.createComponent(LazyComponent);
17+
component = fixture.componentInstance;
18+
fixture.detectChanges();
19+
});
20+
21+
it('should create', () => {
22+
expect(component).toBeTruthy();
23+
});
24+
});
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
import { Component, OnInit } from '@angular/core';
2+
3+
@Component({
4+
selector: 'app-lazy',
5+
templateUrl: './lazy.component.html',
6+
styleUrls: ['./lazy.component.css'],
7+
})
8+
export class LazyComponent implements OnInit {
9+
constructor() {}
10+
11+
ngOnInit(): void {}
12+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
import { NgModule } from '@angular/core';
2+
import { CommonModule } from '@angular/common';
3+
4+
import { LazyRoutingModule } from './lazy-routing.module';
5+
import { LazyComponent } from './lazy.component';
6+
7+
@NgModule({
8+
declarations: [LazyComponent],
9+
imports: [CommonModule, LazyRoutingModule],
10+
})
11+
export class LazyModule {}

integration/clover/test.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ console.log({
1111
pages,
1212
});
1313

14-
const expectedNumberOfPages = 5;
14+
const expectedNumberOfPages = 6;
1515
if (pages.length !== expectedNumberOfPages) {
1616
throw new Error(`Expected to have ${expectedNumberOfPages} index pages, but got ${pages.length}`);
1717
}

modules/common/clover/server/src/custom-resource-loader.ts

+8-2
Original file line numberDiff line numberDiff line change
@@ -33,8 +33,14 @@ export class CustomResourceLoader extends ResourceLoader {
3333
return filePromise;
3434
}
3535

36-
const promise = promises.readFile(path).then((content) => {
37-
this.fileCache.set(path, content);
36+
const promise = promises.readFile(path, 'utf-8').then((content) => {
37+
if (path.includes('runtime.')) {
38+
// JSDOM doesn't support type=module, which will be added to lazy loaded scripts.
39+
// https://github.com/jsdom/jsdom/issues/2475
40+
content = content.replace(/\.type\s?=\s?['"]module["']/, '');
41+
}
42+
43+
this.fileCache.set(path, Buffer.from(content));
3844

3945
return content;
4046
}) as AbortablePromise<Buffer>;

0 commit comments

Comments
 (0)