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 1 commit
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
14 changes: 7 additions & 7 deletions src/presets/cloudflare/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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)
);

Expand Down Expand Up @@ -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: [
Expand Down Expand Up @@ -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(
Expand All @@ -118,7 +118,7 @@ async function writeCFPagesHeaders(nitro: Nitro) {
([_, routeRules]) => routeRules.headers
)) {
const headers = [
path.replace("/**", "/*"),
path.replace("/**", "/*").replace(/^\//, nitro.options.baseURL || "/"),
atinux marked this conversation as resolved.
Show resolved Hide resolved
...Object.entries({ ...routeRules.headers }).map(
([header, value]) => ` ${header}: ${value}`
),
Expand All @@ -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(
Expand Down