Skip to content

Commit

Permalink
fix(cloudflare): support custom baseURL (#2821)
Browse files Browse the repository at this point in the history
  • Loading branch information
atinux authored Oct 31, 2024
1 parent dc42f89 commit 0867c6d
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 12 deletions.
5 changes: 3 additions & 2 deletions src/presets/cloudflare/preset.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ const cloudflarePages = defineNitroPreset(
},
output: {
dir: "{{ rootDir }}/dist",
publicDir: "{{ output.dir }}",
publicDir: "{{ output.dir }}/{{ baseURL }}",
serverDir: "{{ output.dir }}/_worker.js",
},
alias: {
Expand Down Expand Up @@ -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",
Expand Down
23 changes: 13 additions & 10 deletions src/presets/cloudflare/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import { join, resolve } from "pathe";
import { isCI } from "std-env";
import {
joinURL,
hasProtocol,
withLeadingSlash,
withTrailingSlash,
withoutLeadingSlash,
Expand Down Expand Up @@ -35,7 +36,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)
);

Expand Down Expand Up @@ -66,13 +67,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: [
Expand Down Expand Up @@ -107,7 +108,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(
Expand All @@ -118,7 +119,7 @@ async function writeCFPagesHeaders(nitro: Nitro) {
([_, routeRules]) => routeRules.headers
)) {
const headers = [
path.replace("/**", "/*"),
joinURL(nitro.options.baseURL, path.replace("/**", "/*")),
...Object.entries({ ...routeRules.headers }).map(
([header, value]) => ` ${header}: ${value}`
),
Expand All @@ -145,11 +146,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(
Expand All @@ -160,9 +161,11 @@ async function writeCFPagesRedirects(nitro: Nitro) {
([_, routeRules]) => routeRules.redirect
)) {
const code = routeRules.redirect!.statusCode;
contents.unshift(
`${key.replace("/**", "/*")}\t${routeRules.redirect!.to}\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)) {
Expand Down

0 comments on commit 0867c6d

Please sign in to comment.