PokeAPI/pokeapi

Decide on how to mirror functionality when switching to static files

Closed this issue · 5 comments

@neverendingqs As @sargunv pointed out, I don’t think the .json extension will be a problem. We can either use an index document (easy with S3, I’m sure Netlify makes it easy too), or we can write the files directly with no extension and ensure we set the mime type correctly (S3 all good, again I’m sure Netlify would have a way).

Looking at Netlify, I don't see explicit documentation about how to specify an index document. https://www.netlify.com/docs/redirects/#trailing-slash (especially the Pretty URLs feature) does suggest that Netlify handles trailing slashes and seems to suggest that it can map https://pokeapi.co/api/v2/language/index.json to https://pokeapi.co/api/v2/language/.

It seems like ditto clone should work as-is if we turn on this feature. Is this what you two had in mind?

Yeah, the key phrase is "it will also rewrite paths like /about.html to /about/". If that's hardcoded to .html, then no, but if it's configurable to .json (or includes .json by default), then yes. That kind of thing has been configurable in every HTTP server I've used, but I've never used Netlify. I think it's likely to work out.

If not, there's always the plan B of an Nginx instance in a VPS (Amazon or otherwise) which is certain to work, but not free.

EDIT: actually looking at it again that's slightly different from our use case. I'll just test it out on Netlify myself.

If not, there's always the plan B of an Nginx instance

I think plan B might be to use S3. Netlify abstracts away a lot of what you can set up manually with S3, but with the reduced work also comes reduced flexibility. If it doesn't give the flexibility we need, we can make S3 do what we need. It will still be a lot cheaper (and easier to manage) than setting up (and potentially scaling) a VPS.

I've just tested it, and looks like as long as the client is set up to properly handle a 301, the following rewrite rule works on Netlify:

/*  /:splatindex.json

Here's example output from httpie:

sargunv@castle ~  http https://jovial-bhaskara-cb0258.netlify.com/sub/
HTTP/1.1 301 Moved Permanently
Age: 1
Cache-Control: public, max-age=0, must-revalidate
Connection: keep-alive
Content-Length: 31
Content-Type: text/plain; charset=utf-8
Date: Sat, 08 Sep 2018 04:16:44 GMT
Location: /sub/index.json
Server: Netlify
X-NF-Request-ID: 4dda5619-e3f4-47d8-ac26-9dd503c7bff9-7505562

Redirecting to /sub/index.json

Specifying response code 200 in the redirect rule gets rid of the 301, so should work with all clients:

/*  /:splatindex.json  200

Example HTTP response:

sargunv@wincastle ~  http https://jovial-bhaskara-cb0258.netlify.com/sub/
HTTP/1.1 200 OK
Accept-Ranges: bytes
Age: 0
Cache-Control: public, max-age=0, must-revalidate
Connection: keep-alive
Content-Encoding: gzip
Content-Length: 50
Content-Type: application/json
Date: Sat, 08 Sep 2018 04:25:12 GMT
Etag: "c35b3c7aded3580ddb90d2a2f4f68ab1-ssl-df"
Server: Netlify
Strict-Transport-Security: max-age=31536000
Vary: Accept-Encoding
X-NF-Request-ID: 21d9bc6f-687c-4011-a98e-e83d2a3ab5ac-29640

{
    "hello": "subdirectory"
}

It looks like we can solve this with Netlify or with S3 + Cloudfront (plan B). I have added the necessary references to this issue, so I think we can close this one off, and revisit if needed.