summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorGravatar Ido S <50456099+ido-pluto@users.noreply.github.com> 2022-12-06 22:39:23 +0200
committerGravatar GitHub <noreply@github.com> 2022-12-06 15:39:23 -0500
commit8f3f67c96aee63be64de77f374293761ff73f6ce (patch)
tree143ca85c2d5b6b0c4709b26dbbf357a00558e2be
parent9082a850eef4ab0187fc3bfdd5a377f0c7040070 (diff)
downloadastro-8f3f67c96aee63be64de77f374293761ff73f6ce.tar.gz
astro-8f3f67c96aee63be64de77f374293761ff73f6ce.tar.zst
astro-8f3f67c96aee63be64de77f374293761ff73f6ce.zip
Removed premature optimization (#5548)
-rw-r--r--.changeset/sour-otters-exercise.md5
-rw-r--r--packages/astro/src/core/render/result.ts67
2 files changed, 35 insertions, 37 deletions
diff --git a/.changeset/sour-otters-exercise.md b/.changeset/sour-otters-exercise.md
new file mode 100644
index 000000000..45d1fa711
--- /dev/null
+++ b/.changeset/sour-otters-exercise.md
@@ -0,0 +1,5 @@
+---
+'astro': patch
+---
+
+Removed premature optimization
diff --git a/packages/astro/src/core/render/result.ts b/packages/astro/src/core/render/result.ts
index 90732c0fc..6b9bda52c 100644
--- a/packages/astro/src/core/render/result.ts
+++ b/packages/astro/src/core/render/result.ts
@@ -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}")`;