-
Notifications
You must be signed in to change notification settings - Fork 92
fix(new-webui): Register React SPA routes in Fastify server; Fix path resolution for root directory. (fixes #1011; fixes #1014). #1015
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
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change | ||||||||||||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
|
@@ -16,7 +16,7 @@ import settings from "../../settings.json" with {type: "json"}; | |||||||||||||||||||||||||
| const routes: FastifyPluginAsync = async (fastify) => { | ||||||||||||||||||||||||||
| const filename = fileURLToPath(import.meta.url); | ||||||||||||||||||||||||||
| const dirname = path.dirname(filename); | ||||||||||||||||||||||||||
| const rootDirname = path.resolve(dirname, "../.."); | ||||||||||||||||||||||||||
| const rootDirname = path.resolve(dirname, "../../../.."); | ||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||
| let streamFilesDir = settings.StreamFilesDir; | ||||||||||||||||||||||||||
| if (false === path.isAbsolute(streamFilesDir)) { | ||||||||||||||||||||||||||
|
|
@@ -25,6 +25,7 @@ const routes: FastifyPluginAsync = async (fastify) => { | |||||||||||||||||||||||||
| await fastify.register(fastifyStatic, { | ||||||||||||||||||||||||||
| prefix: "/streams", | ||||||||||||||||||||||||||
| root: streamFilesDir, | ||||||||||||||||||||||||||
| decorateReply: false, | ||||||||||||||||||||||||||
| }); | ||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||
| let logViewerDir = settings.LogViewerDir; | ||||||||||||||||||||||||||
|
|
@@ -48,12 +49,13 @@ const routes: FastifyPluginAsync = async (fastify) => { | |||||||||||||||||||||||||
| await fastify.register(fastifyStatic, { | ||||||||||||||||||||||||||
| prefix: "/", | ||||||||||||||||||||||||||
| root: clientDir, | ||||||||||||||||||||||||||
| decorateReply: false, | ||||||||||||||||||||||||||
| decorateReply: true, | ||||||||||||||||||||||||||
| wildcard: false, | ||||||||||||||||||||||||||
|
Comment on lines
49
to
53
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
With - wildcard: false,
+ // allow hashed asset files under /
+ wildcard: true,📝 Committable suggestion
Suggested change
🤖 Prompt for AI Agents
Member
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @davemarco why we had it disabled? and do you know why the assets were still getting served even with it disabeld?
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. when i was looking how to get the log viewer connector served under streamFile, it seemed like u had to disable it to have SPA fallback. I think it still serves other files in the directory, as long as they are there at build time. https://github.com/fastify/fastify-static?tab=readme-ov-file#wildcard
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Member
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
if i understand correctly, this means the routes to serve all static files are created at init time which makes the serving deterministic for assets files now we are adding a catch all route to serve index.html on any misses all seems good then |
||||||||||||||||||||||||||
| }); | ||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||
| fastify.get("/streamFile", (_, reply) => { | ||||||||||||||||||||||||||
| reply.sendFile("index.html", clientDir); | ||||||||||||||||||||||||||
| // Serve index.html for all unmatched routes in the React Single Page Application (SPA). | ||||||||||||||||||||||||||
| fastify.get("/*", (_, reply) => { | ||||||||||||||||||||||||||
| reply.sendFile("index.html"); | ||||||||||||||||||||||||||
| }); | ||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||
| }; | ||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
what is the purpose of the decorate reply change?
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
decorateReplyis by defaulttruein the fastify-static plugin; however, when we register the plugin multiple times to serve static files on different routes from different paths, only one instance of the registration can specifydecorateReply.since we will only be calling
sendFileto send files on/to serve the React SPA client, we should setdecorateReplyon the/fastify-static registration instance. the previous decoration on the/streamsregistration instance was unnecessary and unused anyways.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Are we not also sending files on the /streams directory?
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
like i understand what it does, but not why we are changing
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Oh is it related to removing the client_dir change u made before?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
we are
right. see below
we need to call
reply.sendFile()to serve the index.html file for all requests on/*. we can only setdecorateReply: trueon a single registration of the fastify-static plugin, and this should be done only with