Skip to content

Commit

Permalink
Merge pull request #50 from Sebmaster/feature/static-files
Browse files Browse the repository at this point in the history
Avoid invoking worker for static files
  • Loading branch information
dario-piotrowicz authored Mar 14, 2023
2 parents 5483881 + 6b8cda7 commit 4a36000
Show file tree
Hide file tree
Showing 2 changed files with 48 additions and 1 deletion.
5 changes: 5 additions & 0 deletions .changeset/fair-forks-applaud.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@cloudflare/next-on-pages": minor
---

Avoid invoking worker for static files
44 changes: 43 additions & 1 deletion src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -433,7 +435,7 @@ const transform = async ({
)}, entrypoint: require('${filepath}')}`
)
.join(",")}};
export const __MIDDLEWARE__ = {${[...hydratedMiddleware.entries()]
.map(
([name, { matchers, filepath }]) =>
Expand Down Expand Up @@ -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,
Expand All @@ -502,6 +543,7 @@ const main = async ({
}

await transform({ experimentalMinify });
await generateMetadata();
};

(async () => {
Expand Down

0 comments on commit 4a36000

Please sign in to comment.