Skip to content

Commit

Permalink
feat: add platformProxy option to configure getPlatformProxy (sve…
Browse files Browse the repository at this point in the history
…ltejs#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]>
  • Loading branch information
vnphanquang and eltigerchino authored Apr 8, 2024
1 parent 41b7ae9 commit f80ba75
Show file tree
Hide file tree
Showing 7 changed files with 53 additions and 22 deletions.
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](#options-platformproxy) to change your preferences for the bindings.

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](#options-config).

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](#options-platformproxy) to change your preferences for the bindings.

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 @@ -145,7 +145,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 @@ -128,7 +128,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

0 comments on commit f80ba75

Please sign in to comment.