From 95a9ec4e0a434d6517b6d4922d5b463ca4026487 Mon Sep 17 00:00:00 2001 From: wuls Date: Thu, 13 Apr 2023 13:21:19 +0800 Subject: [PATCH 1/4] fix a bug when Fragment is as a slot --- .changeset/good-parrots-work.md | 5 +++++ packages/astro/src/core/render/result.ts | 9 +++++--- .../astro/src/runtime/server/render/slot.ts | 2 +- .../fixtures/set-html/components/Slot.astro | 2 ++ .../set-html/src/pages/children.astro | 22 +++++++++++++++++++ packages/astro/test/set-html.test.js | 11 ++++++++++ 6 files changed, 47 insertions(+), 4 deletions(-) create mode 100644 .changeset/good-parrots-work.md create mode 100644 packages/astro/test/fixtures/set-html/components/Slot.astro create mode 100644 packages/astro/test/fixtures/set-html/src/pages/children.astro diff --git a/.changeset/good-parrots-work.md b/.changeset/good-parrots-work.md new file mode 100644 index 000000000000..0a015732430c --- /dev/null +++ b/.changeset/good-parrots-work.md @@ -0,0 +1,5 @@ +--- +'astro': minor +--- + +fix a bug when Fragment is as a slot diff --git a/packages/astro/src/core/render/result.ts b/packages/astro/src/core/render/result.ts index 26ea22eee343..417e91332a32 100644 --- a/packages/astro/src/core/render/result.ts +++ b/packages/astro/src/core/render/result.ts @@ -18,6 +18,7 @@ import { renderJSX } from '../../runtime/server/jsx.js'; import { AstroCookies } from '../cookies/index.js'; import { AstroError, AstroErrorData } from '../errors/index.js'; import { warn, type LogOptions } from '../logger/core.js'; +import { isHTMLString } from '../../runtime/server/escape.js'; const clientAddressSymbol = Symbol.for('astro.clientAddress'); const responseSentSymbol = Symbol.for('astro.responseSent'); @@ -108,9 +109,11 @@ class Slots { // Astro const expression = getFunctionExpression(component); if (expression) { - const slot = () => expression(...args); - return await renderSlotToString(result, slot).then((res) => - res != null ? String(res) : res + const slot = async () => isHTMLString(await expression) ? expression : expression(...args) + return await renderSlotToString(result, slot).then((res) =>{ + return res != null ? String(res) : res + } + ); } // JSX diff --git a/packages/astro/src/runtime/server/render/slot.ts b/packages/astro/src/runtime/server/render/slot.ts index afc9560027e5..64461ac8d792 100644 --- a/packages/astro/src/runtime/server/render/slot.ts +++ b/packages/astro/src/runtime/server/render/slot.ts @@ -7,7 +7,7 @@ import { renderChild } from './any.js'; type RenderTemplateResult = ReturnType; export type ComponentSlots = Record; -export type ComponentSlotValue = (result: SSRResult) => RenderTemplateResult; +export type ComponentSlotValue = ((result: SSRResult) => RenderTemplateResult) | ((result: SSRResult) => Promise); const slotString = Symbol.for('astro:slot-string'); diff --git a/packages/astro/test/fixtures/set-html/components/Slot.astro b/packages/astro/test/fixtures/set-html/components/Slot.astro new file mode 100644 index 000000000000..a5b6f5a17543 --- /dev/null +++ b/packages/astro/test/fixtures/set-html/components/Slot.astro @@ -0,0 +1,2 @@ + + diff --git a/packages/astro/test/fixtures/set-html/src/pages/children.astro b/packages/astro/test/fixtures/set-html/src/pages/children.astro new file mode 100644 index 000000000000..360b8208aa58 --- /dev/null +++ b/packages/astro/test/fixtures/set-html/src/pages/children.astro @@ -0,0 +1,22 @@ +--- +import Slot from '../../components/Slot.astro'; +--- + + + +

Bug: Astro.slots.render() with arguments does not work with <Fragment> slot

+

Comment out working example and uncomment non working exmaples

+
+ + + + Test + + + + + + + diff --git a/packages/astro/test/set-html.test.js b/packages/astro/test/set-html.test.js index e439845131b6..dab80d48cfaa 100644 --- a/packages/astro/test/set-html.test.js +++ b/packages/astro/test/set-html.test.js @@ -35,6 +35,12 @@ describe('set:html', () => { expect($('#fetched-html')).to.have.a.lengthOf(1); expect($('#fetched-html').text()).to.equal('works'); }); + it('test Fragment when Fragment is as a slot', async () => { + let res = await fixture.fetch('/children'); + expect(res.status).to.equal(200); + let html = await res.text(); + expect(html).include('Test'); + }) }); describe('Build', () => { @@ -77,5 +83,10 @@ describe('set:html', () => { const $ = cheerio.load(html); expect($('#readable-inner')).to.have.a.lengthOf(1); }); + + it('test Fragment when Fragment is as a slot', async () => { + let res = await fixture.readFile('/children/index.html'); + expect(res).include('Test'); + }) }); }); From e760f0b87101cc43fa42339b39b76da051de65b1 Mon Sep 17 00:00:00 2001 From: wuls Date: Thu, 13 Apr 2023 14:20:38 +0800 Subject: [PATCH 2/4] update yaml --- pnpm-lock.yaml | 11 ++++++----- 1 file changed, 6 insertions(+), 5 deletions(-) diff --git a/pnpm-lock.yaml b/pnpm-lock.yaml index 5f29189d138b..c0fa7f3d8ac0 100644 --- a/pnpm-lock.yaml +++ b/pnpm-lock.yaml @@ -2451,12 +2451,13 @@ importers: version: link:../../.. packages/astro/test/fixtures/custom-assets-name: - specifiers: - '@astrojs/node': workspace:* - astro: workspace:* dependencies: - '@astrojs/node': link:../../../../integrations/node - astro: link:../../.. + '@astrojs/node': + specifier: workspace:* + version: link:../../../../integrations/node + astro: + specifier: workspace:* + version: link:../../.. packages/astro/test/fixtures/custom-elements: dependencies: From ba6b03bb5a8cd45a5d72528ade9309dda1fde519 Mon Sep 17 00:00:00 2001 From: wuls Date: Thu, 13 Apr 2023 14:51:06 +0800 Subject: [PATCH 3/4] fix ts errpr --- packages/astro/src/runtime/server/render/slot.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/packages/astro/src/runtime/server/render/slot.ts b/packages/astro/src/runtime/server/render/slot.ts index 26c96013f48c..1dfe2f5d962f 100644 --- a/packages/astro/src/runtime/server/render/slot.ts +++ b/packages/astro/src/runtime/server/render/slot.ts @@ -7,7 +7,7 @@ import { renderChild } from './any.js'; type RenderTemplateResult = ReturnType; export type ComponentSlots = Record; -export type ComponentSlotValue = ((result: SSRResult) => RenderTemplateResult) | ((result: SSRResult) => Promise); +export type ComponentSlotValue = (result: SSRResult) => RenderTemplateResult | Promise; const slotString = Symbol.for('astro:slot-string'); From 62b639f007a1f4872bfc9f41c21fc209b42d01fe Mon Sep 17 00:00:00 2001 From: wuls Date: Fri, 14 Apr 2023 09:22:40 +0800 Subject: [PATCH 4/4] fix astro name of md --- .changeset/good-parrots-work.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.changeset/good-parrots-work.md b/.changeset/good-parrots-work.md index 0a015732430c..7a58bac96a5a 100644 --- a/.changeset/good-parrots-work.md +++ b/.changeset/good-parrots-work.md @@ -1,5 +1,5 @@ --- -'astro': minor +'astro': patch --- fix a bug when Fragment is as a slot