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

Server returns response, not file #66

Closed
zoecarver opened this issue Apr 10, 2018 · 3 comments
Closed

Server returns response, not file #66

zoecarver opened this issue Apr 10, 2018 · 3 comments

Comments

@zoecarver
Copy link
Contributor

The following code will return the contents of index.html for everything.

fly.http.respondWith(async (req) => {
  const res = await fetch("file://index.html")
  res.headers.set("content-type", "text/html")
  return res
})

This is a problem because if index.html references a resource file like image.png, the contents of image.png will be the contents of index.html.

Possible Solution

All file url paths that are in .fly.yml should be overwritten and only return that file unless otherwise specified.

Current Workaround

This code works properly:

fly.http.respondWith(async (req) => {
  const url = new URL(req.url)
  const res = await fetch("file:/" + url.pathname)
  res.headers.set("content-type", "text/html")
  return res
})

Reproducible here: https://github.com/pudility/fly-error

@jeromegn
Copy link
Member

Hey there!

Does this reference example code we got wrong? I saw there are talks about that in Slack.

The first code sample you show will only serve index.html because, as you've figured out, it's hard-coded in there.

We provide basic functionality for people to arrange in whatever way they need to make things work for their use cases.

In your second example, that seems like what you're trying to do: for every request, check if the path is available as file. The response from this fetch could be a 404 if the file is not found.

If you're trying to load an image, then you're not setting the right content-type header though.

We strongly encourage people to write their own routing logic or use a package that can do that for them. Since there's only one "fetch" handler (that fly.http.respondWith callback), you need to have some form of dispatcher for the logic.

I think we should make that clearer, for sure.

As for your solution description:

All file url paths that are in .fly.yml should be overwritten and only return that file unless otherwise specified.

I'm not sure I understand it. Maybe we need an example app that serves files from pathnames as best it can.

This also makes me wonder if we should try to infer the content-type to avoid confusion and easy-to-make mistakes.

What's the problem you want to solve? I believe the functionality works as intended. We could use a better example for it though :)

@mrkurt
Copy link
Member

mrkurt commented Apr 10, 2018

I'm not sure I understand it. Maybe we need an example app that serves files from pathnames as best it can.

This also makes me wonder if we should try to infer the content-type to avoid confusion and easy-to-make mistakes.

I think we could make a basic static site app that does both of these pretty quickly. It'll be especially useful when we have globbing.

@zoecarver
Copy link
Contributor Author

@jeromegn

I'm not sure I understand it. Maybe we need an example app that serves files from pathnames as best it can.

After thinking about it, this was not a great idea and I think it would probably create more issues than it would solve. Documentation is probably the best rout in order to help people understand how it is supposed to be used.

Another solution might be doing something similar to what express does and have something like this:

app.use(express.static('public'))

For inferring the content-type, I completely agree - this would be a great feature.

I am happy to write some documentation on how to serve files, I am just not sure where the best place to do that would be. Maybe in a wiki?

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

3 participants