Skip to content

Commit 678c0e2

Browse files
Implement SSR for amp-audio extension (#1324)
* Implement SSR for amp-audio extension * Make test input valid AMP * Update expected_output.html Co-authored-by: Sebastian Benz <[email protected]>
1 parent 4afdd75 commit 678c0e2

File tree

5 files changed

+41
-18
lines changed

5 files changed

+41
-18
lines changed

packages/optimizer/lib/transformers/ServerSideRendering.js

Lines changed: 18 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ const {
2020
remove,
2121
createElement,
2222
insertBefore,
23+
insertAfter,
2324
nextNode,
2425
firstChildByTag,
2526
} = require('../NodeUtils');
@@ -81,12 +82,9 @@ class ServerSideRendering {
8182
this.log_.debug('cannot remove boilerplate: amp-experiment');
8283
}
8384

84-
// amp-audio requires knowing the dimensions of the browser. Do not
85-
// remove the boilerplate or apply layout if amp-audio is present in the
86-
// document.
85+
// Server-side rendering of an amp-audio element.
8786
if (node.tagName === 'amp-audio') {
88-
canRemoveBoilerplate = false;
89-
this.log_.debug('cannot remove boilerplate: amp-audio');
87+
this.ssrAmpAudio(node);
9088
continue;
9189
}
9290

@@ -211,6 +209,21 @@ class ServerSideRendering {
211209
return nextNode(node);
212210
}
213211
}
212+
213+
ssrAmpAudio(node) {
214+
// Check if we already have a SSR-ed audio element.
215+
for (const child of node.children || []) {
216+
if (child.tagName === 'audio') {
217+
return;
218+
}
219+
}
220+
221+
const audio = createElement('audio', {
222+
controls: '',
223+
});
224+
225+
insertAfter(node, audio);
226+
}
214227
}
215228

216229
module.exports = ServerSideRendering;

packages/optimizer/spec/transformers/valid/ServerSideRendering/does_not_transform_amp_audio/expected_output.html

Lines changed: 0 additions & 7 deletions
This file was deleted.

packages/optimizer/spec/transformers/valid/ServerSideRendering/does_not_transform_amp_audio/input.html

Lines changed: 0 additions & 6 deletions
This file was deleted.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
<html i-amphtml-layout i-amphtml-no-boilerplate>
2+
<head><style amp-runtime></style>
3+
</head>
4+
<body>
5+
<amp-audio><audio controls></audio></amp-audio>
6+
<amp-audio src="https://example.com/audio.mp3" width="300"><audio controls></audio></amp-audio>
7+
<amp-audio src="https://example.com/audio.mp3" width="300">
8+
<div fallback>Your browser doesn’t support HTML5 audio</div>
9+
<audio controls></audio>
10+
</amp-audio>
11+
</body>
12+
</html>
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
<html >
2+
<head></head>
3+
<body>
4+
<amp-audio></amp-audio>
5+
<amp-audio src="https://example.com/audio.mp3" width="300"></amp-audio>
6+
<amp-audio src="https://example.com/audio.mp3" width="300">
7+
<div fallback>Your browser doesn’t support HTML5 audio</div>
8+
<audio controls></audio>
9+
</amp-audio>
10+
</body>
11+
</html>

0 commit comments

Comments
 (0)