Gatsby: `gatsby develop` does not serve static `index.html` files when directory is accessed

Created on 5 Oct 2017  Â·  18Comments  Â·  Source: gatsbyjs/gatsby

If my directory structure is this:

/static
    /test
        /index.html

and I run gatsby develop, then I cannot access my index.html file by going to localhost:8000/test/, I have to go to localhost:8000/test/index.html.

Most helpful comment

@tech4him1 probably won't get to this soon so would love a PR when you have time or anyone else who sees this issue and wants it solved!

All 18 comments

Hmmm this is perhaps either a limitation of the node server or a misconfiguration. Feel free to submit a PR if you can see ways to improve it.

https://github.com/gatsbyjs/gatsby/blob/master/packages/gatsby/src/utils/develop.js

Haha probably an oversight.

Yeah we should be using the later. See if anything breaks when removing the first.

Well, I think this bug is caused because the second (manual) one doesn't check for index files like the built-in middleware. I could try to implement it, it shouldn't be too hard.

Great! Yeah would love a PR.

I just had a thought for a simpler method (removing the custom code completely):

  app.use(express.static(__dirname + `/public`))

  // Render an HTML page and serve it.
  app.use((req, res, next) => {
    const parsedPath = parsePath(req.originalUrl)
    if (parsedPath.extname === `` || parsedPath.extname.startsWith(`.html`)) {
      res.sendFile(directoryPath(`public/index.html`), err => {
        if (err) {
          res.status(500).end()
        }
      })
    } else {
      res.status(404).end()
    }
  });

What about just like this? Then we can use the built-in one, and we are still essentially doing the same checking.

@KyleAMathews Do you see any obvious problems with that example?

Oh… now that I think about it, we could probably just use the built-in static serving now. I added to the bootstrap process a step to delete all html files in public directory which I think was the only reason to have the custom file serving.

So yeah, your code looks like what we want.

Sounds good. I'm not going to be able to make a PR right away -- if you want to just do it that's fine with me.

@tech4him1 probably won't get to this soon so would love a PR when you have time or anyone else who sees this issue and wants it solved!

Hey, closing out old issues. Please re-open if you have additional questions, thanks!

@KyleAMathews This issue is still relevant in [email protected], we @freecodecamp are trying to resolve this. Would you like a new issue or should I reopen this one?

Let's reopen this one

Thanks for implementing @Yurickh!

Already commented in the linked PR, but this doesn't seem to work for me.

I'm having the same issue, also commented on the PR.

New issue created #13072

Was this page helpful?
0 / 5 - 0 ratings