Skip to content

Commit

Permalink
Separate config for preview_urls (#7227)
Browse files Browse the repository at this point in the history
* feat(wrangler): create separate config for workers_dev_previews

The current Preview URLs (beta) feature routes to version preview urls based on the status of the `workers_dev` config value. Beta users have requested the ability to enable deployment urls and preview urls separately on `workers.dev`, and the new `previews_enabled` field of the enable-subdomain API will allow that. This change separates the `workers_dev` and `workers_dev_previews` behavior during `wrangler triggers deploy` and `wrangler versions upload`. `preview_urls` defaults to true, and does not implicitly depend on routes the way `workers_dev` does.

* Rename to preview_urls

* Update .changeset/famous-mayflies-knock.md

Co-authored-by: emily-shen <[email protected]>

---------

Co-authored-by: Daniel Walsh <[email protected]>
Co-authored-by: Daniel Walsh <[email protected]>
Co-authored-by: emily-shen <[email protected]>
  • Loading branch information
4 people authored Nov 26, 2024
1 parent 8246c45 commit 02a0e1e
Show file tree
Hide file tree
Showing 11 changed files with 224 additions and 85 deletions.
7 changes: 7 additions & 0 deletions .changeset/famous-mayflies-knock.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
---
"wrangler": minor
---

Add `preview_urls` toggle to `wrangler.toml`

The current Preview URLs (beta) feature routes to version preview urls based on the status of the `workers_dev` config value. Beta users have requested the ability to enable deployment urls and preview urls separately on `workers.dev`, and the new `previews_enabled` field of the enable-subdomain API will allow that. This change separates the `workers_dev` and `preview_urls` behavior during `wrangler triggers deploy` and `wrangler versions upload`. `preview_urls` defaults to true, and does not implicitly depend on routes the way `workers_dev` does.
1 change: 1 addition & 0 deletions packages/wrangler/src/__tests__/configuration.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -120,6 +120,7 @@ describe("normalizeAndValidateConfig()", () => {
wasm_modules: undefined,
data_blobs: undefined,
workers_dev: undefined,
preview_urls: true,
zone_id: undefined,
no_bundle: undefined,
minify: undefined,
Expand Down
94 changes: 92 additions & 2 deletions packages/wrangler/src/__tests__/deploy.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5135,7 +5135,7 @@ addEventListener('fetch', event => {});`
expect(std.err).toMatchInlineSnapshot(`""`);
});

it("should not try to enable the workers.dev domain if it has been enabled before", async () => {
it("should not try to enable the workers.dev domain if it has been enabled before and previews are in sync", async () => {
writeWranglerConfig({
workers_dev: true,
});
Expand All @@ -5157,6 +5157,53 @@ addEventListener('fetch', event => {});`
expect(std.err).toMatchInlineSnapshot(`""`);
});

it("should sync the workers.dev domain if it has been enabled before but previews should be enabled", async () => {
writeWranglerConfig({
workers_dev: true,
});
writeWorkerSource();
mockUploadWorkerRequest();
mockGetWorkerSubdomain({ enabled: true, previews_enabled: false });
mockSubDomainRequest();
mockUpdateWorkerSubdomain({ enabled: true, previews_enabled: true });

await runWrangler("deploy ./index");

expect(std.out).toMatchInlineSnapshot(`
"Total Upload: xx KiB / gzip: xx KiB
Worker Startup Time: 100 ms
Uploaded test-name (TIMINGS)
Deployed test-name triggers (TIMINGS)
https://test-name.test-sub-domain.workers.dev
Current Version ID: Galaxy-Class"
`);
expect(std.err).toMatchInlineSnapshot(`""`);
});

it("should sync the workers.dev domain if it has been enabled before but previews should be enabled", async () => {
writeWranglerConfig({
workers_dev: true,
preview_urls: false,
});
writeWorkerSource();
mockUploadWorkerRequest();
mockGetWorkerSubdomain({ enabled: true, previews_enabled: true });
mockSubDomainRequest();
mockUpdateWorkerSubdomain({ enabled: true, previews_enabled: false });

await runWrangler("deploy ./index");

expect(std.out).toMatchInlineSnapshot(`
"Total Upload: xx KiB / gzip: xx KiB
Worker Startup Time: 100 ms
Uploaded test-name (TIMINGS)
Deployed test-name triggers (TIMINGS)
https://test-name.test-sub-domain.workers.dev
Current Version ID: Galaxy-Class"
`);
expect(std.err).toMatchInlineSnapshot(`""`);
});

it("should disable the workers.dev domain if workers_dev is `false`", async () => {
writeWranglerConfig({
workers_dev: false,
Expand All @@ -5178,7 +5225,7 @@ addEventListener('fetch', event => {});`
expect(std.err).toMatchInlineSnapshot(`""`);
});

it("should not try to disable the workers.dev domain if it is not already available", async () => {
it("should not try to disable the workers.dev domain if it is not already available and previews are in sync", async () => {
writeWranglerConfig({
workers_dev: false,
});
Expand All @@ -5200,6 +5247,49 @@ addEventListener('fetch', event => {});`
expect(std.err).toMatchInlineSnapshot(`""`);
});

it("should sync the workers.dev domain if it is not available but previews should be enabled", async () => {
writeWranglerConfig({
workers_dev: false,
});
writeWorkerSource();
mockUploadWorkerRequest();
mockGetWorkerSubdomain({ enabled: false, previews_enabled: false });
mockUpdateWorkerSubdomain({ enabled: false, previews_enabled: true });

await runWrangler("deploy ./index");

expect(std.out).toMatchInlineSnapshot(`
"Total Upload: xx KiB / gzip: xx KiB
Worker Startup Time: 100 ms
Uploaded test-name (TIMINGS)
No deploy targets for test-name (TIMINGS)
Current Version ID: Galaxy-Class"
`);
expect(std.err).toMatchInlineSnapshot(`""`);
});

it("should sync the workers.dev domain if it is not available but previews should be disabled", async () => {
writeWranglerConfig({
workers_dev: false,
preview_urls: false,
});
writeWorkerSource();
mockUploadWorkerRequest();
mockGetWorkerSubdomain({ enabled: false, previews_enabled: true });
mockUpdateWorkerSubdomain({ enabled: false, previews_enabled: false });

await runWrangler("deploy ./index");

expect(std.out).toMatchInlineSnapshot(`
"Total Upload: xx KiB / gzip: xx KiB
Worker Startup Time: 100 ms
Uploaded test-name (TIMINGS)
No deploy targets for test-name (TIMINGS)
Current Version ID: Galaxy-Class"
`);
expect(std.err).toMatchInlineSnapshot(`""`);
});

it("should disable the workers.dev domain if workers_dev is undefined but overwritten to `false` in environment", async () => {
writeWranglerConfig({
env: {
Expand Down
14 changes: 11 additions & 3 deletions packages/wrangler/src/__tests__/helpers/mock-workers-subdomain.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,11 +36,13 @@ export function mockSubDomainRequest(
/** Create a mock handler to fetch the <script>.<user>.workers.dev subdomain status*/
export function mockGetWorkerSubdomain({
enabled,
previews_enabled = true,
env,
legacyEnv = false,
expectedScriptName = "test-name",
}: {
enabled: boolean;
previews_enabled?: boolean;
env?: string | undefined;
legacyEnv?: boolean | undefined;
expectedScriptName?: string;
Expand All @@ -61,7 +63,9 @@ export function mockGetWorkerSubdomain({
expect(params.envName).toEqual(env);
}

return HttpResponse.json(createFetchResult({ enabled }));
return HttpResponse.json(
createFetchResult({ enabled, previews_enabled })
);
},
{ once: true }
)
Expand All @@ -71,11 +75,13 @@ export function mockGetWorkerSubdomain({
/** Create a mock handler to toggle a <script>.<user>.workers.dev subdomain status */
export function mockUpdateWorkerSubdomain({
enabled,
previews_enabled = true,
env,
legacyEnv = false,
expectedScriptName = "test-name",
}: {
enabled: boolean;
previews_enabled?: boolean;
env?: string | undefined;
legacyEnv?: boolean | undefined;
expectedScriptName?: string;
Expand All @@ -96,8 +102,10 @@ export function mockUpdateWorkerSubdomain({
expect(params.envName).toEqual(env);
}
const body = await request.json();
expect(body).toEqual({ enabled });
return HttpResponse.json(createFetchResult({ enabled }));
expect(body).toEqual({ enabled, previews_enabled });
return HttpResponse.json(
createFetchResult({ enabled, previews_enabled })
);
},
{ once: true }
)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -110,7 +110,7 @@ describe("versions upload", () => {
test("should print preview url if version has preview", async () => {
mockGetScript();
mockUploadVersion(true);
mockGetWorkerSubdomain({ enabled: true });
mockGetWorkerSubdomain({ enabled: true, previews_enabled: true });
mockSubDomainRequest();

// Setup
Expand Down Expand Up @@ -140,10 +140,10 @@ describe("versions upload", () => {
`);
});

it("should not print preview url workers_dev is false", async () => {
it("should not print preview url when preview_urls is false", async () => {
mockGetScript();
mockUploadVersion(true);
mockGetWorkerSubdomain({ enabled: false });
mockGetWorkerSubdomain({ enabled: true, previews_enabled: false });

// Setup
writeWranglerConfig({
Expand Down
1 change: 1 addition & 0 deletions packages/wrangler/src/config/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -355,6 +355,7 @@ export const defaultWranglerConfig: Config = {
preserve_file_names: undefined,
base_dir: undefined,
workers_dev: undefined,
preview_urls: true,
route: undefined,
routes: undefined,
tsconfig: undefined,
Expand Down
9 changes: 9 additions & 0 deletions packages/wrangler/src/config/environment.ts
Original file line number Diff line number Diff line change
Expand Up @@ -148,6 +148,15 @@ interface EnvironmentInheritable {
*/
workers_dev: boolean | undefined;

/**
* Whether we use <version>-<name>.<subdomain>.workers.dev to
* serve Preview URLs for your Worker.
*
* @default `true`
* @inheritable
*/
preview_urls: boolean | undefined;

/**
* A list of routes that your Worker should be published to.
* Only one of `routes` or `route` is required.
Expand Down
10 changes: 10 additions & 0 deletions packages/wrangler/src/config/validation.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1122,6 +1122,15 @@ function normalizeAndValidateEnvironment(
undefined
);

const preview_urls = inheritable(
diagnostics,
topLevelEnv,
rawEnv,
"preview_urls",
isBoolean,
true
);

const { deprecatedUpload, ...build } = normalizeAndValidateBuild(
diagnostics,
rawEnv,
Expand Down Expand Up @@ -1257,6 +1266,7 @@ function normalizeAndValidateEnvironment(
placement: normalizeAndValidatePlacement(diagnostics, topLevelEnv, rawEnv),
build,
workers_dev,
preview_urls,
// Not inherited fields
vars: notInheritable(
diagnostics,
Expand Down
2 changes: 2 additions & 0 deletions packages/wrangler/src/init.ts
Original file line number Diff line number Diff line change
Expand Up @@ -140,6 +140,7 @@ type CustomDomainsRes = {

type WorkersDevRes = {
enabled: boolean;
previews_enabled: boolean;
};
type CronTriggersRes = {
schedules: [
Expand Down Expand Up @@ -940,6 +941,7 @@ async function getWorkerConfig(
name: workerName,
main: entrypoint,
workers_dev: workersDev.enabled,
preview_urls: workersDev.previews_enabled,
compatibility_date:
serviceEnvMetadata.script.compatibility_date ??
new Date().toISOString().substring(0, 10),
Expand Down
Loading

0 comments on commit 02a0e1e

Please sign in to comment.