Skip to content

Commit 008dc75

Browse files
fix(astro): astro:config/server urls serialization (#14460)
1 parent 667bd64 commit 008dc75

File tree

4 files changed

+29
-2
lines changed

4 files changed

+29
-2
lines changed

.changeset/full-peas-do.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
'astro': patch
3+
---
4+
5+
Fixes a case where `astro:config/server` values typed as URLs would be serialized as strings

packages/astro/src/manifest/virtual-module.ts

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,7 @@ function serializeClientConfig(manifest: SSRManifest): string {
6868

6969
const output = [];
7070
for (const [key, value] of Object.entries(serClientConfig)) {
71-
output.push(`export const ${key} = ${JSON.stringify(value)};`);
71+
output.push(`export const ${key} = ${stringify(value)};`);
7272
}
7373
return output.join('\n') + '\n';
7474
}
@@ -102,7 +102,22 @@ function serializeServerConfig(manifest: SSRManifest): string {
102102
};
103103
const output = [];
104104
for (const [key, value] of Object.entries(serverConfig)) {
105-
output.push(`export const ${key} = ${JSON.stringify(value)};`);
105+
output.push(`export const ${key} = ${stringify(value)};`);
106106
}
107107
return output.join('\n') + '\n';
108108
}
109+
110+
function stringify(value: any): string {
111+
if (Array.isArray(value)) {
112+
return `[${value.map(e => stringify(e)).join(', ')}]`;
113+
}
114+
if (value instanceof URL) {
115+
return `new URL(${JSON.stringify(value)})`;
116+
}
117+
if (typeof value === 'object') {
118+
return `{\n${Object.entries(value)
119+
.map(([k, v]) => `${JSON.stringify(k)}: ${stringify(v)}`)
120+
.join(',\n')}\n}`;
121+
}
122+
return JSON.stringify(value);
123+
}

packages/astro/test/fixtures/astro-manifest/src/pages/server.astro

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ import { root, outDir, srcDir, build, cacheDir } from "astro:config/server";
1414
<p id="out-dir">{outDir}</p>
1515
<p id="src-dir">{srcDir}</p>
1616
<p id="root">{root}</p>
17+
<p id="root-url">{root instanceof URL}</p>
1718
<p id="cache-dir">{cacheDir}</p>
1819
<p id="build-client">{build.client}</p>
1920
<p id="build-server">{build.server}</p>

packages/astro/test/serializeManifest.test.js

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -132,6 +132,8 @@ describe('astro:config/server', () => {
132132
assert.ok($('#root').text().endsWith('/'));
133133
assert.ok($('#build-client').text().endsWith('/dist/client/'));
134134
assert.ok($('#build-server').text().endsWith('/dist/server/'));
135+
// URL
136+
assert.equal($('#root-url').text(), 'true');
135137
});
136138
});
137139

@@ -152,6 +154,8 @@ describe('astro:config/server', () => {
152154
assert.ok($('#root').text().endsWith('/'));
153155
assert.ok($('#build-client').text().endsWith('/dist/client/'));
154156
assert.ok($('#build-server').text().endsWith('/dist/server/'));
157+
// URL
158+
assert.equal($('#root-url').text(), 'true');
155159
});
156160
});
157161

@@ -179,6 +183,8 @@ describe('astro:config/server', () => {
179183
assert.ok($('#root').text().endsWith('/'));
180184
assert.ok($('#build-client').text().endsWith('/dist/client/'));
181185
assert.ok($('#build-server').text().endsWith('/dist/server/'));
186+
// URL
187+
assert.equal($('#root-url').text(), 'true');
182188
});
183189
});
184190
});

0 commit comments

Comments
 (0)