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

Served static HTML file fails to load resources #776

Open
2 tasks done
mdr0414 opened this issue Dec 5, 2024 · 0 comments
Open
2 tasks done

Served static HTML file fails to load resources #776

mdr0414 opened this issue Dec 5, 2024 · 0 comments

Comments

@mdr0414
Copy link

mdr0414 commented Dec 5, 2024

Prerequisites

  • I have written a descriptive issue title
  • I have searched existing issues to ensure the issue has not already been raised

Issue

Ok - I'm at the end of my rope on this one and I need a little help!

In short, the problem is. I'm serving a static html file with links to external javascript files and css files and these files are not being found. It seems like the Fastify router is trying to route the links (if that makes sense).

Here are the details:

I have created a new Fastify project using fastify-cli v7.0.1 with: fastify generate my-project --esm --lang=ts
Using the npm scripts from the generated project, I run "dev" and the html file is served ok, but I get a 404 on GET http://localhost:8000/assets/mdr.css (the css file linked to the html file).

The error on route "/":
{
"message": "Route GET:/assets/mdr.css not found",
"error": "Not Found",
"statusCode": 404
}

Here is the index.html file:

<!doctype html>
<html lang="en">
  <head>
    <meta charset="UTF-8" />
    <link rel="icon" href="./favicon.ico" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <title>Home Page</title>
    <link rel="stylesheet" href="assets/mdr.css"> <!-- CANNOT FIND THIS FILE -->
    <!-- I ommitted the JS script as this is producing the same error and need to simplfy -->
  </head>
  <body 
      <div>Serving HTML from dist/public REV 1</div>
  </body>
</html>

Here is my /dist folder structure:

/dist
    app.js
    /assets
        mdr.css
    /plugins
        sensible.js <-- generated
        support.js <-- generated

    /public
        index.html
    /routes
        /example
            index.js <-- generated
        root.js

And my root.js route:

    import { FastifyPluginAsync } from "fastify";
    import path from "path";
    import { fileURLToPath } from "url";
    import fastifyStatic from "@fastify/static";

    const __filename = fileURLToPath(import.meta.url);
    const __dirname = path.dirname(__filename);

    const root: FastifyPluginAsync = async (fastify, opts): Promise<void> => {

      fastify.register(fastifyStatic, {
        root: path.join(__dirname, "../","public"),
        prefix: "/assets/", //<-- secondary problem - if set to "/public" I get an error that it is already defined somewhere else (not by me)
      });

      fastify.get("/", async function (request, reply:any) {
        return reply.sendFile("index.html");
      });


    };

    export default root;

The generated app is using Autoload on routes and plugins, all created by the generator.
I have just about everything I can think of to fix this and have not been able to.
Any help would be appreciated!

Thanks
_mark

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant