diff --git a/.changeset/cyan-suits-kneel.md b/.changeset/cyan-suits-kneel.md new file mode 100644 index 00000000..84a6a912 --- /dev/null +++ b/.changeset/cyan-suits-kneel.md @@ -0,0 +1,5 @@ +--- +"@mcansh/remix-fastify": patch +--- + +fix asset serving when using a basename diff --git a/packages/remix-fastify/src/plugin.ts b/packages/remix-fastify/src/plugin.ts index 7d8ab8fd..04cddd00 100644 --- a/packages/remix-fastify/src/plugin.ts +++ b/packages/remix-fastify/src/plugin.ts @@ -83,7 +83,7 @@ export let remixFastify = fp( assetCacheControl = { public: true, maxAge: "1 year", immutable: true }, defaultCacheControl = { public: true, maxAge: "1 hour" }, productionServerBuild, - }, + } ) => { let cwd = process.env.REMIX_ROOT ?? process.cwd(); @@ -105,7 +105,7 @@ export let remixFastify = fp( let SERVER_BUILD = path.join( resolvedBuildDirectory, "server", - serverBuildFile, + serverBuildFile ); let SERVER_BUILD_URL = url.pathToFileURL(SERVER_BUILD).href; @@ -127,7 +127,7 @@ export let remixFastify = fp( let ASSET_DIR = path.join(BUILD_DIR, "assets"); await fastify.register(fastifyStatic, { root: BUILD_DIR, - prefix: "/", + prefix: basename, wildcard: false, cacheControl: true, dotfiles: "allow", @@ -140,29 +140,30 @@ export let remixFastify = fp( "cache-control", isAsset ? cacheHeader(assetCacheControl) - : cacheHeader(defaultCacheControl), + : cacheHeader(defaultCacheControl) ); }, ...fastifyStaticOptions, }); } - fastify.register(async function createRemixRequestHandler(childServer) { - // remove the default content type parsers - childServer.removeAllContentTypeParsers(); - // allow all content types - childServer.addContentTypeParser("*", (_request, payload, done) => { - done(null, payload); - }); - - let basepath = basename.replace(/\/+$/, "") + "/*"; + fastify.register( + async function createRemixRequestHandler(childServer) { + // remove the default content type parsers + childServer.removeAllContentTypeParsers(); + // allow all content types + childServer.addContentTypeParser("*", (_request, payload, done) => { + done(null, payload); + }); - childServer.all(basepath, remixHandler); - }); + childServer.all("*", remixHandler); + }, + { prefix: basename } + ); }, { // replaced with the package name during build name: process.env.__PACKAGE_NAME__, fastify: "4.x", - }, + } );