Skip to content

Commit 2b533ac

Browse files
committed
fix(publish): preserve cloudflare pages urls
1 parent 2525ba9 commit 2b533ac

2 files changed

Lines changed: 17 additions & 15 deletions

File tree

‎src/publish/README.md‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,7 @@ As an alternative to providing account credentials interactively, you case use t
4646
| Service | Variables |
4747
| -------------- | -------------------------------------- |
4848
| Netlify | `NETLIFY_AUTH_TOKEN` |
49+
| Cloudflare Pages | `CLOUDFLARE_ACCOUNT_ID` and `CLOUDFLARE_API_TOKEN` |
4950
| Postit Connect | `CONNECT_SERVER` and `CONNECT_API_KEY` |
5051

5152
Using an environment variable combined with a `_publish.yml` file that points to the requisite publishing destination is enough to enable the following command to perform a headless publish on CI:
@@ -56,7 +57,7 @@ quarto publish
5657

5758
GitHub Pages publishing requires no special environment variables (as it use the version of git available on your system or on the CI server).
5859

59-
Cloudflare Pages publishing uses the Wrangler CLI. Install Wrangler and authenticate it (for example, run `wrangler login` or set `CLOUDFLARE_API_TOKEN` for CI).
60+
Cloudflare Pages publishing uses the Wrangler CLI. Install Wrangler and authenticate it (for example, run `wrangler login` or set `CLOUDFLARE_API_TOKEN` and `CLOUDFLARE_ACCOUNT_ID` for CI).
6061

6162
### Command Line
6263

‎src/publish/cloudflare/cloudflare.ts‎

Lines changed: 15 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -64,20 +64,18 @@ async function publish(
6464
);
6565

6666
const deploymentUrl = await deploymentUrlFromWrangler(projectName);
67-
const recordUrl = deploymentUrl
68-
? canonicalUrlFromDeployment(deploymentUrl) || deploymentUrl
69-
: defaultProjectUrl(projectName);
67+
const recordUrl = target?.url ??
68+
(deploymentUrl
69+
? canonicalUrlFromDeployment(deploymentUrl) || deploymentUrl
70+
: defaultProjectUrl(projectName));
71+
const openUrl = target?.url ?? deploymentUrl ?? recordUrl;
7072

7173
return [
7274
{
7375
id: projectName,
7476
url: recordUrl,
7577
},
76-
deploymentUrl
77-
? new URL(deploymentUrl)
78-
: recordUrl
79-
? new URL(recordUrl)
80-
: undefined,
78+
openUrl ? new URL(openUrl) : undefined,
8179
];
8280
}
8381

@@ -167,8 +165,6 @@ async function deploymentUrlFromWrangler(
167165
"list",
168166
"--project-name",
169167
projectName,
170-
"--environment",
171-
"production",
172168
"--json",
173169
],
174170
stdout: "piped",
@@ -181,8 +177,13 @@ async function deploymentUrlFromWrangler(
181177
if (!Array.isArray(deployments) || deployments.length === 0) {
182178
return undefined;
183179
}
184-
const deployment = deployments[0];
185-
return deployment.Deployment || deployment.deployment;
180+
for (const deployment of deployments) {
181+
const url = deployment.Deployment || deployment.deployment;
182+
if (url) {
183+
return url;
184+
}
185+
}
186+
return undefined;
186187
} catch {
187188
return undefined;
188189
}
@@ -193,10 +194,10 @@ function canonicalUrlFromDeployment(
193194
): string | undefined {
194195
try {
195196
const url = new URL(deploymentUrl);
196-
const host = url.hostname;
197+
const host = url.hostname.toLowerCase();
197198
if (host.endsWith(".pages.dev")) {
198199
const parts = host.split(".");
199-
if (parts.length >= 3) {
200+
if (parts.length >= 4) {
200201
return `${url.protocol}//${parts.slice(1).join(".")}`;
201202
}
202203
}

0 commit comments

Comments
 (0)