Skip to content

Commit

Permalink
fix asset serving when using a basename (#442)
Browse files Browse the repository at this point in the history
* Changing static / to basename

* Create cyan-suits-kneel.md

Signed-off-by: Logan McAnsh <logan@mcan.sh>

* chore: use prefix fastify option

---------

Signed-off-by: Logan McAnsh <logan@mcan.sh>
Co-authored-by: Logan McAnsh <logan@mcan.sh>
  • Loading branch information
iamtomcat and mcansh authored Oct 21, 2024
1 parent d0ad7d7 commit 88e2ad1
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 16 deletions.
5 changes: 5 additions & 0 deletions .changeset/cyan-suits-kneel.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@mcansh/remix-fastify": patch
---

fix asset serving when using a basename
33 changes: 17 additions & 16 deletions packages/remix-fastify/src/plugin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@ export let remixFastify = fp<RemixFastifyOptions>(
assetCacheControl = { public: true, maxAge: "1 year", immutable: true },
defaultCacheControl = { public: true, maxAge: "1 hour" },
productionServerBuild,
},
}
) => {
let cwd = process.env.REMIX_ROOT ?? process.cwd();

Expand All @@ -105,7 +105,7 @@ export let remixFastify = fp<RemixFastifyOptions>(
let SERVER_BUILD = path.join(
resolvedBuildDirectory,
"server",
serverBuildFile,
serverBuildFile
);
let SERVER_BUILD_URL = url.pathToFileURL(SERVER_BUILD).href;

Expand All @@ -127,7 +127,7 @@ export let remixFastify = fp<RemixFastifyOptions>(
let ASSET_DIR = path.join(BUILD_DIR, "assets");
await fastify.register(fastifyStatic, {
root: BUILD_DIR,
prefix: "/",
prefix: basename,
wildcard: false,
cacheControl: true,
dotfiles: "allow",
Expand All @@ -140,29 +140,30 @@ export let remixFastify = fp<RemixFastifyOptions>(
"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",
},
}
);

0 comments on commit 88e2ad1

Please sign in to comment.