Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Implement SSR for amp-audio extension #1324

Merged
merged 3 commits into from
Jun 20, 2022
Merged
Show file tree
Hide file tree
Changes from 2 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
23 changes: 18 additions & 5 deletions packages/optimizer/lib/transformers/ServerSideRendering.js
Original file line number Diff line number Diff line change
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.

Original file line number Diff line number Diff line change
@@ -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="http://example.com/audio.mp3" width="300"><audio controls></audio></amp-audio>
<amp-audio src="http://example.com/audio.mp3" width="300">
<div fallback>Your browser doesn’t support HTML5 audio</div>
<audio controls src="http://example.com/audio.mp3"></audio>
</amp-audio>
</body>
</html>
Original file line number Diff line number Diff line change
@@ -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>