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

vite: make getLoadContext optional for cloudflare pages handler #8701

Merged
merged 3 commits into from
Feb 7, 2024
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
8 changes: 8 additions & 0 deletions .changeset/brown-rats-flow.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
---
"@remix-run/cloudflare-pages": minor
---

Make `getLoadContext` optional for Cloudflare Pages

Defaults to `(context) => ({ env: context })`, which is what we used to have in all the templates.
This gives parity with the Cloudflare preset for the Remix Vite plugin and keeps our templates leaner.
12 changes: 1 addition & 11 deletions docs/guides/envvars.md
Original file line number Diff line number Diff line change
Expand Up @@ -48,17 +48,7 @@ export async function loader() {

If you're using the `@remix-run/cloudflare-pages` adapter, env variables work a little differently. Since Cloudflare Pages are powered by Functions, you'll need to define your local environment variables in the [`.dev.vars`][dev-vars] file. It has the same syntax as `.env` example file mentioned above.

Then, you can pass those through via `getLoadContext` in your server file:

```ts
export const onRequest = createPagesFunctionHandler({
build,
getLoadContext: (context) => ({ env: context.env }), // Hand-off Cloudflare ENV vars to the Remix `context` object
mode: build.mode,
});
```

And they'll be available via Remix's `context` in your `loader`/`action` functions:
Then, they'll be available via Remix's `context.env` in your `loader`/`action` functions:

```tsx
export const loader = async ({
Expand Down
5 changes: 1 addition & 4 deletions docs/guides/manual-mode.md
Original file line number Diff line number Diff line change
Expand Up @@ -259,10 +259,7 @@ app.all(
"*",
process.env.NODE_ENV === "development"
? createDevRequestHandler(initialBuild)
: createRequestHandler({
build: initialBuild,
mode: initialBuild.mode,
})
: createRequestHandler({ build: initialBuild })
);
```

Expand Down
8 changes: 1 addition & 7 deletions docs/other-api/dev.md
Original file line number Diff line number Diff line change
Expand Up @@ -91,13 +91,7 @@ If not, you can follow these steps to integrate your project with `remix dev`:

// ... code for setting up your express app goes here ...

app.all(
"*",
createRequestHandler({
build,
mode: build.mode,
})
);
app.all("*", createRequestHandler({ build }));

const port = 3000;
app.listen(port, () => {
Expand Down
1 change: 0 additions & 1 deletion integration/helpers/deno-template/server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ import * as build from "@remix-run/dev/server-build";
const remixHandler = createRequestHandlerWithStaticFiles({
build,
getLoadContext: () => ({}),
mode: build.mode,
});

const port = Number(Deno.env.get("PORT")) || 8000;
Expand Down
4 changes: 2 additions & 2 deletions packages/remix-cloudflare-pages/worker.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,13 +26,13 @@ export interface createPagesFunctionHandlerParams<Env = any> {

export function createRequestHandler<Env = any>({
build,
getLoadContext,
mode,
getLoadContext = (context) => ({ env: context.env }),
}: createPagesFunctionHandlerParams<Env>): RequestHandler<Env> {
let handleRequest = createRemixRequestHandler(build, mode);

return async (context) => {
let loadContext = await getLoadContext?.(context);
let loadContext = await getLoadContext(context);

return handleRequest(context.request, loadContext);
};
Expand Down
6 changes: 1 addition & 5 deletions packages/remix-dev/__tests__/fixtures/cloudflare/server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,4 @@ if (process.env.NODE_ENV === "development") {
logDevReady(build);
}

export const onRequest = createPagesFunctionHandler({
build,
getLoadContext: (context) => ({ env: context.env }),
mode: build.mode,
});
export const onRequest = createPagesFunctionHandler({ build });
1 change: 0 additions & 1 deletion packages/remix-dev/__tests__/fixtures/deno/server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ import * as build from "@remix-run/dev/server-build";
const remixHandler = createRequestHandlerWithStaticFiles({
build,
getLoadContext: () => ({}),
mode: build.mode,
});

const port = Number(Deno.env.get("PORT")) || 8000;
Expand Down
5 changes: 1 addition & 4 deletions templates/arc/server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,4 @@ import sourceMapSupport from "source-map-support";
sourceMapSupport.install();
installGlobals();

export const handler = createRequestHandler({
build,
mode: build.mode,
});
export const handler = createRequestHandler({ build });
6 changes: 1 addition & 5 deletions templates/cloudflare-pages/server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,4 @@ if (process.env.NODE_ENV === "development") {
logDevReady(build);
}

export const onRequest = createPagesFunctionHandler({
build,
getLoadContext: (context) => ({ env: context.env }),
mode: build.mode,
});
export const onRequest = createPagesFunctionHandler({ build });
3 changes: 1 addition & 2 deletions templates/deno/server.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,11 @@
import { serve } from "https://deno.land/std@0.128.0/http/server.ts";
import { createRequestHandlerWithStaticFiles } from "@remix-run/deno";
// Import path interpreted by the Remix compiler
import * as build from "@remix-run/dev/server-build";
import { serve } from "https://deno.land/std@0.128.0/http/server.ts";

const remixHandler = createRequestHandlerWithStaticFiles({
build,
getLoadContext: () => ({}),
mode: build.mode,
});

const port = Number(Deno.env.get("PORT")) || 8000;
Expand Down
5 changes: 1 addition & 4 deletions templates/express/server.js
Original file line number Diff line number Diff line change
Expand Up @@ -36,10 +36,7 @@ const initialBuild = await reimportServer();
const remixHandler =
process.env.NODE_ENV === "development"
? await createDevRequestHandler(initialBuild)
: createRequestHandler({
build: initialBuild,
mode: initialBuild.mode,
});
: createRequestHandler({ build: initialBuild });

const app = express();

Expand Down