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-pages): autodetect static preset #1659

Merged
merged 2 commits into from
Aug 29, 2023
Merged
Show file tree
Hide file tree
Changes from all 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
21 changes: 10 additions & 11 deletions src/presets/cloudflare-pages.ts
Original file line number Diff line number Diff line change
Expand Up @@ -138,7 +138,7 @@ function comparePaths(a: string, b: string) {

pi0 marked this conversation as resolved.
Show resolved Hide resolved
async function writeCFPagesHeaders(nitro: Nitro) {
const headersPath = join(nitro.options.output.publicDir, "_headers");
let contents = "";
const contents = [];

const rules = Object.entries(nitro.options.routeRules).sort(
(a, b) => b[0].split(/\/(?!\*)/).length - a[0].split(/\/(?!\*)/).length
Expand All @@ -154,7 +154,7 @@ async function writeCFPagesHeaders(nitro: Nitro) {
),
].join("\n");

contents += headers + "\n";
contents.push(headers);
}

if (existsSync(headersPath)) {
Expand All @@ -168,10 +168,10 @@ async function writeCFPagesHeaders(nitro: Nitro) {
nitro.logger.info(
"Adding Nitro fallback to `_headers` to handle all unmatched routes."
);
contents = currentHeaders + "\n" + contents;
contents.unshift(currentHeaders);
}

await fsp.writeFile(headersPath, contents);
await fsp.writeFile(headersPath, contents.join("\n"));
}

async function writeCFPagesRedirects(nitro: Nitro) {
Expand All @@ -181,8 +181,7 @@ async function writeCFPagesRedirects(nitro: Nitro) {
)
? "/* /404.html 404"
: "";
let contents = staticFallback;

const contents = [staticFallback];
const rules = Object.entries(nitro.options.routeRules).sort(
(a, b) => a[0].split(/\/(?!\*)/).length - b[0].split(/\/(?!\*)/).length
);
Expand All @@ -191,9 +190,9 @@ async function writeCFPagesRedirects(nitro: Nitro) {
([_, routeRules]) => routeRules.redirect
)) {
const code = routeRules.redirect.statusCode;
contents =
`${key.replace("/**", "/*")}\t${routeRules.redirect.to}\t${code}\n` +
contents;
contents.unshift(
`${key.replace("/**", "/*")}\t${routeRules.redirect.to}\t${code}`
);
}

if (existsSync(redirectsPath)) {
Expand All @@ -207,8 +206,8 @@ async function writeCFPagesRedirects(nitro: Nitro) {
nitro.logger.info(
"Adding Nitro fallback to `_redirects` to handle all unmatched routes."
);
contents = currentRedirects + "\n" + contents;
contents.unshift(currentRedirects);
}

await fsp.writeFile(redirectsPath, contents);
await fsp.writeFile(redirectsPath, contents.join("\n"));
}
1 change: 1 addition & 0 deletions src/utils/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -116,6 +116,7 @@ const autodetectableStaticProviders: Partial<
> = {
netlify: "netlify-static",
vercel: "vercel-static",
cloudflare_pages: "cloudflare-pages-static",
};

export function detectTarget(options: { static?: boolean } = {}) {
Expand Down