From 9dfd42aba69f757087461a34414a62bac188ac38 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?S=C3=A9bastien=20Chopin?= Date: Wed, 30 Oct 2024 00:25:10 +0100 Subject: [PATCH 1/4] fix(cloudflare): support custom `baseURL` --- src/presets/cloudflare/preset.ts | 5 +++-- src/presets/cloudflare/utils.ts | 14 +++++++------- 2 files changed, 10 insertions(+), 9 deletions(-) diff --git a/src/presets/cloudflare/preset.ts b/src/presets/cloudflare/preset.ts index 46064a441d..910519a353 100644 --- a/src/presets/cloudflare/preset.ts +++ b/src/presets/cloudflare/preset.ts @@ -17,7 +17,7 @@ const cloudflarePages = defineNitroPreset( }, output: { dir: "{{ rootDir }}/dist", - publicDir: "{{ output.dir }}", + publicDir: "{{ output.dir }}/{{ baseURL }}", serverDir: "{{ output.dir }}/_worker.js", }, alias: { @@ -53,7 +53,8 @@ const cloudflarePagesStatic = defineNitroPreset( { extends: "static", output: { - publicDir: "{{ rootDir }}/dist", + dir: "{{ rootDir }}/dist", + publicDir: "{{ output.dir }}/{{ baseURL }}", }, commands: { preview: "npx wrangler pages dev dist", diff --git a/src/presets/cloudflare/utils.ts b/src/presets/cloudflare/utils.ts index 743c00e106..f630230b3c 100644 --- a/src/presets/cloudflare/utils.ts +++ b/src/presets/cloudflare/utils.ts @@ -35,7 +35,7 @@ async function writeCFRoutes(nitro: Nitro) { const writeRoutes = () => fsp.writeFile( - resolve(nitro.options.output.publicDir, "_routes.json"), + resolve(nitro.options.output.dir, "_routes.json"), JSON.stringify(routes, undefined, 2) ); @@ -66,13 +66,13 @@ async function writeCFRoutes(nitro: Nitro) { // Explicit prefixes routes.exclude!.push( ...explicitPublicAssets - .map((dir) => joinURL(dir.baseURL!, "*")) + .map((asset) => joinURL(nitro.options.baseURL, asset.baseURL || "/", "*")) .sort(comparePaths) ); // Unprefixed assets const publicAssetFiles = await globby("**", { - cwd: nitro.options.output.publicDir, + cwd: nitro.options.output.dir, absolute: false, dot: true, ignore: [ @@ -107,7 +107,7 @@ function comparePaths(a: string, b: string) { } async function writeCFPagesHeaders(nitro: Nitro) { - const headersPath = join(nitro.options.output.publicDir, "_headers"); + const headersPath = join(nitro.options.output.dir, "_headers"); const contents = []; const rules = Object.entries(nitro.options.routeRules).sort( @@ -118,7 +118,7 @@ async function writeCFPagesHeaders(nitro: Nitro) { ([_, routeRules]) => routeRules.headers )) { const headers = [ - path.replace("/**", "/*"), + path.replace("/**", "/*").replace(/^\//, nitro.options.baseURL || "/"), ...Object.entries({ ...routeRules.headers }).map( ([header, value]) => ` ${header}: ${value}` ), @@ -145,11 +145,11 @@ async function writeCFPagesHeaders(nitro: Nitro) { } async function writeCFPagesRedirects(nitro: Nitro) { - const redirectsPath = join(nitro.options.output.publicDir, "_redirects"); + const redirectsPath = join(nitro.options.output.dir, "_redirects"); const staticFallback = existsSync( join(nitro.options.output.publicDir, "404.html") ) - ? "/* /404.html 404" + ? `${joinURL(nitro.options.baseURL, "/")}* ${joinURL(nitro.options.baseURL, "/")}404.html 404` : ""; const contents = [staticFallback]; const rules = Object.entries(nitro.options.routeRules).sort( From 32fa9fbf64334ab2f2cbbbe03b5a34ba60f9ef3a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?S=C3=A9bastien=20Chopin?= Date: Wed, 30 Oct 2024 15:46:53 +0100 Subject: [PATCH 2/4] chore: use withBase and joinURL --- src/presets/cloudflare/utils.ts | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/src/presets/cloudflare/utils.ts b/src/presets/cloudflare/utils.ts index f630230b3c..fb6f3d7f00 100644 --- a/src/presets/cloudflare/utils.ts +++ b/src/presets/cloudflare/utils.ts @@ -7,6 +7,7 @@ import { join, resolve } from "pathe"; import { isCI } from "std-env"; import { joinURL, + withBase, withLeadingSlash, withTrailingSlash, withoutLeadingSlash, @@ -118,7 +119,7 @@ async function writeCFPagesHeaders(nitro: Nitro) { ([_, routeRules]) => routeRules.headers )) { const headers = [ - path.replace("/**", "/*").replace(/^\//, nitro.options.baseURL || "/"), + withBase(path.replace("/**", "/*"), nitro.options.baseURL), ...Object.entries({ ...routeRules.headers }).map( ([header, value]) => ` ${header}: ${value}` ), @@ -149,7 +150,7 @@ async function writeCFPagesRedirects(nitro: Nitro) { const staticFallback = existsSync( join(nitro.options.output.publicDir, "404.html") ) - ? `${joinURL(nitro.options.baseURL, "/")}* ${joinURL(nitro.options.baseURL, "/")}404.html 404` + ? `${joinURL(nitro.options.baseURL, "/*")} ${joinURL(nitro.options.baseURL, "/404.html")} 404` : ""; const contents = [staticFallback]; const rules = Object.entries(nitro.options.routeRules).sort( @@ -161,7 +162,7 @@ async function writeCFPagesRedirects(nitro: Nitro) { )) { const code = routeRules.redirect!.statusCode; contents.unshift( - `${key.replace("/**", "/*")}\t${routeRules.redirect!.to}\t${code}` + `${withBase(key.replace("/**", "/*"), nitro.options.baseURL)}\t${withBase(routeRules.redirect!.to, nitro.options.baseURL)}\t${code}` ); } From d2bbd4ce88947a20e92761e70e145dc1a0ce2fe6 Mon Sep 17 00:00:00 2001 From: Pooya Parsa Date: Thu, 31 Oct 2024 13:42:36 +0100 Subject: [PATCH 3/4] Update src/presets/cloudflare/utils.ts MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Co-authored-by: Sébastien Chopin --- src/presets/cloudflare/utils.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/presets/cloudflare/utils.ts b/src/presets/cloudflare/utils.ts index fb6f3d7f00..3212e42821 100644 --- a/src/presets/cloudflare/utils.ts +++ b/src/presets/cloudflare/utils.ts @@ -119,7 +119,7 @@ async function writeCFPagesHeaders(nitro: Nitro) { ([_, routeRules]) => routeRules.headers )) { const headers = [ - withBase(path.replace("/**", "/*"), nitro.options.baseURL), + joinURL(nitro.options.baseURL, path.replace("/**", "/*")), ...Object.entries({ ...routeRules.headers }).map( ([header, value]) => ` ${header}: ${value}` ), From cff5807527ee24b6759be9a64cbd49eeb5b14cb5 Mon Sep 17 00:00:00 2001 From: Pooya Parsa Date: Thu, 31 Oct 2024 17:33:06 +0100 Subject: [PATCH 4/4] update --- src/presets/cloudflare/utils.ts | 10 ++++++---- 1 file changed, 6 insertions(+), 4 deletions(-) diff --git a/src/presets/cloudflare/utils.ts b/src/presets/cloudflare/utils.ts index 3212e42821..b12cc93d7f 100644 --- a/src/presets/cloudflare/utils.ts +++ b/src/presets/cloudflare/utils.ts @@ -7,7 +7,7 @@ import { join, resolve } from "pathe"; import { isCI } from "std-env"; import { joinURL, - withBase, + hasProtocol, withLeadingSlash, withTrailingSlash, withoutLeadingSlash, @@ -161,9 +161,11 @@ async function writeCFPagesRedirects(nitro: Nitro) { ([_, routeRules]) => routeRules.redirect )) { const code = routeRules.redirect!.statusCode; - contents.unshift( - `${withBase(key.replace("/**", "/*"), nitro.options.baseURL)}\t${withBase(routeRules.redirect!.to, nitro.options.baseURL)}\t${code}` - ); + const from = joinURL(nitro.options.baseURL, key.replace("/**", "/*")); + const to = hasProtocol(routeRules.redirect!.to, { acceptRelative: true }) + ? routeRules.redirect!.to + : joinURL(nitro.options.baseURL, routeRules.redirect!.to); + contents.unshift(`${from}\t${to}\t${code}`); } if (existsSync(redirectsPath)) {