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

Unstable Vite: support for custom servers #7682

Merged
merged 7 commits into from
Oct 24, 2023
Merged

Conversation

pcattori
Copy link
Contributor

@pcattori pcattori commented Oct 16, 2023

Expanded createRequestHandler to accept a function for build that acts as a dynamic way to get the latest build during dev. Then added APIs for custom server to delegate asset handling and SSR to Vite in dev:

import { createViteServer, loadViteServerBuild } from "@remix-run/dev"

// util to setup a Vite dev server in middleware mode
let vite = createViteServer()
//  ^? import("vite").ViteDevServer

// serve assets via built-in Vite middleware
app.use(vite.middlewares)

// serve SSR via Remix's request handler
app.use('*', createRequestHandler(() => loadViteServerBuild(vite)))

Names prefixed with unstable_ for now

Example usage

// server.mjs
import {
  unstable_createViteServer,
  unstable_loadViteServerBuild,
} from "@remix-run/dev";
import { createRequestHandler } from "@remix-run/express";
import { installGlobals } from "@remix-run/node";
import compression from "compression";
import express from "express";
import morgan from "morgan";
import sourceMapSupport from "source-map-support";

sourceMapSupport.install();
installGlobals();

let vite =
  process.env.NODE_ENV === "production"
    ? undefined
    : await unstable_createViteServer();

const app = express();
app.disable("x-powered-by");

if (vite) {
  app.use(vite.middlewares);
} else {
  app.use(compression());
  app.use(
    "/build",
    express.static("public/build", { immutable: true, maxAge: "1y" })
  );
}
app.use(express.static("public", { maxAge: "1h" }));
app.use(morgan("tiny"));

app.all(
  "*",
  createRequestHandler({
    build: vite
      ? () => unstable_loadViteServerBuild(vite)
      : await import("./build/index.js"),
  })
);

const port = process.env.PORT || 3000;
app.listen(port, async () => console.log(`Express server listening on port ${port}`));

Testing Strategy

Integration tests

TODO

  • switch to using .ts for vite config

@changeset-bot
Copy link

changeset-bot bot commented Oct 16, 2023

⚠️ No Changeset found

Latest commit: c91dde0

Merging this PR will not cause a version bump for any packages. If these changes should not result in a new version, you're good to go. If these changes should result in a version bump, you need to add a changeset.

This PR includes no changesets

When changesets are added to this PR, you'll see the packages that this PR includes changesets for and the associated semver types

Click here to learn what changesets are, and how to add one.

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

@pcattori pcattori force-pushed the pedro/vite-custom-server branch from c0b7dfa to ab776d0 Compare October 16, 2023 22:20
Comment on lines +449 to +470
server: {
...viteUserConfig.server,
// when parent compiler runs in middleware mode to support
// custom servers, we don't want the child compiler also
// run in middleware mode as that will cause websocket port conflicts
middlewareMode: false,
},
Copy link
Contributor Author

Choose a reason for hiding this comment

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

@markdalgleish maybe we just want to unset all server options for child compiler since we don't want to use it as a server at all?


return async function requestHandler(
request,
loadContext = {},
{ criticalCss } = {}
) {
_build = typeof build === "function" ? await build() : build;
Copy link
Contributor Author

Choose a reason for hiding this comment

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

purposefully placing the await within requestHandler so that createRequestHandler doesn't need to change to async which would be breaking change.

};

export const unstable_loadViteServerBuild = async (vite: ViteDevServer) => {
return vite.ssrLoadModule(id("server-entry"));
Copy link
Contributor Author

Choose a reason for hiding this comment

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

note: duplicated "server-entry" literal from plugin.ts since creating an abstraction on our virtual modules seemed unnecessary so far. might revisit this.

@pcattori pcattori marked this pull request as ready for review October 17, 2023 02:36
@pcattori pcattori changed the title request handler dynamically gets new build when function is passed in Unstable Vite: support for custom servers Oct 17, 2023
@pcattori pcattori force-pushed the pedro/vite-custom-server branch 4 times, most recently from 33a9798 to 4c38ceb Compare October 19, 2023 18:12
@pcattori
Copy link
Contributor Author

Windows CI is failing. Going to investigate/resolve this with a Windows VM next week.

@pcattori pcattori force-pushed the pedro/vite-custom-server branch from 14f5fd3 to cd4809c Compare October 24, 2023 20:24
@pcattori pcattori force-pushed the pedro/vite-custom-server branch from cd4809c to c91dde0 Compare October 24, 2023 20:34
@pcattori pcattori merged commit 9d516dd into dev Oct 24, 2023
9 checks passed
@pcattori pcattori deleted the pedro/vite-custom-server branch October 24, 2023 21:00
@github-actions github-actions bot added the awaiting release This issue has been fixed and will be released soon label Oct 24, 2023
@github-actions
Copy link
Contributor

🤖 Hello there,

We just published version 2.2.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.2.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!

@github-actions github-actions bot removed the awaiting release This issue has been fixed and will be released soon label Oct 31, 2023
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.

2 participants