From e383bce7b68fae16487913dc811be4ff02ac4399 Mon Sep 17 00:00:00 2001 From: Owen Buckley Date: Fri, 27 Dec 2024 14:26:19 -0500 Subject: [PATCH] better handling for JSON.stringify in SSR page bundles --- packages/cli/src/lifecycles/bundle.js | 5 +++-- .../serve.default.ssr.spec.js | 19 +++++++++++++++++++ .../serve.default.ssr/src/pages/about.md | 11 +++++++++-- 3 files changed, 31 insertions(+), 4 deletions(-) diff --git a/packages/cli/src/lifecycles/bundle.js b/packages/cli/src/lifecycles/bundle.js index 551b3f89f..e1a7acefd 100644 --- a/packages/cli/src/lifecycles/bundle.js +++ b/packages/cli/src/lifecycles/bundle.js @@ -6,6 +6,7 @@ import { hashString } from '../lib/hashing-utils.js'; import { checkResourceExists, mergeResponse, normalizePathnameForWindows, trackResourcesForRoute } from '../lib/resource-utils.js'; import path from 'path'; import { rollup } from 'rollup'; +import { pruneGraph } from '../lib/content-utils.js'; async function interceptPage(url, request, plugins, body) { let response = new Response(body, { @@ -304,8 +305,8 @@ async function bundleSsrPages(compilation, optimizePlugins) { const moduleUrl = new URL('${relativeDepth}${pagesPathDiff}${pagePath.replace('./', '')}', import.meta.url); export async function handler(request) { - const compilation = JSON.parse('${JSON.stringify(compilation)}'); - const page = JSON.parse('${JSON.stringify(page)}'); + const compilation = JSON.parse(\`${JSON.stringify({ ...compilation, graph: pruneGraph(compilation.graph) }).replace(/\\"/g, '"e').replace(/\\n/g, '')}\`); + const page = JSON.parse(\`${JSON.stringify(pruneGraph([page])[0]).replace(/\\"/g, '"e').replace(/\\n/g, '')}\`); const data = await executeRouteModule({ moduleUrl, compilation, page, request }); let staticHtml = \`${staticHtml}\`; diff --git a/packages/cli/test/cases/serve.default.ssr/serve.default.ssr.spec.js b/packages/cli/test/cases/serve.default.ssr/serve.default.ssr.spec.js index 757204e89..b2e756545 100644 --- a/packages/cli/test/cases/serve.default.ssr/serve.default.ssr.spec.js +++ b/packages/cli/test/cases/serve.default.ssr/serve.default.ssr.spec.js @@ -100,6 +100,25 @@ describe('Serve Greenwood With: ', function() { }); }); + // https://github.com/ProjectEvergreen/greenwood/issues/1351 + describe('Serve command with HTML route response for the about page using various frontmatter syntaxes', function() { + let response; + let dom; + + before(async function() { + response = await fetch(`${hostname}/about/`); + const body = await response.clone().text(); + dom = new JSDOM(body); + }); + + it('should have the expected output for the page', function() { + const headings = dom.window.document.querySelectorAll('body > h1'); + + expect(headings.length).to.equal(1); + expect(headings[0].textContent).to.equal('Welcome to the about page!'); + }); + }); + describe('Serve command with HTML route response for artists page using "get" functions', function() { let response; let dom; diff --git a/packages/cli/test/cases/serve.default.ssr/src/pages/about.md b/packages/cli/test/cases/serve.default.ssr/src/pages/about.md index 1462b57fd..2a1e84a6f 100644 --- a/packages/cli/test/cases/serve.default.ssr/src/pages/about.md +++ b/packages/cli/test/cases/serve.default.ssr/src/pages/about.md @@ -1,3 +1,10 @@ -## About Page +--- +title: | + Greenwood's Super Cool About Page +imports: + - ../components/counter.js type="module" +sidebar: + order: 1 +--- -Lorum Ipsum. \ No newline at end of file +# Welcome to the about page! \ No newline at end of file