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

fix(remix-dev/vite): use global Request class in Vite dev server handler #8062

Merged

Conversation

hi-ogawa
Copy link
Contributor

@hi-ogawa hi-ogawa commented Nov 20, 2023

Closes: #7819, #8050

Currently in vite dev, remix handler makes use of NodeRequest class (based on undici) which is a different from global Request and this breaks user code relying on ... instanceof Request.

I noticed that this issue doesn't exist on express custom server since it uses its own way to construct Request instance based on global Request in packages/remix-express/server.ts. So, I was wondering if it would make sense to do something similar for vite in packages/remix-dev/vite/node/adapter.ts as well.

Probably there is a reason to use handler code based on solid-start, so if there is a known issue with this approach, please let me know.

testing strategy

I added a new test integration/vite-dev-custom-entry-test.ts to verify request instance Request is true during server entry's handleRequest.

Copy link

changeset-bot bot commented Nov 20, 2023

🦋 Changeset detected

Latest commit: f4b3f34

The changes in this PR will be included in the next version bump.

This PR includes changesets to release 16 packages
Name Type
@remix-run/dev Patch
create-remix Patch
remix Patch
@remix-run/architect Patch
@remix-run/cloudflare Patch
@remix-run/cloudflare-pages Patch
@remix-run/cloudflare-workers Patch
@remix-run/css-bundle Patch
@remix-run/deno Patch
@remix-run/eslint-config Patch
@remix-run/express Patch
@remix-run/node Patch
@remix-run/react Patch
@remix-run/serve Patch
@remix-run/server-runtime Patch
@remix-run/testing Patch

Not sure what this means? Click here to learn what changesets are.

Click here if you're a maintainer who wants to add another changeset to this PR

@hi-ogawa hi-ogawa changed the title fix(remix-dev/vite): use consistent Request class fix(remix-dev/vite): use global Request class in vite middleware handler Nov 20, 2023
@hi-ogawa hi-ogawa marked this pull request as ready for review November 20, 2023 05:21
@markdalgleish markdalgleish changed the title fix(remix-dev/vite): use global Request class in vite middleware handler fix(remix-dev/vite): use global Request class in Vite dev server handler Nov 23, 2023
Copy link
Member

@markdalgleish markdalgleish left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks so much for the PR! I love that this gets us much closer to the other adapters.

@markdalgleish markdalgleish merged commit 3bf45f0 into remix-run:dev Nov 23, 2023
9 checks passed
@hi-ogawa hi-ogawa deleted the fix-vite-consistent-Request-class branch November 23, 2023 23:34

return new ReadableStream({ start: start, pull: pull, cancel: cancel });
}
installGlobals();
Copy link
Contributor Author

@hi-ogawa hi-ogawa Nov 24, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for proceeding with this PR!

I was wondering about what's the future plan of this forced installGlobals during vite dev.
Node 18 ships fetch, Request, etc... globals, so I thought forcing polyfills here might not be appropriate and better to be opt-in choice by individual users.

Looking at what happened in other adapters #7009 (and also unstable-vite-express currently has explicit installGlobals in server.mjs), the general direction seems to remove automatic/forced installGlobals.

Currently this polyfills kicks in whenever remix vite plugin is loaded, so I think it's not even possible for custom server (during dev) to opt-out from installGlobals currently.

Here is a quick repro:

https://stackblitz.com/edit/remix-run-remix-ws1uig?file=vite.config.ts

//
// vite.config.ts
//

let fetch1 = fetch;

import { unstable_vitePlugin as remix } from '@remix-run/dev';
import { defineConfig } from 'vite';
import tsconfigPaths from 'vite-tsconfig-paths';

let fetch2 = fetch;

export default defineConfig({
  clearScreen: false,
  plugins: [remix(), tsconfigPaths()],
});

let fetch3 = fetch;

// the log happens twice in parent and child compilers
//   parent: true false
//   child:  true true
console.log(fetch1 === fetch2, fetch1 === fetch3);

EDIT: Current behavior would be essentially same as installGlobals in vite.config.ts, so in order to allow users to opt-out, I think I would prefer doing it in the template's vite.config.ts #8119

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yep, I was wanting to follow this up separately. Thanks for looking into this!

Copy link
Contributor

github-actions bot commented Dec 5, 2023

🤖 Hello there,

We just published version 2.4.0-pre.0 which includes this pull request. If you'd like to take it for a test run please try it out and let us know what you think!

Thanks!

Copy link
Contributor

🤖 Hello there,

We just published version 2.4.0 which includes this pull request. 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
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants