Skip to content
This repository was archived by the owner on Apr 4, 2025. It is now read-only.

feat(@angular/pwa): add content for when javascript is not available #853

Merged
merged 3 commits into from
May 21, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 22 additions & 7 deletions packages/angular/pwa/pwa/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -66,27 +66,42 @@ function updateIndexFile(options: PwaOptions): Rule {
const lines = content.split('\n');
let closingHeadTagLineIndex = -1;
let closingHeadTagLine = '';
lines.forEach((line, index) => {
let closingBodyTagLineIndex = -1;
let closingBodyTagLine = '';
lines.forEach((line: string, index: number) => {
if (/<\/head>/.test(line) && closingHeadTagLineIndex === -1) {
closingHeadTagLine = line;
closingHeadTagLineIndex = index;
}

if (/<\/body>/.test(line) && closingBodyTagLineIndex === -1) {
closingBodyTagLine = line;
closingBodyTagLineIndex = index;
}
});

const indent = getIndent(closingHeadTagLine) + ' ';
const itemsToAdd = [
const headTagIndent = getIndent(closingHeadTagLine) + ' ';
const itemsToAddToHead = [
'<link rel="manifest" href="manifest.json">',
'<meta name="theme-color" content="#1976d2">',
];

const textToInsert = itemsToAdd
.map(text => indent + text)
const textToInsertIntoHead = itemsToAddToHead
.map(text => headTagIndent + text)
.join('\n');

const bodyTagIndent = getIndent(closingBodyTagLine) + ' ';
const itemsToAddToBody
= '<noscript>Please enable JavaScript to continue using this application.</noscript>';

const textToInsertIntoBody = bodyTagIndent + itemsToAddToBody;

const updatedIndex = [
...lines.slice(0, closingHeadTagLineIndex),
textToInsert,
...lines.slice(closingHeadTagLineIndex),
textToInsertIntoHead,
...lines.slice(closingHeadTagLineIndex, closingBodyTagLineIndex),
textToInsertIntoBody,
...lines.slice(closingBodyTagLineIndex),
].join('\n');

host.overwrite(path, updatedIndex);
Expand Down
2 changes: 2 additions & 0 deletions packages/angular/pwa/pwa/index_spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,8 @@ describe('PWA Schematic', () => {

expect(content).toMatch(/<link rel="manifest" href="manifest.json">/);
expect(content).toMatch(/<meta name="theme-color" content="#1976d2">/);
expect(content)
.toMatch(/<noscript>Please enable JavaScript to continue using this application.<\/noscript>/);
});

it('should update the build and test assets configuration', () => {
Expand Down