Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(cloudflare): support custom baseURL #2821

Merged
merged 5 commits into from
Oct 31, 2024
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
17 changes: 9 additions & 8 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,
withBase,
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 @@ -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}`
pi0 marked this conversation as resolved.
Show resolved Hide resolved
);
}

Expand Down