Skip to content

Commit

Permalink
feat(wrangler): create separate config for workers_dev_previews
Browse files Browse the repository at this point in the history
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`. `wrangler_dev_previews` defaults to true, and does not implicitly depend on routes the way `wrangler_dev` does.
  • Loading branch information
taylorlee committed Nov 11, 2024
1 parent 9ae9c6c commit 40665e2
Show file tree
Hide file tree
Showing 11 changed files with 224 additions and 75 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 `workers_dev_previews` 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 `workers_dev_previews` behavior during `wrangler triggers deploy` and `wrangler versions upload`. `wrangler_dev_previews` defaults to true, and does not implicitly depend on routes the way `wrangler_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,
workers_dev_previews: 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 @@ -5050,7 +5050,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 () => {
writeWranglerToml({
workers_dev: true,
});
Expand All @@ -5072,6 +5072,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 () => {
writeWranglerToml({
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 () => {
writeWranglerToml({
workers_dev: true,
workers_dev_previews: 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 () => {
writeWranglerToml({
workers_dev: false,
Expand All @@ -5093,7 +5140,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 () => {
writeWranglerToml({
workers_dev: false,
});
Expand All @@ -5115,6 +5162,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 () => {
writeWranglerToml({
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 () => {
writeWranglerToml({
workers_dev: false,
workers_dev_previews: 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 () => {
writeWranglerToml({
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 workers_dev_previews is false", async () => {
mockGetScript();
mockUploadVersion(true);
mockGetWorkerSubdomain({ enabled: false });
mockGetWorkerSubdomain({ enabled: true, previews_enabled: false });

// Setup
writeWranglerToml({
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,
workers_dev_previews: 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
*/
workers_dev_previews: 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 @@ -1121,6 +1121,15 @@ function normalizeAndValidateEnvironment(
undefined
);

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

const { deprecatedUpload, ...build } = normalizeAndValidateBuild(
diagnostics,
rawEnv,
Expand Down Expand Up @@ -1255,6 +1264,7 @@ function normalizeAndValidateEnvironment(
placement: normalizeAndValidatePlacement(diagnostics, topLevelEnv, rawEnv),
build,
workers_dev,
workers_dev_previews,
// 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 @@ -945,6 +946,7 @@ async function getWorkerConfig(
name: workerName,
main: entrypoint,
workers_dev: workersDev.enabled,
workers_dev_previews: workersDev.previews_enabled,
compatibility_date:
serviceEnvMetadata.script.compatibility_date ??
new Date().toISOString().substring(0, 10),
Expand Down
Loading

0 comments on commit 40665e2

Please sign in to comment.