fix(docs-infra): do not use an Angular element in hard-coded `FETCH_ERROR` document (#27250)

The `FETCH_ERROR` document is used when we are unable to retrieve a
document (except for 404 errors), which includes when there is no
internet connection. Using the `<current-location>` element in the
document's template to show the path of the page we failed to retrieve
assumes that the element's bundle is available (e.g. cached by the SW)
or can be fetched from the server.

When none of these conditions is met, the `DocViewer` is unable to
prepare the document and fails, never showing the `FETCH_ERROR` page to
the user.

Furthermore, the path we are looking to retrieve via
`<current-location>` is essentially the document ID, which we already
have. Thus, loading and instantiating a whole component just for that is
overkill.

This commit addresses both issues by getting rid of the
`<current-location>` component and directly embedding the document ID
into the `FETCH_ERROR` content.

PR Close #27250
This commit is contained in:
George Kalpakas 2018-11-27 17:16:08 +02:00 committed by Igor Minar
parent dfdc2adbc6
commit 6abbaaed89
8 changed files with 7 additions and 72 deletions

View File

@ -1,7 +1,5 @@
<h1>Test Code Page</h1>
<p>Current location is <current-location></current-location></p>
<h2>&lt;code-tabs&gt;</h2>
<p>No linenums at code-tabs level</p>

View File

@ -2,8 +2,8 @@
"aio": {
"master": {
"uncompressed": {
"runtime": 3881,
"main": 502188,
"runtime": 3799,
"main": 501972,
"polyfills": 53926
}
}

View File

@ -1,35 +0,0 @@
import { ComponentFixture, TestBed } from '@angular/core/testing';
import { LocationService } from 'app/shared/location.service';
import { MockLocationService } from 'testing/location.service';
import { CurrentLocationComponent } from './current-location.component';
describe('CurrentLocationComponent', () => {
let element: HTMLElement;
let fixture: ComponentFixture<CurrentLocationComponent>;
let locationService: MockLocationService;
beforeEach(() => {
locationService = new MockLocationService('initial/url');
TestBed.configureTestingModule({
declarations: [ CurrentLocationComponent ],
providers: [
{ provide: LocationService, useValue: locationService }
]
});
fixture = TestBed.createComponent(CurrentLocationComponent);
element = fixture.nativeElement;
});
it('should render the current location', () => {
fixture.detectChanges();
expect(element.textContent).toEqual('initial/url');
locationService.urlSubject.next('next/url');
fixture.detectChanges();
expect(element.textContent).toEqual('next/url');
});
});

View File

@ -1,12 +0,0 @@
/* tslint:disable component-selector */
import { Component } from '@angular/core';
import { LocationService } from 'app/shared/location.service';
/** Renders the current location path. */
@Component({
selector: 'current-location',
template: '{{ location.currentPath | async }}'
})
export class CurrentLocationComponent {
constructor(public location: LocationService) { }
}

View File

@ -1,13 +0,0 @@
import { NgModule, Type } from '@angular/core';
import { CommonModule } from '@angular/common';
import { CurrentLocationComponent } from './current-location.component';
import { WithCustomElementComponent } from '../element-registry';
@NgModule({
imports: [ CommonModule ],
declarations: [ CurrentLocationComponent ],
entryComponents: [ CurrentLocationComponent ]
})
export class CurrentLocationModule implements WithCustomElementComponent {
customElementComponent: Type<any> = CurrentLocationComponent;
}

View File

@ -36,10 +36,6 @@ export const ELEMENT_MODULE_PATHS_AS_ROUTES = [
selector: 'code-tabs',
loadChildren: './code/code-tabs.module#CodeTabsModule'
},
{
selector: 'current-location',
loadChildren: './current-location/current-location.module#CurrentLocationModule'
},
{
selector: 'expandable-section',
loadChildren: './expandable-section/expandable-section.module#ExpandableSectionModule'

View File

@ -113,6 +113,7 @@ describe('DocumentService', () => {
httpMock.expectOne({}).flush(null, {status: 500, statusText: 'Server Error'});
expect(latestDocument.id).toEqual(FETCHING_ERROR_ID);
expect(latestDocument.contents).toContain('We are unable to retrieve the "initial/doc" page at this time.');
expect(logger.output.error).toEqual([
[jasmine.any(Error)]
]);

View File

@ -15,13 +15,13 @@ export const FETCHING_ERROR_ID = 'fetching-error';
export const CONTENT_URL_PREFIX = 'generated/';
export const DOC_CONTENT_URL_PREFIX = CONTENT_URL_PREFIX + 'docs/';
const FETCHING_ERROR_CONTENTS = `
const FETCHING_ERROR_CONTENTS = (path: string) => `
<div class="nf-container l-flex-wrap flex-center">
<div class="nf-icon material-icons">error_outline</div>
<div class="nf-response l-flex-wrap">
<h1 class="no-toc">Request for document failed.</h1>
<p>
We are unable to retrieve the "<current-location></current-location>" page at this time.
We are unable to retrieve the "${path}" page at this time.
Please check your connection and try again later.
</p>
</div>
@ -46,7 +46,7 @@ export class DocumentService {
private getDocument(url: string) {
const id = url || 'index';
this.logger.log('getting document', id);
if ( !this.cache.has(id)) {
if (!this.cache.has(id)) {
this.cache.set(id, this.fetchDocument(id));
}
return this.cache.get(id)!;
@ -93,7 +93,7 @@ export class DocumentService {
this.cache.delete(id);
return of({
id: FETCHING_ERROR_ID,
contents: FETCHING_ERROR_CONTENTS
contents: FETCHING_ERROR_CONTENTS(id),
});
}
}