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

Jsdom #71

Closed
wants to merge 3 commits into from
Closed
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
10 changes: 0 additions & 10 deletions .flowconfig

This file was deleted.

240 changes: 116 additions & 124 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,9 @@ const minimalcss = require("minimalcss");
const CleanCSS = require("clean-css");
const twentyKb = 20 * 1024;

const { serializeDocument } = require("jsdom");


const defaultOptions = {
//# stable configurations
port: 45678,
Expand Down Expand Up @@ -369,10 +372,11 @@ const fixWebpackChunksIssue = ({ page, basePath, http2PushManifest }) => {
);
};

const saveAsHtml = async ({ page, filePath, options, route }) => {
let content = await page.content();
const saveAsHtml = ({ page, filePath, options, route }) => {
let content = serializeDocument(page.document);

content = content.replace(/react-snap-onload/g, "onload");
const title = await page.title();
const title = page.document.title;
const minifiedContent = options.minifyHtml
? minify(content, options.minifyHtml)
: content;
Expand All @@ -389,19 +393,6 @@ const saveAsHtml = async ({ page, filePath, options, route }) => {
}
};

const saveAsPng = ({ page, filePath, options, route }) => {
mkdirp.sync(path.dirname(filePath));
let screenshotPath;
if (route.endsWith(".html")) {
screenshotPath = filePath.replace(/\.html$/, ".png");
} else if (route === "/") {
screenshotPath = `${filePath}/index.png`;
} else {
screenshotPath = `${filePath.replace(/\/$/, "")}.png`;
}
return page.screenshot({ path: screenshotPath });
};

const run = async userOptions => {
const options = defaults(userOptions);

Expand Down Expand Up @@ -453,123 +444,124 @@ const run = async userOptions => {
basePath,
publicPath,
beforeFetch: async ({ page, route }) => {
const {
preloadImages,
cacheAjaxRequests,
preconnectThirdParty
} = options;
if (
preloadImages ||
cacheAjaxRequests ||
preconnectThirdParty ||
http2PushManifest
) {
const {
ajaxCache: ac,
http2PushManifestItems: hpm
} = preloadResources({
page,
basePath,
preloadImages,
cacheAjaxRequests,
preconnectThirdParty,
http2PushManifest
});
ajaxCache[route] = ac;
http2PushManifestItems[route] = hpm;
}
// const {
// preloadImages,
// cacheAjaxRequests,
// preconnectThirdParty
// } = options;
// if (
// preloadImages ||
// cacheAjaxRequests ||
// preconnectThirdParty ||
// http2PushManifest
// ) {
// const {
// ajaxCache: ac,
// http2PushManifestItems: hpm
// } = preloadResources({
// page,
// basePath,
// preloadImages,
// cacheAjaxRequests,
// preconnectThirdParty,
// http2PushManifest
// });
// ajaxCache[route] = ac;
// http2PushManifestItems[route] = hpm;
// }
},
afterFetch: async ({ page, route, browser }) => {
const pageUrl = `${basePath}${route}`;
if (options.removeStyleTags) await removeStyleTags({ page });
if (options.removeScriptTags) await removeScriptTags({ page });
if (options.removeBlobs) await removeBlobs({ page });
if (options.inlineCss) {
const { cssFiles } = await inlineCss({
page,
pageUrl,
options,
basePath,
browser
});

if (http2PushManifest) {
const filesToRemove = cssFiles
.filter(file => file.startsWith(basePath))
.map(file => file.replace(basePath, ""));

for (let i = http2PushManifestItems[route].length - 1; i >= 0; i--) {
const x = http2PushManifestItems[route][i];
filesToRemove.forEach(fileToRemove => {
if (x.link.startsWith(fileToRemove)) {
http2PushManifestItems[route].splice(i, 1);
}
});
}
}
}
if (options.fixWebpackChunksIssue) {
await fixWebpackChunksIssue({
page,
basePath,
http2PushManifest
});
}
if (options.asyncScriptTags) await asyncScriptTags({ page });

await page.evaluate(ajaxCache => {
const snapEscape = (() => {
const UNSAFE_CHARS_REGEXP = /[<>\/\u2028\u2029]/g;
// Mapping of unsafe HTML and invalid JavaScript line terminator chars to their
// Unicode char counterparts which are safe to use in JavaScript strings.
const ESCAPED_CHARS = {
"<": "\\u003C",
">": "\\u003E",
"/": "\\u002F",
"\u2028": "\\u2028",
"\u2029": "\\u2029"
};
const escapeUnsafeChars = unsafeChar => ESCAPED_CHARS[unsafeChar];
return str => str.replace(UNSAFE_CHARS_REGEXP, escapeUnsafeChars);
})();
// TODO: as of now it only prevents XSS attack,
// but can stringify only basic data types
// e.g. Date, Set, Map, NaN won't be handled right
const snapStringify = obj => snapEscape(JSON.stringify(obj));

let scriptTagText = "";
if (ajaxCache && Object.keys(ajaxCache).length > 0) {
scriptTagText += `window.snapStore=${snapEscape(
JSON.stringify(ajaxCache)
)};`;
}
let state;
if (
window.snapSaveState &&
(state = window.snapSaveState()) &&
Object.keys(state).length !== 0
) {
scriptTagText += Object.keys(state)
.map(key => `window["${key}"]=${snapStringify(state[key])};`)
.join("");
}
if (scriptTagText !== "") {
const scriptTag = document.createElement("script");
scriptTag.type = "text/javascript";
scriptTag.text = scriptTagText;
const firstScript = Array.from(document.scripts)[0];
firstScript.parentNode.insertBefore(scriptTag, firstScript);
}
}, ajaxCache[route]);
delete ajaxCache[route];
// if (options.removeStyleTags) await removeStyleTags({ page });
// if (options.removeScriptTags) await removeScriptTags({ page });
// if (options.removeBlobs) await removeBlobs({ page });
// if (options.inlineCss) {
// const { cssFiles } = await inlineCss({
// page,
// pageUrl,
// options,
// basePath,
// browser
// });

// if (http2PushManifest) {
// const filesToRemove = cssFiles
// .filter(file => file.startsWith(basePath))
// .map(file => file.replace(basePath, ""));

// for (let i = http2PushManifestItems[route].length - 1; i >= 0; i--) {
// const x = http2PushManifestItems[route][i];
// filesToRemove.forEach(fileToRemove => {
// if (x.link.startsWith(fileToRemove)) {
// http2PushManifestItems[route].splice(i, 1);
// }
// });
// }
// }
// }
// if (options.fixWebpackChunksIssue) {
// await fixWebpackChunksIssue({
// page,
// basePath,
// http2PushManifest
// });
// }
// if (options.asyncScriptTags) await asyncScriptTags({ page });

// await page.evaluate(ajaxCache => {
// const snapEscape = (() => {
// const UNSAFE_CHARS_REGEXP = /[<>\/\u2028\u2029]/g;
// // Mapping of unsafe HTML and invalid JavaScript line terminator chars to their
// // Unicode char counterparts which are safe to use in JavaScript strings.
// const ESCAPED_CHARS = {
// "<": "\\u003C",
// ">": "\\u003E",
// "/": "\\u002F",
// "\u2028": "\\u2028",
// "\u2029": "\\u2029"
// };
// const escapeUnsafeChars = unsafeChar => ESCAPED_CHARS[unsafeChar];
// return str => str.replace(UNSAFE_CHARS_REGEXP, escapeUnsafeChars);
// })();
// // TODO: as of now it only prevents XSS attack,
// // but can stringify only basic data types
// // e.g. Date, Set, Map, NaN won't be handled right
// const snapStringify = obj => snapEscape(JSON.stringify(obj));

// let scriptTagText = "";
// if (ajaxCache && Object.keys(ajaxCache).length > 0) {
// scriptTagText += `window.snapStore=${snapEscape(
// JSON.stringify(ajaxCache)
// )};`;
// }
// let state;
// if (
// window.snapSaveState &&
// (state = window.snapSaveState()) &&
// Object.keys(state).length !== 0
// ) {
// scriptTagText += Object.keys(state)
// .map(key => `window["${key}"]=${snapStringify(state[key])};`)
// .join("");
// }
// if (scriptTagText !== "") {
// const scriptTag = document.createElement("script");
// scriptTag.type = "text/javascript";
// scriptTag.text = scriptTagText;
// const firstScript = Array.from(document.scripts)[0];
// firstScript.parentNode.insertBefore(scriptTag, firstScript);
// }
// }, ajaxCache[route]);
// delete ajaxCache[route];

const routePath = route.replace(publicPath, "");
const filePath = path.join(destinationDir, routePath);
if (options.saveAs === "html") {
await saveAsHtml({ page, filePath, options, route });
saveAsHtml({ page, filePath, options, route });
} else if (options.saveAs === "png") {
await saveAsPng({ page, filePath, options, route });
throw "png not supported";
}
return Promise.resolve();
},
onEnd: () => {
if (server) server.close();
Expand Down
15 changes: 10 additions & 5 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
"version": "1.4.0",
"description": "Pre-renders web app into static HTML",
"main": "index.js",
"browser": "src/render.js",
"author": "stereobooster",
"license": "MIT",
"repository": "stereobooster/react-snap",
Expand All @@ -15,9 +16,10 @@
"express-history-api-fallback": "^2.2.1",
"highland": "^2.11.1",
"html-minifier": "^3.5.5",
"jsdom": "^9.4.5",
"minimalcss": "^0.3.1",
"mkdirp": "^0.5.1",
"puppeteer": "^0.11.0",
"node-fetch": "^1.7.3",
"serve-static": "^1.13.1",
"sourcemapped-stacktrace-node": "^2.1.5"
},
Expand All @@ -29,18 +31,21 @@
"@types/node": "^8.0.46",
"@types/puppeteer": "^0.12.0",
"@types/serve-static": "^1.13.0",
"jsdoc": "^3.5.5",
"markdown-toc": "^1.2.0",
"prettier": "1.7.3",
"tsd-jsdoc": "^2.0.0-beta.3",
"react": "^16.2.0",
"react-dom": "^16.2.0",
"typescript": "^2.7.0-dev.20171026"
},
"peerDependencies": {
"react": "^16.0.0",
"react-dom": "^16.0.0"
},
"scripts": {
"toc": "yarn run markdown-toc -i Recipes.md",
"test": "echo \"Error: no test specified\" && exit 1",
"precommit": "prettier --write {*,src/*}.{js,json,css}",
"tsc": "tsc -p .",
"ts-definitions": "jsdoc -t node_modules/tsd-jsdoc -r src"
"tsc": "tsc -p ."
},
"bin": {
"react-snap": "./run.js"
Expand Down
Loading