Removed premature optimization (#5548)
This commit is contained in:
parent
9082a850ee
commit
8f3f67c96a
2 changed files with 35 additions and 37 deletions
5
.changeset/sour-otters-exercise.md
Normal file
5
.changeset/sour-otters-exercise.md
Normal file
|
@ -0,0 +1,5 @@
|
||||||
|
---
|
||||||
|
'astro': patch
|
||||||
|
---
|
||||||
|
|
||||||
|
Removed premature optimization
|
|
@ -56,7 +56,6 @@ function getFunctionExpression(slot: any) {
|
||||||
}
|
}
|
||||||
|
|
||||||
class Slots {
|
class Slots {
|
||||||
#cache = new Map<string, string>();
|
|
||||||
#result: SSRResult;
|
#result: SSRResult;
|
||||||
#slots: Record<string, any> | null;
|
#slots: Record<string, any> | null;
|
||||||
#loggingOpts: LogOptions;
|
#loggingOpts: LogOptions;
|
||||||
|
@ -90,42 +89,36 @@ class Slots {
|
||||||
}
|
}
|
||||||
|
|
||||||
public async render(name: string, args: any[] = []) {
|
public async render(name: string, args: any[] = []) {
|
||||||
const cacheable = args.length === 0;
|
if (!this.#slots || !this.has(name)) return;
|
||||||
if (!this.#slots) return undefined;
|
|
||||||
if (cacheable && this.#cache.has(name)) {
|
if (!Array.isArray(args)) {
|
||||||
const result = this.#cache.get(name);
|
warn(
|
||||||
return result;
|
this.#loggingOpts,
|
||||||
}
|
'Astro.slots.render',
|
||||||
if (!this.has(name)) return undefined;
|
`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"]])`
|
||||||
if (!cacheable) {
|
);
|
||||||
|
} else if (args.length > 0) {
|
||||||
const component = await this.#slots[name]();
|
const component = await this.#slots[name]();
|
||||||
if (!Array.isArray(args)) {
|
|
||||||
warn(
|
// Astro
|
||||||
this.#loggingOpts,
|
const expression = getFunctionExpression(component);
|
||||||
'Astro.slots.render',
|
if (expression) {
|
||||||
`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"]])`
|
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 content = await renderSlot(this.#result, this.#slots[name]);
|
||||||
const outHTML = stringifyChunk(this.#result, content);
|
const outHTML = stringifyChunk(this.#result, content);
|
||||||
|
|
||||||
if (cacheable) this.#cache.set(name, outHTML);
|
|
||||||
return outHTML;
|
return outHTML;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -201,13 +194,13 @@ export function createResult(args: CreateResultArgs): SSRResult {
|
||||||
url,
|
url,
|
||||||
redirect: args.ssr
|
redirect: args.ssr
|
||||||
? (path, status) => {
|
? (path, status) => {
|
||||||
return new Response(null, {
|
return new Response(null, {
|
||||||
status: status || 302,
|
status: status || 302,
|
||||||
headers: {
|
headers: {
|
||||||
Location: path,
|
Location: path,
|
||||||
},
|
},
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
: onlyAvailableInSSR('Astro.redirect'),
|
: onlyAvailableInSSR('Astro.redirect'),
|
||||||
resolve(path: string) {
|
resolve(path: string) {
|
||||||
let extra = `This can be replaced with a dynamic import like so: await import("${path}")`;
|
let extra = `This can be replaced with a dynamic import like so: await import("${path}")`;
|
||||||
|
|
Loading…
Reference in a new issue