Skip to content

Commit

Permalink
adding prerender support (#45)
Browse files Browse the repository at this point in the history
* prerender

* fix

* fix
  • Loading branch information
chenshay authored Jun 30, 2017
1 parent 5bad712 commit 782647e
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 6 deletions.
3 changes: 2 additions & 1 deletion example/platform.html
Original file line number Diff line number Diff line change
Expand Up @@ -118,7 +118,8 @@ <h1>Viewer</h1>
var ampDocUrl = ampDocEl.getAttribute('url');
var viewerHost = document.getElementById('viewerHost');
var referrer = 'http://localhost:8000/';
viewer = new Viewer(viewerHost, ampDocUrl, referrer);
var prerender = true; // Can be true, false, or undefined.
viewer = new Viewer(viewerHost, ampDocUrl, referrer, prerender);
viewer.setViewerShowAndHide(showViewer, hideViewer);
ampDocEl.addEventListener('click', openAmpDocInViewer);
}
Expand Down
17 changes: 12 additions & 5 deletions src/viewer.js
Original file line number Diff line number Diff line change
Expand Up @@ -28,9 +28,10 @@ class Viewer {
/**
* @param {!Element} hostElement the element to attatch the iframe to.
* @param {string} ampDocUrl the AMP Document url.
* @param {string} opt_referrer.
* @param {string} opt_referrer
* @param {boolean|undefined} opt_prerender
*/
constructor(hostElement, ampDocUrl, opt_referrer) {
constructor(hostElement, ampDocUrl, opt_referrer, opt_prerender) {
/** @private {ViewerMessaging} */
this.viewerMessaging_ = null;

Expand All @@ -43,6 +44,9 @@ class Viewer {
/** @private {string} */
this.referrer_ = opt_referrer;

/** @private {boolean|undefined} */
this.prerender_ = opt_prerender;

/** @private {?Element} */
this.iframe_ = null;

Expand Down Expand Up @@ -107,12 +111,15 @@ class Viewer {
createInitParams_() {
const parsedViewerUrl = parseUrl(window.location.href);

// TODO (chenshay): set more init params.
const initParams = {
origin: parsedViewerUrl.origin,
'origin': parsedViewerUrl.origin,
};

if (this.referrer_) initParams.referrer = this.referrer_;
if (this.referrer_) initParams['referrer'] = this.referrer_;
if (this.prerender_) {
initParams['visibilityState'] = 'prerender';
initParams['prerenderSize'] = 1;
}

return initParams;
}
Expand Down

0 comments on commit 782647e

Please sign in to comment.