Skip to content

Commit 62777fe

Browse files
committed
Merge branch 'main' into elliott/init-tracing
2 parents 0c3aad6 + 683c12d commit 62777fe

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

66 files changed

+6105
-468
lines changed

.changeset/happy-beers-remain.md

Lines changed: 0 additions & 5 deletions
This file was deleted.

.changeset/shiny-groups-relax.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
'@sveltejs/kit': patch
3+
---
4+
5+
fix: correctly set URL when navigating during an ongoing navigation

.changeset/tricky-pants-kick.md

Lines changed: 0 additions & 5 deletions
This file was deleted.

.changeset/warm-rats-say.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
'@sveltejs/adapter-cloudflare': patch
3+
---
4+
5+
fix: support assets-only workers in wrangler validation

eslint.config.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@ export default [
3333
},
3434
ignores: [
3535
'packages/adapter-cloudflare/test/apps/**/*',
36+
'packages/adapter-netlify/test/apps/**/*',
3637
'packages/adapter-node/rollup.config.js',
3738
'packages/adapter-node/tests/smoke.spec_disabled.js',
3839
'packages/adapter-static/test/apps/**/*',

package.json

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,10 +37,13 @@
3737
},
3838
"pnpm": {
3939
"onlyBuiltDependencies": [
40+
"@parcel/watcher",
4041
"esbuild",
42+
"netlify-cli",
4143
"rolldown",
4244
"sharp",
4345
"svelte-preprocess",
46+
"unix-dgram",
4447
"workerd"
4548
]
4649
}

packages/adapter-cloudflare/CHANGELOG.md

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,23 @@
11
# @sveltejs/adapter-cloudflare
22

3+
## 7.1.0
4+
### Minor Changes
5+
6+
7+
- feat: add support for `read` imported from `$app/server` ([#13859](https://github.com/sveltejs/kit/pull/13859))
8+
9+
10+
### Patch Changes
11+
12+
13+
- fix: include missing utils file ([#14009](https://github.com/sveltejs/kit/pull/14009))
14+
15+
16+
- fix: correctly warn users when assets key is missing ([#13844](https://github.com/sveltejs/kit/pull/13844))
17+
18+
- Updated dependencies [[`e5ce8bb`](https://github.com/sveltejs/kit/commit/e5ce8bb42ea020b88bd0a4ff18dc600745657541), [`cf88369`](https://github.com/sveltejs/kit/commit/cf883692fa0e163cff6b1a2f9b17a568af14124d)]:
19+
- @sveltejs/kit@2.25.0
20+
321
## 7.0.5
422
### Patch Changes
523

packages/adapter-cloudflare/index.js

Lines changed: 23 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,17 @@
1+
import { VERSION } from '@sveltejs/kit';
12
import { copyFileSync, existsSync, writeFileSync } from 'node:fs';
23
import path from 'node:path';
34
import { fileURLToPath } from 'node:url';
4-
import { is_building_for_cloudflare_pages } from './utils.js';
5+
import { is_building_for_cloudflare_pages, validate_worker_settings } from './utils.js';
56
import { getPlatformProxy, unstable_readConfig } from 'wrangler';
67

8+
const name = '@sveltejs/adapter-cloudflare';
9+
const [kit_major, kit_minor] = VERSION.split('.');
10+
711
/** @type {import('./index.js').default} */
812
export default function (options = {}) {
913
return {
10-
name: '@sveltejs/adapter-cloudflare',
14+
name,
1115
async adapt(builder) {
1216
if (existsSync('_routes.json')) {
1317
throw new Error(
@@ -27,7 +31,7 @@ export default function (options = {}) {
2731
);
2832
}
2933

30-
const wrangler_config = validate_config(options.config);
34+
const wrangler_config = validate_wrangler_config(options.config);
3135
const building_for_cloudflare_pages = is_building_for_cloudflare_pages(wrangler_config);
3236

3337
let dest = builder.getBuildDirectory('cloudflare');
@@ -162,6 +166,18 @@ export default function (options = {}) {
162166
return prerender ? emulated.prerender_platform : emulated.platform;
163167
}
164168
};
169+
},
170+
supports: {
171+
read: ({ route }) => {
172+
// TODO bump peer dep in next adapter major to simplify this
173+
if (kit_major === '2' && kit_minor < '25') {
174+
throw new Error(
175+
`${name}: Cannot use \`read\` from \`$app/server\` in route \`${route.id}\` when using SvelteKit < 2.25.0`
176+
);
177+
}
178+
179+
return true;
180+
}
165181
}
166182
};
167183
}
@@ -270,33 +286,12 @@ _redirects
270286
* @param {string} config_file
271287
* @returns {import('wrangler').Unstable_Config}
272288
*/
273-
function validate_config(config_file = undefined) {
289+
function validate_wrangler_config(config_file = undefined) {
274290
const wrangler_config = unstable_readConfig({ config: config_file });
275291

276-
// we don't support workers sites
277-
if (wrangler_config.site) {
278-
throw new Error(
279-
`You must remove all \`site\` keys in ${wrangler_config.configPath}. Consult https://svelte.dev/docs/kit/adapter-cloudflare#Migrating-from-Workers-Sites-to-Workers-Static-Assets`
280-
);
281-
}
282-
283-
if (is_building_for_cloudflare_pages(wrangler_config)) {
284-
return wrangler_config;
285-
}
286-
287-
// probably deploying to Cloudflare Workers
288-
if (wrangler_config.main || wrangler_config.assets) {
289-
if (!wrangler_config.assets?.directory) {
290-
throw new Error(
291-
`You must specify the \`assets.directory\` key in ${wrangler_config.configPath}. Consult https://developers.cloudflare.com/workers/static-assets/binding/#directory`
292-
);
293-
}
294-
295-
if (!wrangler_config.assets?.binding) {
296-
throw new Error(
297-
`You must specify the \`assets.binding\` key in ${wrangler_config.configPath}. Consult https://developers.cloudflare.com/workers/static-assets/binding/#binding`
298-
);
299-
}
292+
if (!is_building_for_cloudflare_pages(wrangler_config)) {
293+
// probably deploying to Cloudflare Workers
294+
validate_worker_settings(wrangler_config);
300295
}
301296

302297
return wrangler_config;

packages/adapter-cloudflare/package.json

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "@sveltejs/adapter-cloudflare",
3-
"version": "7.0.5",
3+
"version": "7.1.0",
44
"description": "Adapter for building SvelteKit applications on Cloudflare Pages with Workers integration",
55
"keywords": [
66
"adapter",
@@ -29,18 +29,19 @@
2929
"files": [
3030
"files",
3131
"index.js",
32+
"utils.js",
3233
"index.d.ts",
3334
"ambient.d.ts"
3435
],
3536
"scripts": {
36-
"build": "esbuild src/worker.js --bundle --outfile=files/worker.js --external:SERVER --external:MANIFEST --format=esm",
37+
"build": "esbuild src/worker.js --bundle --outfile=files/worker.js --external:SERVER --external:MANIFEST --external:cloudflare:workers --format=esm",
3738
"lint": "prettier --check .",
3839
"format": "pnpm lint --write",
3940
"check": "tsc --skipLibCheck",
40-
"prepublishOnly": "pnpm build",
4141
"test:unit": "vitest run",
4242
"test:e2e": "pnpm build && pnpm -r --workspace-concurrency 1 --filter=\"./test/**\" test",
43-
"test": "pnpm test:unit && pnpm test:e2e"
43+
"test": "pnpm test:unit && pnpm test:e2e",
44+
"prepublishOnly": "pnpm build"
4445
},
4546
"dependencies": {
4647
"@cloudflare/workers-types": "^4.20250507.0",

packages/adapter-cloudflare/src/worker.js

Lines changed: 26 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import { Server } from 'SERVER';
22
import { manifest, prerendered, base_path } from 'MANIFEST';
3+
import { env } from 'cloudflare:workers';
34
import * as Cache from 'worktop/cfw.cache';
45

56
const server = new Server(manifest);
@@ -9,6 +10,27 @@ const app_path = `/${manifest.appPath}`;
910
const immutable = `${app_path}/immutable/`;
1011
const version_file = `${app_path}/version.json`;
1112

13+
/**
14+
* We don't know the origin until we receive a request, but
15+
* that's guaranteed to happen before we call `read`
16+
* @type {string}
17+
*/
18+
let origin;
19+
20+
const initialized = server.init({
21+
// @ts-expect-error env contains environment variables and bindings
22+
env,
23+
read: async (file) => {
24+
const response = await /** @type {{ ASSETS: { fetch: typeof fetch } }} */ (env).ASSETS.fetch(
25+
`${origin}/${file}`
26+
);
27+
if (!response.ok) {
28+
throw new Error(`Failed to fetch ${file}: ${response.status} ${response.statusText}`);
29+
}
30+
return response.body;
31+
}
32+
});
33+
1234
export default {
1335
/**
1436
* @param {Request} req
@@ -17,10 +39,10 @@ export default {
1739
* @returns {Promise<Response>}
1840
*/
1941
async fetch(req, env, ctx) {
20-
await server.init({
21-
// @ts-expect-error env contains environment variables and bindings
22-
env
23-
});
42+
if (!origin) {
43+
origin = new URL(req.url).origin;
44+
await initialized;
45+
}
2446

2547
// skip cache if "cache-control: no-cache" in request
2648
let pragma = req.headers.get('cache-control') || '';

0 commit comments

Comments
 (0)