Skip to content

Commit

Permalink
fix: cloudflare imports
Browse files Browse the repository at this point in the history
  • Loading branch information
Sebastien Ringrose committed Jul 26, 2024
1 parent b8ac60c commit 6d9f0b1
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 21 deletions.
37 changes: 18 additions & 19 deletions example/reactSSR/handlers/github.handler.ts
Original file line number Diff line number Diff line change
@@ -1,24 +1,23 @@
import { Handler, RequestContext, file } from "../../../mod.ts";

const base = `https://raw.githubusercontent.com/sejori/peko/main/example/reactSSR/`;

export const githubHandler =
(path: string, type?: string): Handler =>
async (ctx: RequestContext<{ env?: { ENVIRONMENT: string } }>) =>
(
await file(
ctx.state.env?.ENVIRONMENT === "production"
? new URL(`${base}${path}`)
: new URL(".." + path, import.meta.url),
{
headers: new Headers({
...(type && { "Content-Type": type }),
// instruct browser to cache file in prod env
"Cache-Control":
ctx.state.env?.ENVIRONMENT === "production"
? "max-age=86400, stale-while-revalidate=86400"
: "no-store",
}),
}
)
async (ctx: RequestContext<{ env?: { ENVIRONMENT: string } }>) => {
const base =
ctx.state.env?.ENVIRONMENT === "production" || !import.meta.url
? `https://raw.githubusercontent.com/sejori/peko/main/example/reactSSR/`
: import.meta.url.replace("handlers/github.handler.ts", "");

return (
await file(new URL(`${base}${path}`), {
headers: new Headers({
...(type && { "Content-Type": type }),
// instruct browser to cache file in prod env
"Cache-Control":
ctx.state.env?.ENVIRONMENT === "production"
? "max-age=86400, stale-while-revalidate=86400"
: "no-store",
}),
})
)(ctx);
};
2 changes: 1 addition & 1 deletion tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
"module": "esnext",
"lib": ["dom", "esnext"],
"types": ["@cloudflare/workers-types"],
"jsx": "react",
"jsx": "react-jsx",
"allowSyntheticDefaultImports": true
}
}
1 change: 0 additions & 1 deletion wrangler.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
name = "peko"
main = "scripts/wrangler/main.ts"
node_compat = true
compatibility_date = "2024-02-23"

[vars]
Expand Down

0 comments on commit 6d9f0b1

Please sign in to comment.