Skip to content

Commit

Permalink
move HTML entities preservation to standard html plugin
Browse files Browse the repository at this point in the history
  • Loading branch information
thescientist13 committed Jan 9, 2025
1 parent e6e033c commit d91b3c6
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 5 deletions.
5 changes: 1 addition & 4 deletions packages/cli/src/lifecycles/prerender.js
Original file line number Diff line number Diff line change
Expand Up @@ -95,15 +95,14 @@ async function preRenderCompilationWorker(compilation, workerPrerender) {
.filter(resource => resource.type === 'script')
.map(resource => resource.sourcePathURL.href);

// TODO we should probably do this in the HTML plugin
body = await new Promise((resolve, reject) => {
pool.runTask({
executeModuleUrl: workerPrerender.executeModuleUrl.href,
modulePath: null,
compilation: JSON.stringify(compilation),
page: JSON.stringify(page),
prerender: true,
htmlContents: body.replace(/</g, 'greenwood-custom-left-bracket'),
htmlContents: body,
scripts: JSON.stringify(scripts)
}, (err, result) => {
if (err) {
Expand All @@ -118,8 +117,6 @@ async function preRenderCompilationWorker(compilation, workerPrerender) {
body = body.replace('<!-- greenwood-ssr-start --><!-- greenwood-ssr-end -->', ssrContents);
}

body = body.replace(/greenwood-custom-left-bracket/g, '&lt;');

await createOutputDirectory(route, new URL(scratchUrl.href.replace('index.html', '')));
await fs.writeFile(scratchUrl, body);

Expand Down
21 changes: 20 additions & 1 deletion packages/cli/src/plugins/resource/plugin-standard-html.js
Original file line number Diff line number Diff line change
Expand Up @@ -159,13 +159,28 @@ class StandardHtmlResource extends ResourceInterface {
});
}

// https://github.com/ProjectEvergreen/greenwood/issues/1375
async shouldIntercept(url) {
const { pathname } = url;
const matchingRoute = this.compilation.graph.find((node) => node.route === pathname) || {};

return matchingRoute?.pageHref?.endsWith(this.extensions[1]) && this.compilation.config.prerender && process.env.__GWD_COMMAND__ === 'build'; // eslint-disable-line no-underscore-dangle
}

async intercept(url, request, response) {
const body = await response.text();

return new Response(body.replace(/&#x3C;/g, 'greenwood-custom-left-bracket'));
}

async shouldOptimize(url, response) {
return response.headers.get('Content-Type')?.indexOf(this.contentType) >= 0;
}

async optimize(url, response) {
const { optimization, basePath } = this.compilation.config;
const { optimization, basePath, prerender } = this.compilation.config;
const { pathname } = url;
const matchingRoute = this.compilation.graph.find((node) => node.route === pathname) || {};
const pageResources = this.compilation.graph.find(page => page.route === pathname).resources;
let body = await response.text();

Expand Down Expand Up @@ -245,6 +260,10 @@ class StandardHtmlResource extends ResourceInterface {
}
}

if (matchingRoute?.pageHref?.endsWith(this.extensions[1]) && prerender) {
body = body.replace(/greenwood-custom-left-bracket/g, '&lt;');
}

return new Response(body);
}
}
Expand Down

0 comments on commit d91b3c6

Please sign in to comment.