Skip to content

Commit f80ba75

Browse files
feat: add platformProxy option to configure getPlatformProxy (#12011)
* feat: allow passing config to getPlatformProxy via platformProxy option * remove platformProxy default value * Update packages/adapter-cloudflare-workers/index.js * Update 60-adapter-cloudflare.md * Update 60-adapter-cloudflare.md * Update 70-adapter-cloudflare-workers.md * Update index.d.ts * Update packages/adapter-cloudflare-workers/index.d.ts * Update packages/adapter-cloudflare-workers/index.js * Update documentation/docs/25-build-and-deploy/70-adapter-cloudflare-workers.md * Update documentation/docs/25-build-and-deploy/60-adapter-cloudflare.md * Update documentation/docs/25-build-and-deploy/70-adapter-cloudflare-workers.md * Update documentation/docs/25-build-and-deploy/70-adapter-cloudflare-workers.md * Update 60-adapter-cloudflare.md * Update documentation/docs/25-build-and-deploy/60-adapter-cloudflare.md * Update documentation/docs/25-build-and-deploy/70-adapter-cloudflare-workers.md --------- Co-authored-by: Tee Ming <[email protected]>
1 parent 41b7ae9 commit f80ba75

File tree

7 files changed

+53
-22
lines changed

7 files changed

+53
-22
lines changed

.changeset/tough-dolphins-kick.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
---
2+
"@sveltejs/adapter-cloudflare-workers": minor
3+
"@sveltejs/adapter-cloudflare": minor
4+
---
5+
6+
feat: support platform emulation configuration via the `platformProxy` adapter option

documentation/docs/25-build-and-deploy/60-adapter-cloudflare.md

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -28,15 +28,20 @@ export default {
2828
routes: {
2929
include: ['/*'],
3030
exclude: ['<all>']
31-
}
31+
},
32+
platformProxy: {
33+
persist: './your-custom-path'
34+
}
3235
})
3336
}
3437
};
3538
```
3639

3740
## Options
3841

39-
The `routes` option allows you to customise the [`_routes.json`](https://developers.cloudflare.com/pages/platform/functions/routing/#create-a-_routesjson-file) file generated by `adapter-cloudflare`.
42+
### routes
43+
44+
Allows you to customise the [`_routes.json`](https://developers.cloudflare.com/pages/platform/functions/routing/#create-a-_routesjson-file) file generated by `adapter-cloudflare`.
4045

4146
- `include` defines routes that will invoke a function, and defaults to `['/*']`
4247
- `exclude` defines routes that will _not_ invoke a function — this is a faster and cheaper way to serve your app's static assets. This array can include the following special values:
@@ -47,6 +52,10 @@ The `routes` option allows you to customise the [`_routes.json`](https://develop
4752

4853
You can have up to 100 `include` and `exclude` rules combined. Generally you can omit the `routes` options, but if (for example) your `<prerendered>` paths exceed that limit, you may find it helpful to manually create an `exclude` list that includes `'/articles/*'` instead of the auto-generated `['/articles/foo', '/articles/bar', '/articles/baz', ...]`.
4954

55+
### platformProxy
56+
57+
Preferences for the emulated `platform.env` local bindings. See the [getPlatformProxy](https://developers.cloudflare.com/workers/wrangler/api/#syntax) Wrangler API documentation for a full list of options.
58+
5059
## Deployment
5160

5261
Please follow the [Get Started Guide](https://developers.cloudflare.com/pages/get-started) for Cloudflare Pages to begin.
@@ -90,7 +99,7 @@ export {};
9099

91100
### Testing Locally
92101

93-
Cloudflare Workers specific values in the `platform` property are emulated during dev and preview modes. Local [bindings](https://developers.cloudflare.com/workers/wrangler/configuration/#bindings) are created based on the configuration in your `wrangler.toml` file and are used to populate `platform.env` during development.
102+
Cloudflare Workers specific values in the `platform` property are emulated during dev and preview modes. Local [bindings](https://developers.cloudflare.com/workers/wrangler/configuration/#bindings) are created based on the configuration in your `wrangler.toml` file and are used to populate `platform.env` during development and preview. Use the adapter config [`platformProxy` option](#options-platformproxy) to change your preferences for the bindings.
94103

95104
For testing the build, you should use [wrangler](https://developers.cloudflare.com/workers/cli-wrangler) **version 3**. Once you have built your site, run `wrangler pages dev .svelte-kit/cloudflare`.
96105

documentation/docs/25-build-and-deploy/70-adapter-cloudflare-workers.md

Lines changed: 18 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -17,11 +17,26 @@ import adapter from '@sveltejs/adapter-cloudflare-workers';
1717

1818
export default {
1919
kit: {
20-
adapter: adapter()
20+
adapter: adapter({
21+
config: '<your-wrangler-name>.toml',
22+
platformProxy: {
23+
persist: './your-custom-path'
24+
}
25+
})
2126
}
2227
};
2328
```
2429

30+
## Options
31+
32+
### config
33+
34+
Path to your custom `wrangler.toml` config file.
35+
36+
### platformProxy
37+
38+
Preferences for the emulated `platform.env` local bindings. See the [getPlatformProxy](https://developers.cloudflare.com/workers/wrangler/api/#syntax) Wrangler API documentation for a full list of options.
39+
2540
## Basic Configuration
2641

2742
This adapter expects to find a [wrangler.toml](https://developers.cloudflare.com/workers/platform/sites/configuration) file in the project root. It should look something like this:
@@ -63,19 +78,7 @@ wrangler deploy
6378

6479
## Custom config
6580

66-
If you would like to use a config file other than `wrangler.toml`, you can do like so:
67-
68-
```js
69-
// @errors: 2307
70-
/// file: svelte.config.js
71-
import adapter from '@sveltejs/adapter-cloudflare-workers';
72-
73-
export default {
74-
kit: {
75-
adapter: adapter({ config: '<your-wrangler-name>.toml' })
76-
}
77-
};
78-
```
81+
If you would like to use a config file other than `wrangler.toml` you can specify so using the [`config` option](#options-config).
7982

8083
If you would like to enable [Node.js compatibility](https://developers.cloudflare.com/workers/runtime-apis/nodejs/#enable-nodejs-from-the-cloudflare-dashboard), you can add "nodejs_compat" flag to `wrangler.toml`:
8184

@@ -117,7 +120,7 @@ export {};
117120

118121
### Testing Locally
119122

120-
Cloudflare Workers specific values in the `platform` property are emulated during dev and preview modes. Local [bindings](https://developers.cloudflare.com/workers/wrangler/configuration/#bindings) are created based on the configuration in your `wrangler.toml` file and are used to populate `platform.env` during development.
123+
Cloudflare Workers specific values in the `platform` property are emulated during dev and preview modes. Local [bindings](https://developers.cloudflare.com/workers/wrangler/configuration/#bindings) are created based on the configuration in your `wrangler.toml` file and are used to populate `platform.env` during development and preview. Use the adapter config [`platformProxy` option](#options-platformproxy) to change your preferences for the bindings.
121124

122125
For testing the build, you should use [wrangler](https://developers.cloudflare.com/workers/cli-wrangler) **version 3**. Once you have built your site, run `wrangler dev`.
123126

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,14 @@
11
import { Adapter } from '@sveltejs/kit';
22
import './ambient.js';
3+
import { GetPlatformProxyOptions } from 'wrangler';
34

45
export default function plugin(options?: AdapterOptions): Adapter;
56

67
export interface AdapterOptions {
78
config?: string;
9+
/**
10+
* Config object passed to {@link https://developers.cloudflare.com/workers/wrangler/api/#getplatformproxy | getPlatformProxy}
11+
* during development and preview.
12+
*/
13+
platformProxy?: GetPlatformProxyOptions;
814
}

packages/adapter-cloudflare-workers/index.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ const compatible_node_modules = [
3232
];
3333

3434
/** @type {import('./index.js').default} */
35-
export default function ({ config = 'wrangler.toml' } = {}) {
35+
export default function ({ config = 'wrangler.toml', platformProxy = {} } = {}) {
3636
return {
3737
name: '@sveltejs/adapter-cloudflare-workers',
3838

@@ -145,7 +145,7 @@ export default function ({ config = 'wrangler.toml' } = {}) {
145145
},
146146

147147
async emulate() {
148-
const proxy = await getPlatformProxy();
148+
const proxy = await getPlatformProxy(platformProxy);
149149
const platform = /** @type {App.Platform} */ ({
150150
env: proxy.env,
151151
context: proxy.ctx,

packages/adapter-cloudflare/index.d.ts

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import { Adapter } from '@sveltejs/kit';
22
import './ambient.js';
3+
import { GetPlatformProxyOptions } from 'wrangler';
34

45
export default function plugin(options?: AdapterOptions): Adapter;
56

@@ -17,7 +18,7 @@ export interface AdapterOptions {
1718
fallback?: 'plaintext' | 'spa';
1819

1920
/**
20-
* Customize the automatically-generated `_routes.json` file
21+
* Customize the automatically-generated `_routes.json` file.
2122
* https://developers.cloudflare.com/pages/platform/functions/routing/#create-a-_routesjson-file
2223
*/
2324
routes?: {
@@ -42,6 +43,12 @@ export interface AdapterOptions {
4243
*/
4344
exclude?: string[];
4445
};
46+
47+
/**
48+
* Config object passed to {@link https://developers.cloudflare.com/workers/wrangler/api/#getplatformproxy | getPlatformProxy}
49+
* during development and preview.
50+
*/
51+
platformProxy?: GetPlatformProxyOptions;
4552
}
4653

4754
export interface RoutesJSONSpec {

packages/adapter-cloudflare/index.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -128,7 +128,7 @@ export default function (options = {}) {
128128
}
129129
},
130130
async emulate() {
131-
const proxy = await getPlatformProxy();
131+
const proxy = await getPlatformProxy(options.platformProxy);
132132
const platform = /** @type {App.Platform} */ ({
133133
env: proxy.env,
134134
context: proxy.ctx,

0 commit comments

Comments
 (0)