0
Fork 0
mirror of https://github.com/withastro/astro.git synced 2024-12-16 21:46:22 -05:00

Optimize minor hot code (#10821)

Co-authored-by: ematipico <ematipico@users.noreply.github.com>
This commit is contained in:
Bjorn Lu 2024-04-25 20:14:59 +08:00 committed by GitHub
parent 9876163406
commit a10ed2a4cf
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
2 changed files with 20 additions and 13 deletions

View file

@ -49,23 +49,25 @@ export class AstroComponentInstance {
async init(result: SSRResult) { async init(result: SSRResult) {
if (this.returnValue !== undefined) return this.returnValue; if (this.returnValue !== undefined) return this.returnValue;
this.returnValue = this.factory(result, this.props, this.slotValues); this.returnValue = this.factory(result, this.props, this.slotValues);
// Save the resolved value after promise is resolved for optimization
if (isPromise(this.returnValue)) {
this.returnValue
.then((resolved) => {
this.returnValue = resolved;
})
.catch(() => {
// Ignore errors and appease unhandledrejection error
});
}
return this.returnValue; return this.returnValue;
} }
async render(destination: RenderDestination) { async render(destination: RenderDestination) {
if (this.returnValue === undefined) { const returnValue = await this.init(this.result);
await this.init(this.result); if (isHeadAndContent(returnValue)) {
} await returnValue.content.render(destination);
let value: Promise<AstroFactoryReturnValue> | AstroFactoryReturnValue | undefined =
this.returnValue;
if (isPromise(value)) {
value = await value;
}
if (isHeadAndContent(value)) {
await value.content.render(destination);
} else { } else {
await renderChild(destination, value); await renderChild(destination, returnValue);
} }
} }
} }

View file

@ -12,6 +12,9 @@ const htmlEnumAttributes = /^(?:contenteditable|draggable|spellcheck|value)$/i;
// Note: SVG is case-sensitive! // Note: SVG is case-sensitive!
const svgEnumAttributes = /^(?:autoReverse|externalResourcesRequired|focusable|preserveAlpha)$/i; const svgEnumAttributes = /^(?:autoReverse|externalResourcesRequired|focusable|preserveAlpha)$/i;
const AMPERSAND_REGEX = /&/g;
const DOUBLE_QUOTE_REGEX = /"/g;
const STATIC_DIRECTIVES = new Set(['set:html', 'set:text']); const STATIC_DIRECTIVES = new Set(['set:html', 'set:text']);
// converts (most) arbitrary strings to valid JS identifiers // converts (most) arbitrary strings to valid JS identifiers
@ -22,7 +25,9 @@ const toIdent = (k: string) =>
}); });
export const toAttributeString = (value: any, shouldEscape = true) => export const toAttributeString = (value: any, shouldEscape = true) =>
shouldEscape ? String(value).replace(/&/g, '&#38;').replace(/"/g, '&#34;') : value; shouldEscape
? String(value).replace(AMPERSAND_REGEX, '&#38;').replace(DOUBLE_QUOTE_REGEX, '&#34;')
: value;
const kebab = (k: string) => const kebab = (k: string) =>
k.toLowerCase() === k ? k : k.replace(/[A-Z]/g, (match) => `-${match.toLowerCase()}`); k.toLowerCase() === k ? k : k.replace(/[A-Z]/g, (match) => `-${match.toLowerCase()}`);