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

Remix 2.7.0 has an invalid default getLoadContext for Cloudflare Pages #8816

Closed
AdiRishi opened this issue Feb 21, 2024 · 3 comments
Closed
Labels
adapter:cloudflare-pages bug Something isn't working

Comments

@AdiRishi
Copy link
Contributor

Reproduction

https://github.com/AdiRishi/remix-2.7.0-env-bug

System Info

System:
  OS: macOS 14.3.1
  CPU: (8) arm64 Apple M1 Pro
  Memory: 6.61 GB / 32.00 GB
  Shell: 5.9 - /bin/zsh
Binaries:
  Node: 20.11.0 - ~/.nvm/versions/node/v20.11.0/bin/node
  Yarn: 1.22.19 - ~/.nvm/versions/node/v20.11.0/bin/yarn
  npm: 10.4.0 - ~/.nvm/versions/node/v20.11.0/bin/npm
  pnpm: 8.15.3 - ~/.nvm/versions/node/v20.11.0/bin/pnpm
  bun: 1.0.25 - /opt/homebrew/bin/bun
Browsers:
  Chrome: 121.0.6167.184
  Safari: 17.3.1
npmPackages:
  @remix-run/cloudflare: ^2.7.0 => 2.7.0
  @remix-run/cloudflare-pages: ^2.7.0 => 2.7.0
  @remix-run/css-bundle: ^2.7.0 => 2.7.0
  @remix-run/dev: ^2.7.0 => 2.7.0
  @remix-run/react: ^2.7.0 => 2.7.0

Used Package Manager

pnpm

Expected Behavior

When accessing args.context.env from a loader we expect this to contain environment variables loaded from .dev.vars

Actual Behavior

The 2.7.0 release seems to make the getLoadContext function optional in createPagesFunctionHandler. The expectation according to the release notes is that this will bring the implementation inline with the vite setup and correctly default to loading context.env.

However, this is not the case. The reasoning seems to be that the default implementation of getLoadContext is (context) => ({ env: context }). The correct implementation actually seems to be ({ context }) => ({ env: context.cloudflare.env }) since both the input arguments and context type seem to have changed in the latest release.

To be clear, this is what shows up in the cloudflare-pages template for server.ts

import { logDevReady } from "@remix-run/cloudflare";
import { createPagesFunctionHandler } from "@remix-run/cloudflare-pages";
import * as build from "@remix-run/dev/server-build";

if (process.env.NODE_ENV === "development") {
  logDevReady(build);
}

export const onRequest = createPagesFunctionHandler({ build });

A correct server.ts looks like this

import { logDevReady } from '@remix-run/cloudflare';
import { createPagesFunctionHandler } from '@remix-run/cloudflare-pages';
import * as build from '@remix-run/dev/server-build';

if (process.env.NODE_ENV === 'development') {
  logDevReady(build);
}

export const onRequest = createPagesFunctionHandler({
  build,
  getLoadContext: ({ context }) => ({ env: context.cloudflare.env }),
});

I've demonstrated this in the reproduction repository as well.

@markdalgleish
Copy link
Member

Fixed in #8819, released in v2.7.1.

@AdiRishi
Copy link
Contributor Author

Awesome!

Just a question, I see in the PR that you've marked the old args passed to getLoadContext as deprecated. To maintain compatibility with v3 going forward, is it expected that we use getLoadContext: ({ context }) => ({ env: context.cloudflare.env })?

Copy link
Contributor

🤖 Hello there,

We just published version 2.7.1 which involves this issue. If you'd like to take it for a test run please try it out and let us know what you think!

Thanks!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
adapter:cloudflare-pages bug Something isn't working
Projects
None yet
Development

No branches or pull requests

3 participants