Skip to content

Commit

Permalink
feat: accept custom production server build (#397)
Browse files Browse the repository at this point in the history
  • Loading branch information
alanko0511 authored Aug 13, 2024
1 parent 352c74f commit bd2a09c
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 2 deletions.
5 changes: 5 additions & 0 deletions .changeset/tender-plums-taste.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@mcansh/remix-fastify": patch
---

New optional parameter to provide a custom server build for production. If not provided, it will be loaded using `import()` with the server build path provided in the options.
12 changes: 11 additions & 1 deletion packages/remix-fastify/src/plugin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import fp from "fastify-plugin";
import type { InlineConfig, ViteDevServer } from "vite";
import fastifyStatic, { type FastifyStaticOptions } from "@fastify/static";
import { cacheHeader } from "pretty-cache-header";
import type { ServerBuild } from "@remix-run/node";

import { createRequestHandler } from "./server";
import type { HttpServer, GetLoadContextFunction } from "./server";
Expand Down Expand Up @@ -58,6 +59,14 @@ export type RemixFastifyOptions = {
* @default { public: true, maxAge: '1 hour' }
*/
defaultCacheControl?: Parameters<typeof cacheHeader>[0];
/**
* The Remix server build to use in production. Use this only if the default approach doesn't work for you.
*
* If not provided, it will be loaded using `import()` with the server build path provided in the options.
*/
productionServerBuild?:
| ServerBuild
| (() => ServerBuild | Promise<ServerBuild>);
};

export let remixFastify = fp<RemixFastifyOptions>(
Expand All @@ -73,6 +82,7 @@ export let remixFastify = fp<RemixFastifyOptions>(
fastifyStaticOptions,
assetCacheControl = { public: true, maxAge: "1 year", immutable: true },
defaultCacheControl = { public: true, maxAge: "1 hour" },
productionServerBuild,
},
) => {
let cwd = process.env.REMIX_ROOT ?? process.cwd();
Expand Down Expand Up @@ -150,7 +160,7 @@ export let remixFastify = fp<RemixFastifyOptions>(
if (!vite) throw new Error("we lost vite!");
return vite.ssrLoadModule("virtual:remix/server-build");
}
: () => import(SERVER_BUILD_URL),
: productionServerBuild ?? (() => import(SERVER_BUILD_URL)),
});

return handler(request, reply);
Expand Down
2 changes: 1 addition & 1 deletion packages/remix-fastify/src/server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ export function createRequestHandler<Server extends HttpServer>({
getLoadContext,
mode = process.env.NODE_ENV,
}: {
build: ServerBuild | (() => Promise<ServerBuild>);
build: ServerBuild | (() => ServerBuild | Promise<ServerBuild>);
getLoadContext?: GetLoadContextFunction<Server>;
mode?: string;
}): RequestHandler<Server> {
Expand Down

0 comments on commit bd2a09c

Please sign in to comment.