Skip to content

Commit

Permalink
Implement SSR for amp-audio extension (#1324)
Browse files Browse the repository at this point in the history
* Implement SSR for amp-audio extension

* Make test input valid AMP

* Update expected_output.html

Co-authored-by: Sebastian Benz <sebastian.benz@gmail.com>
  • Loading branch information
ediamin and sebastianbenz committed Jun 20, 2022
1 parent 4afdd75 commit 678c0e2
Show file tree
Hide file tree
Showing 5 changed files with 41 additions and 18 deletions.
23 changes: 18 additions & 5 deletions packages/optimizer/lib/transformers/ServerSideRendering.js
Expand Up @@ -20,6 +20,7 @@ const {
remove,
createElement,
insertBefore,
insertAfter,
nextNode,
firstChildByTag,
} = require('../NodeUtils');
Expand Down Expand Up @@ -81,12 +82,9 @@ class ServerSideRendering {
this.log_.debug('cannot remove boilerplate: amp-experiment');
}

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

Expand Down Expand Up @@ -211,6 +209,21 @@ class ServerSideRendering {
return nextNode(node);
}
}

ssrAmpAudio(node) {
// Check if we already have a SSR-ed audio element.
for (const child of node.children || []) {
if (child.tagName === 'audio') {
return;
}
}

const audio = createElement('audio', {
controls: '',
});

insertAfter(node, audio);
}
}

module.exports = ServerSideRendering;

This file was deleted.

This file was deleted.

@@ -0,0 +1,12 @@
<html i-amphtml-layout i-amphtml-no-boilerplate>
<head><style amp-runtime></style>
</head>
<body>
<amp-audio><audio controls></audio></amp-audio>
<amp-audio src="https://example.com/audio.mp3" width="300"><audio controls></audio></amp-audio>
<amp-audio src="https://example.com/audio.mp3" width="300">
<div fallback>Your browser doesn’t support HTML5 audio</div>
<audio controls></audio>
</amp-audio>
</body>
</html>
@@ -0,0 +1,11 @@
<html >
<head></head>
<body>
<amp-audio></amp-audio>
<amp-audio src="https://example.com/audio.mp3" width="300"></amp-audio>
<amp-audio src="https://example.com/audio.mp3" width="300">
<div fallback>Your browser doesn’t support HTML5 audio</div>
<audio controls></audio>
</amp-audio>
</body>
</html>

0 comments on commit 678c0e2

Please sign in to comment.