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

bug/issue 1241 prerendering formatting breaks bundle mapping optimization #1244

Merged
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
26 changes: 19 additions & 7 deletions packages/cli/src/plugins/resource/plugin-standard-html.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ import { getUserScripts, getPageLayout, getAppLayout } from '../../lib/layout-ut
import { requestAsObject } from '../../lib/resource-utils.js';
import unified from 'unified';
import { Worker } from 'worker_threads';
import htmlparser from 'node-html-parser';

class StandardHtmlResource extends ResourceInterface {
constructor(compilation, options) {
Expand Down Expand Up @@ -200,12 +201,19 @@ class StandardHtmlResource extends ResourceInterface {
const pageResources = this.compilation.graph.find(page => page.outputPath === pathname || page.route === pathname).resources;
let body = await response.text();

const root = htmlparser.parse(body, {
script: true,
style: true
});

for (const pageResource of pageResources) {
const keyedResource = this.compilation.resources.get(pageResource);
const { contents, src, type, optimizationAttr, optimizedFileContents, optimizedFileName, rawAttributes } = keyedResource;
const { contents, src, type, optimizationAttr, optimizedFileContents, optimizedFileName } = keyedResource;

if (src) {
if (type === 'script') {
const tag = root.querySelectorAll('script').find(script => script.getAttribute('src') === src);

if (!optimizationAttr && optimization === 'default') {
const optimizedFilePath = `${basePath}/${optimizedFileName}`;

Expand All @@ -215,17 +223,19 @@ class StandardHtmlResource extends ResourceInterface {
<link rel="modulepreload" href="${optimizedFilePath}" as="script">
`);
} else if (optimizationAttr === 'inline' || optimization === 'inline') {
const isModule = rawAttributes.indexOf('type="module') >= 0 ? ' type="module"' : '';
const isModule = tag.rawAttrs.indexOf('type="module') >= 0 ? ' type="module"' : '';

body = body.replace(`<script ${rawAttributes}></script>`, `
body = body.replace(`<script ${tag.rawAttrs}></script>`, `
<script ${isModule}>
${optimizedFileContents.replace(/\.\//g, `${basePath}/`).replace(/\$/g, '$$$')}
</script>
`);
} else if (optimizationAttr === 'static' || optimization === 'static') {
body = body.replace(`<script ${rawAttributes}></script>`, '');
body = body.replace(`<script ${tag.rawAttrs}></script>`, '');
}
} else if (type === 'link') {
const tag = root.querySelectorAll('link').find(link => link.getAttribute('href') === src);

if (!optimizationAttr && (optimization !== 'none' && optimization !== 'inline')) {
const optimizedFilePath = `${basePath}/${optimizedFileName}`;

Expand All @@ -239,11 +249,11 @@ class StandardHtmlResource extends ResourceInterface {
// when pre-rendering, puppeteer normalizes everything to <link .../>
// but if not using pre-rendering, then it could come out as <link ...></link>
// not great, but best we can do for now until #742
body = body.replace(`<link ${rawAttributes}>`, `
body = body.replace(`<link ${tag.rawAttrs}>`, `
<style>
${optimizedFileContents}
</style>
`).replace(`<link ${rawAttributes}/>`, `
`).replace(`<link ${tag.rawAttrs}/>`, `
<style>
${optimizedFileContents}
</style>
Expand All @@ -252,8 +262,10 @@ class StandardHtmlResource extends ResourceInterface {
}
} else {
if (type === 'script') {
const tag = root.querySelectorAll('script').find(script => script.innerHTML === contents);

if (optimizationAttr === 'static' || optimization === 'static') {
body = body.replace(`<script ${rawAttributes}>${contents.replace(/\.\//g, '/').replace(/\$/g, '$$$')}</script>`, '');
body = body.replace(`<script ${tag.rawAttrs}>${contents.replace(/\.\//g, '/').replace(/\$/g, '$$$')}</script>`, '');
} else if (optimizationAttr === 'none') {
body = body.replace(contents, contents.replace(/\.\//g, `${basePath}/`).replace(/\$/g, '$$$'));
} else {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
// https://github.com/ProjectEvergreen/greenwood/issues/1241
// to ensure formatting does not break bundle optimization linking
export default {
prerender: true
};
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,23 @@
<html lang="en" prefix="og:http://ogp.me/ns#">

<head>
<script type="module" src="/components/header.js" data-gwd-opt="static"></script>
<script type="module" src="/components/footer.js" data-gwd-opt="inline"></script>
<link rel="stylesheet" href="/styles/theme.css" data-gwd-opt="inline"/>
<script
type="module"
src="../components/header.js"
data-gwd-opt="static"
></script>
<link
rel="stylesheet"
href="/styles/theme.css"
data-gwd-opt="inline"
/>
<script
data-gwd-opt="static"
type="module"
>
console.log('I should not be here');
</script>
</head>

<body>
Expand Down
Loading