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

gatsby develop does not serve static index.html files when directory is accessed #2352

Closed
tech4him1 opened this issue Oct 5, 2017 · 18 comments · Fixed by #7952
Closed

gatsby develop does not serve static index.html files when directory is accessed #2352

tech4him1 opened this issue Oct 5, 2017 · 18 comments · Fixed by #7952

Comments

@tech4him1
Copy link

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.

@tech4him1 tech4him1 mentioned this issue Oct 5, 2017
@KyleAMathews
Copy link
Contributor

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

@tech4him1
Copy link
Author

I wonder why there is both of these:

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

// Check if the file exists in the public folder.
app.get(`*`, (req, res, next) => {
// Load file but ignore errors.
res.sendFile(
directoryPath(`/public/${decodeURIComponent(req.url)}`),

@KyleAMathews
Copy link
Contributor

Haha probably an oversight.

@KyleAMathews
Copy link
Contributor

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

@tech4him1
Copy link
Author

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.

@KyleAMathews
Copy link
Contributor

Great! Yeah would love a PR.

@tech4him1
Copy link
Author

tech4him1 commented Oct 5, 2017

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.

@tech4him1
Copy link
Author

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

@KyleAMathews
Copy link
Contributor

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.

@tech4him1
Copy link
Author

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.

@KyleAMathews
Copy link
Contributor

@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!

@KyleAMathews
Copy link
Contributor

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

@Bouncey
Copy link
Contributor

Bouncey commented Sep 6, 2018

@KyleAMathews This issue is still relevant in gatsby@2.0.0-rc.10, we @freeCodeCamp are trying to resolve this. Would you like a new issue or should I reopen this one?

@pieh
Copy link
Contributor

pieh commented Sep 6, 2018

Let's reopen this one

@tech4him1
Copy link
Author

Thanks for implementing @Yurickh!

@simoneb
Copy link

simoneb commented Apr 2, 2019

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

@patriziosotgiu
Copy link

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

@simoneb
Copy link

simoneb commented Apr 3, 2019

New issue created #13072

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

Successfully merging a pull request may close this issue.

6 participants