Skip to content

Commit

Permalink
fix(cloudflare-pages): autodetect static preset (#1659)
Browse files Browse the repository at this point in the history
  • Loading branch information
Hebilicious committed Aug 29, 2023
1 parent 4ac686a commit 670fb90
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 11 deletions.
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) {

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

0 comments on commit 670fb90

Please sign in to comment.