diff --git a/.changeset/fair-forks-applaud.md b/.changeset/fair-forks-applaud.md new file mode 100644 index 000000000..1737527c6 --- /dev/null +++ b/.changeset/fair-forks-applaud.md @@ -0,0 +1,5 @@ +--- +"@cloudflare/next-on-pages": minor +--- + +Avoid invoking worker for static files diff --git a/src/index.ts b/src/index.ts index 147ede05c..98be93769 100644 --- a/src/index.ts +++ b/src/index.ts @@ -9,6 +9,8 @@ import pLimit from "p-limit"; import acorn, { parse, Node } from "acorn"; import { generate } from "astring"; +import { version as nextOnPagesVersion } from "../package.json"; + type LooseNode = Node & { expression?: LooseNode; callee?: LooseNode; @@ -433,7 +435,7 @@ const transform = async ({ )}, entrypoint: require('${filepath}')}` ) .join(",")}}; - + export const __MIDDLEWARE__ = {${[...hydratedMiddleware.entries()] .map( ([name, { matchers, filepath }]) => @@ -489,6 +491,45 @@ const help = () => { ); }; + +const generateHeaders = async () => { + await writeFile( + ".vercel/output/static/_headers", + ` +# === START AUTOGENERATED next-on-pages IMMUTABLE HEADERS === +/_next/static/* + Cache-Control: public,max-age=31536000,immutable +# === END AUTOGENERATED next-on-pages IMMUTABLE HEADERS ===\n`, + { + // in case someone configured redirects already, append to the end + flag: "a", + } + ); +}; + +const generateRoutes = async () => { + try { + await writeFile( + ".vercel/output/static/_routes.json", + JSON.stringify({ + version: 1, + description: `Built with @cloudflare/next-on-pages@${nextOnPagesVersion}.`, + include: ["/*"], + exclude: ["/_next/static/*"], + }), + { flag: "ax" } // don't generate file if it's already manually maintained + ); + } catch (e) { + if (e.code != "EEXIST") { + throw e; + } + } +}; + +const generateMetadata = async () => { + await Promise.all([generateHeaders(), generateRoutes()]); +}; + const main = async ({ skipBuild, experimentalMinify, @@ -502,6 +543,7 @@ const main = async ({ } await transform({ experimentalMinify }); + await generateMetadata(); }; (async () => {