Skip to content
This repository was archived by the owner on Feb 10, 2025. It is now read-only.

Commit 70e0054

Browse files
authored
Fix cloudflare ssr conditions setup (#465)
1 parent 2c36ee5 commit 70e0054

File tree

12 files changed

+291
-47
lines changed

12 files changed

+291
-47
lines changed

.changeset/shy-bananas-wait.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
'@astrojs/cloudflare': patch
3+
---
4+
5+
Fixes setting custom `workerd` and `worker` conditions for the ssr environment only

package.json

Lines changed: 0 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -29,15 +29,6 @@
2929
"packageManager": "pnpm@9.7.1",
3030
"pnpm": {
3131
"peerDependencyRules": {
32-
"ignoreMissing": [
33-
"rollup",
34-
"@babel/core",
35-
"@babel/plugin-transform-react-jsx",
36-
"vite",
37-
"react",
38-
"react-dom",
39-
"@types/react"
40-
],
4132
"allowAny": ["astro", "vite"]
4233
}
4334
},

packages/cloudflare/package.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@
3737
"magic-string": "^0.30.14",
3838
"miniflare": "^3.20241106.1",
3939
"tiny-glob": "^0.2.9",
40+
"vite": "^6.0.2",
4041
"wrangler": "^3.91.0"
4142
},
4243
"peerDependencies": {
@@ -50,8 +51,7 @@
5051
"execa": "^8.0.1",
5152
"fast-glob": "^3.3.2",
5253
"rollup": "^4.27.4",
53-
"strip-ansi": "^7.1.0",
54-
"vite": "6.0.1"
54+
"strip-ansi": "^7.1.0"
5555
},
5656
"publishConfig": {
5757
"provenance": true

packages/cloudflare/src/index.ts

Lines changed: 7 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ import {
1212
import { createRedirectsFromAstroRoutes } from '@astrojs/underscore-redirects';
1313
import astroWhen from '@inox-tools/astro-when';
1414
import { AstroError } from 'astro/errors';
15+
import { defaultServerConditions } from 'vite';
1516
import { type GetPlatformProxyOptions, getPlatformProxy } from 'wrangler';
1617
import {
1718
type CloudflareModulePluginExtra,
@@ -213,12 +214,13 @@ export default function createIntegration(args?: Options): AstroIntegration {
213214
}
214215
}
215216

216-
vite.resolve.conditions ||= [];
217-
// We need those conditions, previous these conditions where applied at the esbuild step which we removed
218-
// https://github.com/withastro/astro/pull/7092
219-
vite.resolve.conditions.push('workerd', 'worker');
220-
217+
// Support `workerd` and `worker` conditions for the ssr environment
218+
// (previously supported in esbuild instead: https://github.com/withastro/astro/pull/7092)
221219
vite.ssr ||= {};
220+
vite.ssr.resolve ||= {};
221+
vite.ssr.resolve.conditions ||= [...defaultServerConditions];
222+
vite.ssr.resolve.conditions.push('workerd', 'worker');
223+
222224
vite.ssr.target = 'webworker';
223225
vite.ssr.noExternal = true;
224226

@@ -250,21 +252,6 @@ export default function createIntegration(args?: Options): AstroIntegration {
250252
...vite.define,
251253
};
252254
}
253-
// we thought that vite config inside `if (target === 'server')` would not apply for client
254-
// but it seems like the same `vite` reference is used for both
255-
// so we need to reset the previous conflicting setting
256-
// in the future we should look into a more robust solution
257-
if (target === 'client') {
258-
vite.resolve ||= {};
259-
vite.resolve.conditions ||= [];
260-
vite.resolve.conditions = vite.resolve.conditions.filter(
261-
(c) => c !== 'workerd' && c !== 'worker'
262-
);
263-
264-
vite.build ||= {};
265-
vite.build.rollupOptions ||= {};
266-
vite.build.rollupOptions.output ||= {};
267-
}
268255
},
269256
'astro:build:done': async ({ pages, routes, dir, logger }) => {
270257
await cloudflareModulePlugin.afterBuildCompleted(_config);
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
import cloudflare from '@astrojs/cloudflare';
2+
import svelte from "@astrojs/svelte";
3+
import { defineConfig } from 'astro/config';
4+
5+
export default defineConfig({
6+
integrations: [svelte()],
7+
adapter: cloudflare(),
8+
output: 'server',
9+
});
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
{
2+
"name": "@test/astro-cloudflare-with-svelte",
3+
"version": "0.0.0",
4+
"private": true,
5+
"dependencies": {
6+
"@astrojs/cloudflare": "workspace:*",
7+
"@astrojs/svelte": "^7.0.1",
8+
"astro": "^5.0.0",
9+
"svelte": "^5.6.0"
10+
}
11+
}
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
<div class="svelte">Svelte Content</div>
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
---
2+
import Component from '../components/Component.svelte';
3+
---
4+
5+
<html>
6+
<head>
7+
<title>Testing</title>
8+
</head>
9+
<body>
10+
<h1>Testing</h1>
11+
<Component />
12+
</body>
13+
</html>

packages/cloudflare/test/with-solid-js.test.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ import { astroCli, wranglerCli } from './_test-utils.js';
66

77
const root = new URL('./fixtures/with-solid-js/', import.meta.url);
88

9-
describe('SolidJS', { skip: 'Figure out why the test fail.' }, () => {
9+
describe('SolidJS', () => {
1010
let wrangler;
1111
before(async () => {
1212
await astroCli(fileURLToPath(root), 'build');
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
import * as assert from 'node:assert/strict';
2+
import { after, before, describe, it } from 'node:test';
3+
import { fileURLToPath } from 'node:url';
4+
import * as cheerio from 'cheerio';
5+
import { astroCli, wranglerCli } from './_test-utils.js';
6+
7+
const root = new URL('./fixtures/with-svelte/', import.meta.url);
8+
9+
describe('Svelte', () => {
10+
let wrangler;
11+
before(async () => {
12+
await astroCli(fileURLToPath(root), 'build');
13+
14+
wrangler = wranglerCli(fileURLToPath(root));
15+
await new Promise((resolve) => {
16+
wrangler.stdout.on('data', (data) => {
17+
// console.log('[stdout]', data.toString());
18+
if (data.toString().includes('http://127.0.0.1:8788')) resolve();
19+
});
20+
wrangler.stderr.on('data', (data) => {
21+
// console.log('[stderr]', data.toString());
22+
});
23+
});
24+
});
25+
26+
after((done) => {
27+
wrangler.kill();
28+
});
29+
30+
it('renders the svelte component', async () => {
31+
const res = await fetch('http://127.0.0.1:8788/');
32+
assert.equal(res.status, 200);
33+
const html = await res.text();
34+
const $ = cheerio.load(html);
35+
assert.equal($('.svelte').text(), 'Svelte Content');
36+
});
37+
});

0 commit comments

Comments
 (0)