Hacktoberfest 2026: le issue che i maintainer hanno segnato per ottobre, aperte e adatte ai principianti. Sfoglia le issue Hacktoberfest

Served static HTML file fails to load resources

Aperta
#776 0 commenti 0 reazioni 0 assegnatari Vedi su GitHub

Nessuno ha ancora preso questa issue.

Valutazione

Difficoltà
3/5
Tempo stimato
1-2 giorni
Idoneità per principianti
45/100
Tipo di issue
Bug
Chiarezza
Abbastanza chiara
Stato di attività
Ferma
Stack tecnologico
css, javascript, node.js, typescript
Ambito
backend, cli

Direzione di ricerca

Riproduci il progetto generato da fastify-cli v7.0.1 con lo script dev ed esamina dist/routes/root.js, dist/public/index.html e la directory dist/assets. Inizia dalla registrazione di fastifyStatic e dalla route root, quindi verifica come l’app generata espone i file pubblici. Il lavoro è completato quando le risorse CSS e JavaScript collegate vengono caricate senza risposte 404 nel progetto generato.

Scritto dal modello di indicizzazione a partire dal testo della issue.

Descrizione

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

Lingua principale
JavaScript
Stelle
733
Fork
179
Merge medio
8g 12h
PR unite (30g)
2

Preparare l'ambiente

Come iniziare

  1. Leggi tutta la issue e poi la guida ai contributi del progetto.
  2. Commenta sulla issue per dire che te ne occupi tu — evita che due persone facciano lo stesso lavoro.
  3. Fai un fork del repository e lavora su un branch.
  4. Apri una pull request che faccia riferimento al numero della issue.

Altre issue di fastify/fastify-cli

Tutte le issue di fastify/fastify-cli

Issue simili

Altre issue su JavaScript

Ricevi le nuove issue nella tua casella

Un breve riepilogo di issue GitHub adatte ai principianti.