Removed premature optimization (#5548)

This commit is contained in:
Ido S 2022-12-06 22:39:23 +02:00 committed by GitHub
parent 9082a850ee
commit 8f3f67c96a
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 35 additions and 37 deletions

View file

@ -0,0 +1,5 @@
---
'astro': patch
---
Removed premature optimization

View file

@ -56,7 +56,6 @@ function getFunctionExpression(slot: any) {
}
class Slots {
#cache = new Map<string, string>();
#result: SSRResult;
#slots: Record<string, any> | null;
#loggingOpts: LogOptions;
@ -90,42 +89,36 @@ class Slots {
}
public async render(name: string, args: any[] = []) {
const cacheable = args.length === 0;
if (!this.#slots) return undefined;
if (cacheable && this.#cache.has(name)) {
const result = this.#cache.get(name);
return result;
}
if (!this.has(name)) return undefined;
if (!cacheable) {
if (!this.#slots || !this.has(name)) return;
if (!Array.isArray(args)) {
warn(
this.#loggingOpts,
'Astro.slots.render',
`Expected second parameter to be an array, received a ${typeof args}. If you're trying to pass an array as a single argument and getting unexpected results, make sure you're passing your array as a item of an array. Ex: Astro.slots.render('default', [["Hello", "World"]])`
);
} else if (args.length > 0) {
const component = await this.#slots[name]();
if (!Array.isArray(args)) {
warn(
this.#loggingOpts,
'Astro.slots.render',
`Expected second parameter to be an array, received a ${typeof args}. If you're trying to pass an array as a single argument and getting unexpected results, make sure you're passing your array as a item of an array. Ex: Astro.slots.render('default', [["Hello", "World"]])`
// Astro
const expression = getFunctionExpression(component);
if (expression) {
const slot = expression(...args);
return await renderSlot(this.#result, slot).then((res) =>
res != null ? String(res) : res
);
}
// JSX
if (typeof component === 'function') {
return await renderJSX(this.#result, component(...args)).then((res) =>
res != null ? String(res) : res
);
} else {
// Astro
const expression = getFunctionExpression(component);
if (expression) {
const slot = expression(...args);
return await renderSlot(this.#result, slot).then((res) =>
res != null ? String(res) : res
);
}
// JSX
if (typeof component === 'function') {
return await renderJSX(this.#result, component(...args)).then((res) =>
res != null ? String(res) : res
);
}
}
}
const content = await renderSlot(this.#result, this.#slots[name]);
const outHTML = stringifyChunk(this.#result, content);
if (cacheable) this.#cache.set(name, outHTML);
return outHTML;
}
}
@ -201,13 +194,13 @@ export function createResult(args: CreateResultArgs): SSRResult {
url,
redirect: args.ssr
? (path, status) => {
return new Response(null, {
status: status || 302,
headers: {
Location: path,
},
});
}
return new Response(null, {
status: status || 302,
headers: {
Location: path,
},
});
}
: onlyAvailableInSSR('Astro.redirect'),
resolve(path: string) {
let extra = `This can be replaced with a dynamic import like so: await import("${path}")`;