Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add platformProxy option to configure getPlatformProxy #12011

Merged
merged 16 commits into from
Apr 8, 2024
Merged
Show file tree
Hide file tree
Changes from 9 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions .changeset/tough-dolphins-kick.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
---
"@sveltejs/adapter-cloudflare-workers": minor
"@sveltejs/adapter-cloudflare": minor
---

feat: support platform emulation configuration via the `platformProxy` adapter option
15 changes: 12 additions & 3 deletions documentation/docs/25-build-and-deploy/60-adapter-cloudflare.md
Original file line number Diff line number Diff line change
Expand Up @@ -28,15 +28,20 @@ export default {
routes: {
include: ['/*'],
exclude: ['<all>']
}
},
platformProxy: {
persist: './your-custom-path'
}
})
}
};
```

## Options

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`.
### routes

Allows you to customise the [`_routes.json`](https://developers.cloudflare.com/pages/platform/functions/routing/#create-a-_routesjson-file) file generated by `adapter-cloudflare`.

- `include` defines routes that will invoke a function, and defaults to `['/*']`
- `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:
Expand All @@ -47,6 +52,10 @@ The `routes` option allows you to customise the [`_routes.json`](https://develop

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', ...]`.

### platformProxy

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.

## Deployment

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

### Testing Locally

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.
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](#platformProxy) to change your preferences for the bindings.
eltigerchino marked this conversation as resolved.
Show resolved Hide resolved

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`.

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,11 +17,26 @@ import adapter from '@sveltejs/adapter-cloudflare-workers';

export default {
kit: {
adapter: adapter()
adapter: adapter({
config: '<your-wrangler-name>.toml',
platformProxy: {
persist: './your-custom-path'
}
})
}
};
```

## Options

### config

Path to your custom `wrangler.toml` config file.

### platformProxy

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.

## Basic Configuration

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:
Expand Down Expand Up @@ -63,19 +78,7 @@ wrangler deploy

## Custom config

If you would like to use a config file other than `wrangler.toml`, you can do like so:

```js
// @errors: 2307
/// file: svelte.config.js
import adapter from '@sveltejs/adapter-cloudflare-workers';

export default {
kit: {
adapter: adapter({ config: '<your-wrangler-name>.toml' })
}
};
```
If you would like to use a config file other than `wrangler.toml` you can specify so using the [config option](#config).
eltigerchino marked this conversation as resolved.
Show resolved Hide resolved

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`:

Expand Down Expand Up @@ -117,7 +120,7 @@ export {};

### Testing Locally

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.
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](#platformProxy) to change your preferences for the bindings.
eltigerchino marked this conversation as resolved.
Show resolved Hide resolved

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`.

Expand Down
6 changes: 6 additions & 0 deletions packages/adapter-cloudflare-workers/index.d.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,14 @@
import { Adapter } from '@sveltejs/kit';
import './ambient.js';
import { GetPlatformProxyOptions } from 'wrangler';

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

export interface AdapterOptions {
config?: string;
/**
* Config object passed to {@link https://developers.cloudflare.com/workers/wrangler/api/#getplatformproxy | getPlatformProxy}
* during development and preview.
*/
platformProxy?: GetPlatformProxyOptions;
}
4 changes: 2 additions & 2 deletions packages/adapter-cloudflare-workers/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ const compatible_node_modules = [
];

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

Expand Down Expand Up @@ -144,7 +144,7 @@ export default function ({ config = 'wrangler.toml' } = {}) {
},

async emulate() {
const proxy = await getPlatformProxy();
const proxy = await getPlatformProxy(platformProxy);
const platform = /** @type {App.Platform} */ ({
env: proxy.env,
context: proxy.ctx,
Expand Down
9 changes: 8 additions & 1 deletion packages/adapter-cloudflare/index.d.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { Adapter } from '@sveltejs/kit';
import './ambient.js';
import { GetPlatformProxyOptions } from 'wrangler';

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

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

/**
* Customize the automatically-generated `_routes.json` file
* Customize the automatically-generated `_routes.json` file.
* https://developers.cloudflare.com/pages/platform/functions/routing/#create-a-_routesjson-file
*/
routes?: {
Expand All @@ -42,6 +43,12 @@ export interface AdapterOptions {
*/
exclude?: string[];
};

/**
* Config object passed to {@link https://developers.cloudflare.com/workers/wrangler/api/#getplatformproxy | getPlatformProxy}
* during development and preview.
*/
platformProxy?: GetPlatformProxyOptions;
}

export interface RoutesJSONSpec {
Expand Down
2 changes: 1 addition & 1 deletion packages/adapter-cloudflare/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -127,7 +127,7 @@ export default function (options = {}) {
}
},
async emulate() {
const proxy = await getPlatformProxy();
const proxy = await getPlatformProxy(options.platformProxy);
const platform = /** @type {App.Platform} */ ({
env: proxy.env,
context: proxy.ctx,
Expand Down
Loading