Skip to content
This repository has been archived by the owner on Jan 11, 2023. It is now read-only.

Allow dynamic routes with file extension (e.g. api/blog/[slug].json.js) #139

Closed
niksurff opened this issue Feb 25, 2018 · 2 comments
Closed

Comments

@niksurff
Copy link

niksurff commented Feb 25, 2018

I'd love to be able to define dynamic routes such as api/blog/[slug].json.js which I can fetch or export as api/blog/the-title.json.

My use case is something like the following url structure.

  • /blog/[slug].html Serves html page
  • /blog/[slug].json.js Serves json version
  • /blog/[slug].md.js Serves markdown source

Also when exporting the current sapper-template app the api routes become files without the proper file extension and as such don't get served with the correct mime type by static file servers without extra effort. Or did I miss something?

Allowing multiple dynamic parts at one level might be cool, too. Such as /dynamic/route/[id]_[slug].js. Just a thought while we're updating the route pattern logic.

I tried to hack it together myself, but at the moment I am still figuring out the code base. Also RegExp... :octocat:

Rich-Harris added a commit that referenced this issue Feb 28, 2018
allow dynamic parameters inside route parts
@Rich-Harris
Copy link
Member

Excellent idea, thanks! Implemented in 0.7.5 (including the [id]_[slug].js bit).

Also when exporting the current sapper-template app the api routes become files without the proper file extension and as such don't get served with the correct mime type by static file servers without extra effort. Or did I miss something?

No, you didn't miss anything. It basically works by capturing requests and saving them; if the response is text/html then it will append /index.html because file servers will generally accommodate that, but otherwise it has to keep the path identical to what was requested, even if that means borking up the mime type.

With this change, it's more practical to use .json extensions, so we should do that and update our examples. I think I'm going to update the default template to use the /blog/some-post.json pattern, I prefer it to /api/blog/some-post.

@niksurff
Copy link
Author

niksurff commented Mar 1, 2018

@Rich-Harris Legend. Thanks a bunch. Your work is much appreciated. :octocat:

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants