Skip to content

Commit

Permalink
assume supportsAstroStaticSlot
Browse files Browse the repository at this point in the history
  • Loading branch information
lilnasy committed Mar 7, 2024
1 parent aedcf92 commit d2e1e2a
Show file tree
Hide file tree
Showing 3 changed files with 6 additions and 30 deletions.
4 changes: 1 addition & 3 deletions packages/astro/src/runtime/server/render/common.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@ import {
import { renderAllHeadContent } from './head.js';
import { isRenderInstruction } from './instruction.js';
import { type SlotString, isSlotString } from './slot.js';
import { restoreHydratableAstroSlot } from './component.js';

/**
* Possible chunk types to be written to the destination, and it'll
Expand Down Expand Up @@ -138,8 +137,7 @@ export function chunkToByteArray(
} else {
// `stringifyChunk` might return a HTMLString, call `.toString()` to really ensure it's a string
const stringified = stringifyChunk(result, chunk);
const restoredMarkup = restoreHydratableAstroSlot(stringified)
return encoder.encode(restoredMarkup.toString());
return encoder.encode(stringified.toString());
}
}

Expand Down
28 changes: 3 additions & 25 deletions packages/astro/src/runtime/server/render/component.ts
Original file line number Diff line number Diff line change
Expand Up @@ -66,34 +66,13 @@ function isHTMLComponent(Component: unknown) {
}

const ASTRO_SLOT_EXP = /<\/?astro-slot\b[^>]*>/g;
// same as ASTRO_SLOT_EXP, but only includes the tag name in the match while requiring that it still be surrounded by "<" and ">"
const ASTRO_SLOT_TAGNAME_EXP = /(?<=<\/?)astro-slot(?=\b[^>]*>)/g;
// used to match temporary tags that will be replaced back with astro-slot
const ASTRO_PRESERVED_SLOT_TAGNAME_EXP = /(?<=<\/?)astro-preserved-slot(?=\b[^>]*>)/g;

const ASTRO_STATIC_SLOT_EXP = /<\/?astro-static-slot\b[^>]*>/g;
// same as ASTRO_STATIC_SLOT_EXP, but only includes the tag name in the match while requiring that it still be surrounded by "<" and ">"
const ASTRO_STATIC_SLOT_TAGNAME_EXP = /(?<=<\/?)astro-static-slot(?=\b[^>]*>)/g;
// used to match temporary tags that will be replaced back with astro-static-slot
const ASTRO_PRESERVED_STATIC_SLOT_TAGNAME_EXP = /(?<=<\/?)astro-preserved-static-slot(?=\b[^>]*>)/g;

function removeStaticAstroSlot(html: string, supportsAstroStaticSlot: boolean) {
function removeStaticAstroSlot(html: string, supportsAstroStaticSlot = true) {
const exp = supportsAstroStaticSlot ? ASTRO_STATIC_SLOT_EXP : ASTRO_SLOT_EXP;
return html.replace(exp, '');
}

// An HTML string may be processed by the parent of a parent, and if it isn't to be hydrated, astro-slot tags will be incorrectly removed.
// We rename them here so that the regex doesn't match.
function preserveHydratableAstroSlot(html: string) {
return html.replaceAll(ASTRO_STATIC_SLOT_TAGNAME_EXP, 'astro-preserved-static-slot')
.replaceAll(ASTRO_SLOT_TAGNAME_EXP, 'astro-preserved-slot');
}

export function restoreHydratableAstroSlot(html: string) {
return html.replaceAll(ASTRO_PRESERVED_STATIC_SLOT_TAGNAME_EXP, 'astro-static-slot')
.replaceAll(ASTRO_PRESERVED_SLOT_TAGNAME_EXP, 'astro-slot');
}

async function renderFrameworkComponent(
result: SSRResult,
displayName: string,
Expand Down Expand Up @@ -332,7 +311,7 @@ If you're still stuck, please open an issue on GitHub or join us at https://astr
} else if (html && html.length > 0) {
destination.write(
markHTMLString(
removeStaticAstroSlot(html, renderer?.ssr?.supportsAstroStaticSlot ?? false)
removeStaticAstroSlot(html, renderer?.ssr?.supportsAstroStaticSlot)
)
);
}
Expand Down Expand Up @@ -414,8 +393,7 @@ If you're still stuck, please open an issue on GitHub or join us at https://astr
);
}
const renderedElement = renderElement('astro-island', island, false);
const hydratableMarkup = preserveHydratableAstroSlot(renderedElement);
destination.write(markHTMLString(hydratableMarkup));
destination.write(renderedElement);
},
};
}
Expand Down
4 changes: 2 additions & 2 deletions packages/astro/src/runtime/server/render/page.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import type { RouteData, SSRResult } from '../../../@types/astro.js';
import { type NonAstroPageComponent, renderComponentToString, restoreHydratableAstroSlot } from './component.js';
import { type NonAstroPageComponent, renderComponentToString } from './component.js';
import type { AstroComponentFactory } from './index.js';

import { isAstroComponentFactory } from './astro/index.js';
Expand Down Expand Up @@ -31,7 +31,7 @@ export async function renderPage(
route
);

const bytes = encoder.encode(restoreHydratableAstroSlot(str));
const bytes = encoder.encode(str);

return new Response(bytes, {
headers: new Headers([
Expand Down

0 comments on commit d2e1e2a

Please sign in to comment.