Skip to content

Commit

Permalink
fix(@nguniversal/common): correctly handle lazy loaded routes in Clover
Browse files Browse the repository at this point in the history
JSDom doesn't support script with `type=module` therefore we need to strip out this from the runtime.js.

Closes #2433

(cherry picked from commit c38b739)
  • Loading branch information
alan-agius4 committed Nov 8, 2021
1 parent fee929e commit 71516ec
Show file tree
Hide file tree
Showing 9 changed files with 69 additions and 3 deletions.
1 change: 1 addition & 0 deletions integration/clover/src/app/app-routing.module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import { HomepageComponent } from './homepage.component';

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

Expand Down
11 changes: 11 additions & 0 deletions integration/clover/src/app/lazy/lazy-routing.module.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
import { NgModule } from '@angular/core';
import { RouterModule, Routes } from '@angular/router';
import { LazyComponent } from './lazy.component';

const routes: Routes = [{ path: '', component: LazyComponent }];

@NgModule({
imports: [RouterModule.forChild(routes)],
exports: [RouterModule],
})
export class LazyRoutingModule {}
Empty file.
1 change: 1 addition & 0 deletions integration/clover/src/app/lazy/lazy.component.html
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
<p>lazy works!</p>
24 changes: 24 additions & 0 deletions integration/clover/src/app/lazy/lazy.component.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
import { ComponentFixture, TestBed } from '@angular/core/testing';

import { LazyComponent } from './lazy.component';

describe('LazyComponent', () => {
let component: LazyComponent;
let fixture: ComponentFixture<LazyComponent>;

beforeEach(async () => {
await TestBed.configureTestingModule({
declarations: [LazyComponent],
}).compileComponents();
});

beforeEach(() => {
fixture = TestBed.createComponent(LazyComponent);
component = fixture.componentInstance;
fixture.detectChanges();
});

it('should create', () => {
expect(component).toBeTruthy();
});
});
12 changes: 12 additions & 0 deletions integration/clover/src/app/lazy/lazy.component.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
import { Component, OnInit } from '@angular/core';

@Component({
selector: 'app-lazy',
templateUrl: './lazy.component.html',
styleUrls: ['./lazy.component.css'],
})
export class LazyComponent implements OnInit {
constructor() {}

ngOnInit(): void {}
}
11 changes: 11 additions & 0 deletions integration/clover/src/app/lazy/lazy.module.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
import { NgModule } from '@angular/core';
import { CommonModule } from '@angular/common';

import { LazyRoutingModule } from './lazy-routing.module';
import { LazyComponent } from './lazy.component';

@NgModule({
declarations: [LazyComponent],
imports: [CommonModule, LazyRoutingModule],
})
export class LazyModule {}
2 changes: 1 addition & 1 deletion integration/clover/test.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ console.log({
pages,
});

const expectedNumberOfPages = 5;
const expectedNumberOfPages = 6;
if (pages.length !== expectedNumberOfPages) {
throw new Error(`Expected to have ${expectedNumberOfPages} index pages, but got ${pages.length}`);
}
Expand Down
10 changes: 8 additions & 2 deletions modules/common/clover/server/src/custom-resource-loader.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,8 +33,14 @@ export class CustomResourceLoader extends ResourceLoader {
return filePromise;
}

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

this.fileCache.set(path, Buffer.from(content));

return content;
}) as AbortablePromise<Buffer>;
Expand Down

0 comments on commit 71516ec

Please sign in to comment.